From owner-freebsd-hackers@FreeBSD.ORG Wed Dec 20 10:44:11 2006 Return-Path: X-Original-To: freebsd-hackers@freebsd.org Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id C54D916A412 for ; Wed, 20 Dec 2006 10:44:11 +0000 (UTC) (envelope-from hselasky@c2i.net) Received: from swip.net (mailfe09.swip.net [212.247.155.1]) by mx1.FreeBSD.org (Postfix) with ESMTP id 2A05A43CA0 for ; Wed, 20 Dec 2006 10:44:10 +0000 (GMT) (envelope-from hselasky@c2i.net) X-Cloudmark-Score: 0.000000 [] Received: from [193.217.102.48] (account mc467741@c2i.net HELO [10.0.0.249]) by mailfe09.swip.net (CommuniGate Pro SMTP 5.0.12) with ESMTPA id 192790238; Wed, 20 Dec 2006 10:43:44 +0100 From: Hans Petter Selasky To: freebsd-hackers@freebsd.org Date: Wed, 20 Dec 2006 10:43:26 +0100 User-Agent: KMail/1.7 References: <20061220041843.GA10511@dwpc.dwlabs.ca> In-Reply-To: <20061220041843.GA10511@dwpc.dwlabs.ca> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200612201043.27088.hselasky@c2i.net> Cc: Duane Whitty Subject: Re: Locking fundamentals X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 Dec 2006 10:44:11 -0000 On Wednesday 20 December 2006 05:18, Duane Whitty wrote: > Hello again, > > It seems to me that understanding locking holds the key to > understanding fbsd internals. > > Could someone review my understanding of fbsd locking fundamentals. > (No assertions here, just questions) > > lock_mgr > -------------------- > mutexes|sx_lock > ------------------- ^ > atomic | mem barriers | > > Don't lock if you don't need to. > Lock only what you need to. > Use the simplest lock that gets the job done. > Don't drop locks prematurely because acquiring locks is expensive. > When possible sleep rather than spin. > > ?????? > Memory barriers order operations > Atomic operations complete without being interrupted > > Atomic operations and memory barriers are the primitives. > > Mutexes are implemented by atomic operations and memory barriers. > Mutexes are relatively simple and inexpensive but may not recurse. This is not true. See MTX_RECURSE. > > Shared/exclusive locks are more versatile than mutexes in that they > may be upgraded or downgraded from or to shared/exclusive and they > may be acquired recursively. More expensive than mutexes. > > lock_mgr locks are used when reference counting is needed sx_locks are used when you sleep inside the locked piece of code. Mutexes does not allow you to sleep. > Would there be any particular sections of kernel code that you believe > might be enlightening with respects to locking? > > I have read the man pages describing each family of locks, John Baldwin's > BSDCon 2002 paper, Jeffrey Hsu's paper, the Arch handbook, and the source. > > I don't understand it all yet and it feels like I am missing some > fundamental point, especially concerning the lock_mgr family. Does > anyone have some pointers they feel like sharing? > > Does anyone have any good references they can point me to that is > relevant to fbsd given all the recent changes as a result of SMP? > Is The Design and Implementation of the FreeBSD Operating System > still current enough? --HPS