summaryrefslogtreecommitdiff
path: root/lib/thread
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-01-06 12:27:22 -0800
committerOri Bernstein <ori@eigenstate.org>2016-01-06 12:27:22 -0800
commitf3c06e95f5a15488e1a99a272c3f0123dc5516a7 (patch)
tree55503f7546db0c5a6cedbd9c17c70b4efb636446 /lib/thread
parent0fd71287884cec6e5d3a34307970a354ae99fc92 (diff)
downloadmc-f3c06e95f5a15488e1a99a272c3f0123dc5516a7.tar.gz
Fix mutexes.
Turns out atomic increments weren't returning the old value. Oops.
Diffstat (limited to 'lib/thread')
-rw-r--r--lib/thread/atomic-impl+plan9-x64.s3
-rw-r--r--lib/thread/mutex+plan9.myr9
-rw-r--r--lib/thread/test/mutex.myr1
3 files changed, 9 insertions, 4 deletions
diff --git a/lib/thread/atomic-impl+plan9-x64.s b/lib/thread/atomic-impl+plan9-x64.s
index 809c480..2b4f5dd 100644
--- a/lib/thread/atomic-impl+plan9-x64.s
+++ b/lib/thread/atomic-impl+plan9-x64.s
@@ -20,12 +20,15 @@ TEXT thread$xsetp+0(SB),1,$0
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
diff --git a/lib/thread/mutex+plan9.myr b/lib/thread/mutex+plan9.myr
index 3ec8f8c..7c87218 100644
--- a/lib/thread/mutex+plan9.myr
+++ b/lib/thread/mutex+plan9.myr
@@ -18,11 +18,12 @@ pkg thread =
;;
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 mtxtrylock = {mtx
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)
}
diff --git a/lib/thread/test/mutex.myr b/lib/thread/test/mutex.myr
index 5a439d7..226bef5 100644
--- a/lib/thread/test/mutex.myr
+++ b/lib/thread/test/mutex.myr
@@ -29,6 +29,5 @@ const incvar = {
val++
thread.mtxunlock(&mtx)
;;
- std.write(1, "done\n")
thread.xadd(&done, 1)
}