Created
April 27, 2011 13:14
-
-
Save seadowg/944229 to your computer and use it in GitHub Desktop.
A general semaphore implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Semaphore: | |
def init(self, n): | |
self.lock = BinSemaphore(1) | |
self.delay = BinSemaphore(0) | |
self.var = n | |
def wait(self): | |
self.lock.wait() | |
self.var = self.var - 1 | |
if self.var < 0: | |
self.lock.signal() | |
self.delay.wait() | |
else: | |
self.lock.signal() | |
def signal(self): | |
self.lock.wait() | |
self.var = self.var + 1 | |
if self.var <= 0: | |
self.delay.signal() | |
self.lock.signal() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: Assumes BinSemaphore(a standard boolean/binary semaphore) implementation.