Skip to content

Instantly share code, notes, and snippets.

@exhesham
Created March 23, 2018 18:36
Show Gist options
  • Save exhesham/00fe10fd9a6867b4001d718fd4b2f61f to your computer and use it in GitHub Desktop.
Save exhesham/00fe10fd9a6867b4001d718fd4b2f61f to your computer and use it in GitHub Desktop.
mutex
function lock(boolean *lock) {
while (test_and_set(lock) == 1);
}
/* set the lock to true and send its previous value. lock=true means it is locked.
Notice that the test_and_lock always locks the lock (set lock == true) and returns the previous value before the locking*/
boolean_ref test_and_set(boolean_ref lock) {
boolean initial = lock;
lock = true;
return initial;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment