shithub: mc

Download patch

ref: b941cdfdf1940e4a64492daf1a15d921c32f2dc2
parent: 3d4d7c4ca9eb30d468d71c26f4b4588c800c9d1c
author: Ori Bernstein <ori@eigenstate.org>
date: Thu Aug 2 06:04:34 EDT 2018

Prettify mutex code a bit.

	The if statements at the start were a touch odd.

--- a/lib/thread/mutex+futex.myr
+++ b/lib/thread/mutex+futex.myr
@@ -60,17 +60,19 @@
 
 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)
 }