From owner-freebsd-drivers@freebsd.org Tue Jun 13 12:57:08 2017 Return-Path: Delivered-To: freebsd-drivers@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 6EA6CBF3AC2; Tue, 13 Jun 2017 12:57:08 +0000 (UTC) (envelope-from baijiaju1990@163.com) Received: from m12-13.163.com (m12-13.163.com [220.181.12.13]) by mx1.freebsd.org (Postfix) with ESMTP id 8345E7ADFE; Tue, 13 Jun 2017 12:57:06 +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=FCpqg Q6/YH51f4vEb4KhA/0F3cbFe44Ny+1OZ9HhXeE=; b=RX2gfLmA6EPbXbPlM8+AF HZvibOTxeeBY66D3EWEKtoIoRe9nnK8gitb7nHNIAZOZ0CUYzH0NB1+M2xId9Xjf 3+bG+JvHayuU3d3tioOJokil0UglfGJa8V4jx/7TGF+Rp2ENx3uQ2WOcHWcxwkF1 f/0US7QR3FN2zmrFFqaIqk= Received: from [166.111.70.19] (unknown [166.111.70.19]) by smtp9 (Coremail) with SMTP id DcCowACHt+Mc4T9ZjLnlKQ--.54220S2; Tue, 13 Jun 2017 20:57:00 +0800 (CST) Message-ID: <593FE1C9.2060706@163.com> Date: Tue, 13 Jun 2017 20:59:53 +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: freebsd-questions@freebsd.org, freebsd-drivers@freebsd.org Subject: A question about in lock usage in FreeBSD Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-CM-TRANSID: DcCowACHt+Mc4T9ZjLnlKQ--.54220S2 X-Coremail-Antispam: 1Uf129KBjvdXoWrZF4fXr4kGF1kury8WF47XFb_yoWfJrg_Ca y0kFWDCa1agr4Sqa1jgF4fCr4jgF48ury5AFyrAF13Jw1fJa97WF1kua4xZF4fJF45Aw4D tr98Aa4a9rya9jkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IU56uWJUUUUU== X-Originating-IP: [166.111.70.19] X-CM-SenderInfo: xedlyx5dmximizq6il2tof0z/1tbiYxv1elaDtXir4AAAsk X-BeenThere: freebsd-drivers@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Writing device drivers for FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 13 Jun 2017 12:57:08 -0000 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. From my knowledge of FreeBSD document, I make the following points: (1) "rw lock" and "sx lock" can be used in this situation; (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). Are my points are right? I am looking forward to useful opinions and answers :) Thanks in advance :) Jia-Ju Bai