summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-08-02 10:04:34 -0700
committerOri Bernstein <ori@eigenstate.org>2018-08-02 10:04:34 -0700
commitb941cdfdf1940e4a64492daf1a15d921c32f2dc2 (patch)
tree4bb1b053cc652ba9dcb942126ddc85e01b6de35f
parent3d4d7c4ca9eb30d468d71c26f4b4588c800c9d1c (diff)
downloadmc-b941cdfdf1940e4a64492daf1a15d921c32f2dc2.tar.gz
Prettify mutex code a bit.
The if statements at the start were a touch odd.
-rw-r--r--lib/thread/mutex+futex.myr10
1 files changed, 6 insertions, 4 deletions
diff --git a/lib/thread/mutex+futex.myr b/lib/thread/mutex+futex.myr
index c8d40c6..a99f0a8 100644
--- a/lib/thread/mutex+futex.myr
+++ b/lib/thread/mutex+futex.myr
@@ -60,17 +60,19 @@ const mtxtrylock = {mtx
const mtxunlock = {mtx
/*
+ Either the lock is contended or it's uncontended. Any other
+ state is a bug.
+
Uncontended case: If the mutex state is not contended, and we still
are uncontended by the xchg() call, then it's safe to simply return;
nobody was waiting for us.
*/
- if mtx._state == Contended
- mtx._state = Unlocked
- elif xchg(&mtx._state, Unlocked) == Locked
+ if xcas(&mtx._state, Locked, Unlocked) == Locked
-> void
;;
- /* wake one thread */
+ /* Contended case: set the state to unlocked and wake one thread */
+ mtx._state = Unlocked
ftxwake(&mtx._state)
}