From owner-freebsd-hackers@freebsd.org Wed Jun 14 01:22:50 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 95FC0C316FE for ; Wed, 14 Jun 2017 01:22:50 +0000 (UTC) (envelope-from baijiaju1990@163.com) Received: from m12-11.163.com (m12-11.163.com [220.181.12.11]) by mx1.freebsd.org (Postfix) with ESMTP id E1204738FE for ; Wed, 14 Jun 2017 01:22:49 +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=jVk8yxM9pM+cquyVTKg+m XlpH40/7KhPcxrYBpd4EGtLWtWKCL6CsUL1Ihb9oR+ZIETPRAd81dLw0ik5+hi1/ dGZyAQdAnaoPPtrP78RawN/FdYk0mL6fEvQGzsvuS4Bs9TiHDZjcQxO7fbz51hWR UDLF9DybDd8R2RleXt3ykE= Received: from [166.111.70.19] (unknown [166.111.70.19]) by smtp7 (Coremail) with SMTP id C8CowACHjxdWjEBZoRdjLQ--.44985S2; Wed, 14 Jun 2017 09:07:34 +0800 (CST) Message-ID: <59408D05.7070407@163.com> Date: Wed, 14 Jun 2017 09:10:29 +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-hackers@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: C8CowACHjxdWjEBZoRdjLQ--.44985S2 X-Coremail-Antispam: 1Uf129KBjvdXoWrZF4fXr4kGF1kury8WF47XFb_yoWfJrg_Ca y0kFWDCa1agr4Sqa1jgF4fCr4jgF48ury5AFyrAF13Jw1fJa97WF1kua4xZF4fJF45Aw4D tr98Aa4a9rya9jkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IU5bID7UUUUU== X-Originating-IP: [166.111.70.19] X-CM-SenderInfo: xedlyx5dmximizq6il2tof0z/1tbiYxb2elaDtYC0QwAAsu X-Mailman-Approved-At: Wed, 14 Jun 2017 01:39:22 +0000 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 01:22:50 -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