home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!sun-barr!olivea!hal.com!darkstar.UCSC.EDU!osr
- From: Guido.van.Rossum@cwi.nl (Guido van Rossum)
- Newsgroups: comp.os.research
- Subject: implementing counting semaphores w. binary mutexes
- Message-ID: <17n07jINN794@darkstar.UCSC.EDU>
- Date: 29 Aug 92 04:59:31 GMT
- Organization: University of California, Santa Cruz
- Lines: 48
- Approved: comp-os-research@ftp.cse.ucsc.edu
- NNTP-Posting-Host: ftp.cse.ucsc.edu
- Originator: osr@ftp
-
-
- (Excuse me if this is a bozo question -- I'm somewhat out of touch
- with this field.)
-
- I have a threads package that efficiently implements mutexes (binary
- semaphores) with the usual lock and unlock operations. Now I have the
- need for a counting semaphore with down and up operations. My
- proposal to implement this is the following:
-
- - a semaphore is a structure containing an integer "level" and two
- mutexes, "mutex" (used to protect the data structure) and "block"
- (used to block when a down operation finds the level at zero).
-
- - to initialize a semaphore s:
-
- s.level = 0
- s.block = new_mutex()
- s.mutex = new_mutex()
- lock(s.block)
-
- - to implement down(s):
-
- lock(s.block)
- lock(s.mutex)
- s.level = s.level - 1
- if s.level > 0: unlock(s.block)
- unlock(s.mutex)
-
- - to implement up(s):
-
- lock(s.mutex)
- s.level = s.level + 1
- if s.level == 1: unlock(s.block)
- unlock(s.mutex)
-
- I have two questions about this:
-
- (a) has this implementation been described in the literature, and if
- so, where?
-
- (b) is it better or worse than other implementations, and why? (under
- the assumptions that only binary mutexes are available)
-
- (Note: I put this code in the Amoeba 4.0 library so some readers may
- be familiar with it -- I'm asking for descriptions elsewhere...)
-
- --Guido van Rossum, CWI, Amsterdam <guido@cwi.nl>
- "Well I'm a plumber. I can't act"
-