From owner-freebsd-hackers@freebsd.org Wed Jun 14 08:01:55 2017 Return-Path: Delivered-To: freebsd-hackers@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 6B0F6D88610 for ; Wed, 14 Jun 2017 08:01:55 +0000 (UTC) (envelope-from baijiaju1990@163.com) Received: from m12-17.163.com (m12-17.163.com [220.181.12.17]) by mx1.freebsd.org (Postfix) with ESMTP id E4C837E916 for ; Wed, 14 Jun 2017 08:01:54 +0000 (UTC) (envelope-from baijiaju1990@163.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=Message-ID:Date:From:MIME-Version:Subject; bh=wvz85 0iao2G+3Q/WDIDDk5H3Nc0WTVbwzh7BUYYCyhY=; b=W+0PqNpIGt2DJrcqTQybe 191AejjqhUc3RtCf7oMhavXvuWvBIgfXJVQd9qlB7KOKQSEdhkUbxBzQu1XY6JVf E67EKOq4IJD4ad20ch/SEent8n7IPMwXAmYBT1umIl11EFrCR62if5SM6FbOWSRr zFj1MeCI8rz9Tb5EoRakjE= Received: from [166.111.70.19] (unknown [166.111.70.19]) by smtp13 (Coremail) with SMTP id EcCowADX7i7X6UBZzdtdKw--.55961S2; Wed, 14 Jun 2017 15:46:32 +0800 (CST) Message-ID: <5940EA87.4030002@163.com> Date: Wed, 14 Jun 2017 15:49:27 +0800 From: Jia-Ju Bai User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120410 Thunderbird/11.0.1 MIME-Version: 1.0 To: kostikbel@gmail.com CC: freebsd-hackers@freebsd.org Subject: Re: Re: A question about in lock usage in FreeBSD X-CM-TRANSID: EcCowADX7i7X6UBZzdtdKw--.55961S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7CFW7ZFy3uF4rCF43tF4ruFg_yoW8Xr13pF Z3Gas8tFs5Zw48Zwnrt3yfur40y3y7GFW5Xry3WF1xZryj9an3KrW7KFZ8urWfWr1xXa4v vw45XF97Aa90vaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jkKsUUUUUU= X-Originating-IP: [166.111.70.19] X-CM-SenderInfo: xedlyx5dmximizq6il2tof0z/1tbiThj2elUCyYSXHAAAsL Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Content-Filtered-By: Mailman/MimeDel 2.1.23 X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Jun 2017 08:01:55 -0000 > On Wed, Jun 14, 2017 at 09:10:29AM +0800, Jia-Ju Bai wrote: > >/ Hi, /> >/ /> >/ I am a freshman in developing FreeBSD drivers, and I have a question in /> >/ lock usage in FreeBSD. /> >/ /> >/ The kernel provides some kinds of locks in developing drivers, such as /> >/ "mutex lock", "mutex spin lock", "rw lock" and "sx lock". I want to know /> >/ which locks should be held when the thread can sleep. /> Replace should with can in the question. > >/ From my knowledge of FreeBSD document, I make the following points: /> >/ (1) "rw lock" and "sx lock" can be used in this situation; /> You cannot own rw lock while trying to sleep. > >/ (2) "mutex lock" and "mutex spin lock" are forbidden in this situation. /> >/ /> >/ If my points are right, I will make another point: /> >/ mutex_lock will let the thread sleep when the lock is requested, so the /> >/ mutex_lock can not be called in nested style (namely it is unsafe that /> >/ mutex_lock is called again when a "mutex lock" is held). /> No, mutex does not put the thread which cannot obtain the lock immediately, > to sleep. The thread is blocked on turnstile. > > >/ /> >/ Are my points are right? /> >/ I am looking forward to useful opinions and answers :) /> Read locking(9). Thank you very much, your reply is very helpful to me :) According to your advice, I think I get the answer from locking(9). Namely, it is unsafe that the thread calls the function which may sleep when the thread holds mutexes, reader/writer locks or read-mostly locks. Is it right? Thanks, Jia-Ju Bai