Skip to content

Instantly share code, notes, and snippets.

@seadowg
Created April 27, 2011 13:14
Show Gist options
  • Save seadowg/944229 to your computer and use it in GitHub Desktop.
Save seadowg/944229 to your computer and use it in GitHub Desktop.
A general semaphore implementation
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()
@seadowg
Copy link
Author

seadowg commented Apr 27, 2011

Note: Assumes BinSemaphore(a standard boolean/binary semaphore) implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment