From owner-freebsd-drivers@freebsd.org Thu Jun 15 07:00:01 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 F06CDC31C04 for ; Thu, 15 Jun 2017 07:00:01 +0000 (UTC) (envelope-from jmg@gold.funkthat.com) Received: from gold.funkthat.com (gate2.funkthat.com [208.87.223.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "gate2.funkthat.com", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 6FE8A66DE6 for ; Thu, 15 Jun 2017 07:00:01 +0000 (UTC) (envelope-from jmg@gold.funkthat.com) Received: from gold.funkthat.com (localhost [127.0.0.1]) by gold.funkthat.com (8.15.2/8.15.2) with ESMTPS id v5F6nwms082485 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 14 Jun 2017 23:49:58 -0700 (PDT) (envelope-from jmg@gold.funkthat.com) Received: (from jmg@localhost) by gold.funkthat.com (8.15.2/8.15.2/Submit) id v5F6nvV1082484; Wed, 14 Jun 2017 23:49:58 -0700 (PDT) (envelope-from jmg) Date: Wed, 14 Jun 2017 23:49:57 -0700 From: John-Mark Gurney To: Jia-Ju Bai Cc: freebsd-drivers@freebsd.org Subject: Re: A question about in lock usage in FreeBSD Message-ID: <20170615064957.GA2042@funkthat.com> Mail-Followup-To: Jia-Ju Bai , freebsd-drivers@freebsd.org References: <593FE1C9.2060706@163.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <593FE1C9.2060706@163.com> X-Operating-System: FreeBSD 11.0-RELEASE-p7 amd64 X-PGP-Fingerprint: D87A 235F FB71 1F3F 55B7 ED9B D5FF 5A51 C0AC 3D65 X-Files: The truth is out there X-URL: https://www.funkthat.com/ X-Resume: https://www.funkthat.com/~jmg/resume.html X-TipJar: bitcoin:13Qmb6AeTgQecazTWph4XasEsP7nGRbAPE X-to-the-FBI-CIA-and-NSA: HI! HOW YA DOIN? can i haz chizburger? User-Agent: Mutt/1.6.1 (2016-04-27) X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.4.3 (gold.funkthat.com [127.0.0.1]); Wed, 14 Jun 2017 23:49:58 -0700 (PDT) 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: Thu, 15 Jun 2017 07:00:02 -0000 dropped -quesetions as that isn't the best list for this question. Jia-Ju Bai wrote this message on Tue, Jun 13, 2017 at 20:59 +0800: > 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 :) Have you read the locking(9)[1] man page? That should answer all of your questions wrt locking. [1] https://www.freebsd.org/cgi/man.cgi?query=locking&sektion=9&apropos=0&manpath=FreeBSD+11.0-RELEASE+and+Ports -- John-Mark Gurney Voice: +1 415 225 5579 "All that I will do, has been done, All that I have, has not."