From owner-freebsd-arch Sat Sep 23 12:45:33 2000 Delivered-To: freebsd-arch@freebsd.org Received: from whistle.com (s205m131.whistle.com [207.76.205.131]) by hub.freebsd.org (Postfix) with ESMTP id E5CDB37B424; Sat, 23 Sep 2000 12:45:29 -0700 (PDT) Received: (from smap@localhost) by whistle.com (8.10.0/8.10.0) id e8NJhAk07190; Sat, 23 Sep 2000 12:43:10 -0700 (PDT) Received: from bubba.whistle.com( 207.76.205.7) by whistle.com via smap (V2.0) id xma007188; Sat, 23 Sep 2000 12:42:54 -0700 Received: (from archie@localhost) by bubba.whistle.com (8.9.3/8.9.3) id MAA06755; Sat, 23 Sep 2000 12:42:47 -0700 (PDT) (envelope-from archie) From: Archie Cobbs Message-Id: <200009231942.MAA06755@bubba.whistle.com> Subject: Re: Mutexes and semaphores (was: cvs commit: src/sys/conf files src/sys/sys random.h src/sys/dev/randomdev hash.c hash.h harvest.c randomdev.c yarrow.c yarro) In-Reply-To: <200009130027.e8D0RrH91664@hak.lan.Awfulhak.org> "from Brian Somers at Sep 13, 2000 01:27:53 am" To: Brian Somers Date: Sat, 23 Sep 2000 12:42:47 -0700 (PDT) Cc: Joerg Micheel , Greg Lehey , Matthew Jacob , Frank Mayhar , John Baldwin , Mark Murray , FreeBSD-arch@FreeBSD.ORG X-Mailer: ELM [version 2.4ME+ PL82 (25)] MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII Sender: owner-freebsd-arch@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG Brian Somers writes: > > None, if it comes to the actual usage and implementation. Usually, > > people will use semaphores for locking blocks of code, mutexes for > > data structures. Not much of a difference in the end result, but > > locking data structures seems more natural to me. > > I would tend to disagree. > > A mutex is a way of guaranteeing that a resource isn't used more than > once. A semaphore is a mechanism for queuing a notification to a > (potentially) waiting process. > > A mutex is just a binary semaphore, but I don't think that's > relevant. This discussion (sorry I'm joining a week late) is somewhat fascinating to me, because everybody seems to have a slightly different definition of things. Now I don't know what to think :-) Does the following make sense? A SPIN LOCK is only used to serialize threads' access to data. A spin lock "protects" some region of memory. At the time a spin lock is acquired, you always know the data is in a consistent state. Moreover, a thread should never have to block on a spin lock for any longer than it takes the other thread(s) to access the protected data (and do nothing else!). All threads either holding or blocking on a spin lock are always "runnable" in a sense. It is illegal to "sleep" while holding a spin lock -- instead, you should arrange the data in a consistent state, release the spin lock, and then sleep. When you wake up, reacquire the spin lock. Since most data structures are small, spin lock hold times should be small and contention hopefully low. Therefore, spin-waiting is a feasible choice to implement spin lock blocking. There is no queue associated with a spin lock. Once you have the spin lock primitive, you can easily build semaphores, sleep queues, etc. A semaphore is just a counter plus a sleep queue -- all protected by the spin lock. A MUTEX is just a sepaphore whose initial count is 1. ?? -Archie ___________________________________________________________________________ Archie Cobbs * Whistle Communications, Inc. * http://www.whistle.com To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-arch" in the body of the message