ref: f3c06e95f5a15488e1a99a272c3f0123dc5516a7
parent: 0fd71287884cec6e5d3a34307970a354ae99fc92
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Jan 6 07:27:22 EST 2016
Fix mutexes. Turns out atomic increments weren't returning the old value. Oops.
--- a/lib/thread/atomic-impl+plan9-x64.s
+++ b/lib/thread/atomic-impl+plan9-x64.s
@@ -20,12 +20,15 @@
TEXT thread$xadd32+0(SB),1,$0
LOCK; XADDL SI, (DI)
+ MOVL SI, AX
RET
TEXT thread$xadd64+0(SB),1,$0
LOCK; XADDQ SI, (DI)
+ MOVQ SI, AX
RET
TEXT thread$xaddp+0(SB),1,$0
LOCK; XADDQ SI, (DI)
+ MOVQ SI, AX
RET
TEXT thread$xsub32+0(SB),1,$0
--- a/lib/thread/mutex+plan9.myr
+++ b/lib/thread/mutex+plan9.myr
@@ -18,11 +18,12 @@
;;
const mkmtx = {
- -> [._state = 0]
+ -> [._state = 0, ._sem=0]
}
const mtxlock = {mtx
- if xadd(&mtx._state, 1) == 1
+ /* if the old value was 0, we aren't contended */
+ if xadd(&mtx._state, 1) == 0
-> void
;;
@@ -37,8 +38,10 @@
const mtxunlock = {mtx
- if xadd(&mtx._state, -1) == 0
+ /* if we were the only thread waiting on the lock, there was no contention */
+ if xadd(&mtx._state, -1) == 1
-> void
;;
+
sys.semrelease(&mtx._sem, 1)
}
--- a/lib/thread/test/mutex.myr
+++ b/lib/thread/test/mutex.myr
@@ -29,6 +29,5 @@
val++
thread.mtxunlock(&mtx)
;;
- std.write(1, "done\n")
thread.xadd(&done, 1)
}