Skip to content

Instantly share code, notes, and snippets.

@exhesham
Created Mar 23, 2018
Embed
What would you like to do?
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