Skip to content

Instantly share code, notes, and snippets.

@itolosa
Created February 25, 2015 20:04
Show Gist options
  • Select an option

  • Save itolosa/c67aa7ac61ba967a35ff to your computer and use it in GitHub Desktop.

Select an option

Save itolosa/c67aa7ac61ba967a35ff to your computer and use it in GitHub Desktop.
Mutex implementation with no spinlocks
def wait():
condmut.acquire()
sleepQueue.enqueue(P)
condmut.release()
sleep(P)
def signal():
condmut.acquire()
P = sleepQueue.dequeue(NoWait)
if not P:
return -1
signal(P)
condmut.release()
def wait():
mutex.acquire()
if deactivate:
mutex.release()
perform()
else:
in_count += 1
mutex.release()
x.wait()
auxmut.acquire()
out_count += 1
auxmut.release()
if deactivate:
perform()
def abort():
mutex.acquire()
deactivate = true
while out_count < in_count:
x.signal()
mutex.release()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment