Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 23 Sep 2000 12:42:47 -0700 (PDT)
From:      Archie Cobbs <archie@whistle.com>
To:        Brian Somers <brian@Awfulhak.org>
Cc:        Joerg Micheel <joerg@cs.waikato.ac.nz>, Greg Lehey <grog@lemis.com>, Matthew Jacob <mjacob@feral.com>, Frank Mayhar <frank@exit.com>, John Baldwin <jhb@pike.osd.bsdi.com>, Mark Murray <markm@FreeBSD.ORG>, FreeBSD-arch@FreeBSD.ORG
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)
Message-ID:  <200009231942.MAA06755@bubba.whistle.com>
In-Reply-To: <200009130027.e8D0RrH91664@hak.lan.Awfulhak.org> "from Brian Somers at Sep 13, 2000 01:27:53 am"

next in thread | previous in thread | raw e-mail | index | archive | help
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




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200009231942.MAA06755>