summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2015-09-17 23:53:59 -0700
committerOri Bernstein <ori@eigenstate.org>2015-09-17 23:53:59 -0700
commit1fd6d73d4da455a2ded723e320c7451d5076eb83 (patch)
tree757f86a89847c67d3e0051b06164c03f75a887d4
parenta8a61e673c000c9802adb0d5a6fccca87967a90e (diff)
downloadmc-1fd6d73d4da455a2ded723e320c7451d5076eb83.tar.gz
Add basic test for condvars.
-rw-r--r--lib/thread/condvar+linux.myr2
-rw-r--r--lib/thread/test/condvar.myr89
2 files changed, 90 insertions, 1 deletions
diff --git a/lib/thread/condvar+linux.myr b/lib/thread/condvar+linux.myr
index d8f048b..5d30274 100644
--- a/lib/thread/condvar+linux.myr
+++ b/lib/thread/condvar+linux.myr
@@ -45,6 +45,6 @@ const condsignal = {cond : cond#
const condbroadcast = {cond : cond#
xadd(&cond._seq, 1)
- sys.futex(&cond._seq, sys.Futexrequeue | sys.Futexpriv, 1, Zptr, &cond._mtx._state, 0)
+ sys.futex(&cond._seq, sys.Futexwake | sys.Futexpriv, 0x7fffffff, Zptr, Zptr, 0)
}
diff --git a/lib/thread/test/condvar.myr b/lib/thread/test/condvar.myr
new file mode 100644
index 0000000..94cda94
--- /dev/null
+++ b/lib/thread/test/condvar.myr
@@ -0,0 +1,89 @@
+use std
+use thread
+
+use "test/util.use"
+
+const Nwakes = 1000
+
+var cv
+var mtx
+var val
+
+var done : int32
+var nwoken : int32
+var nready : int32
+var locked : int32
+
+const main = {
+ done = 0
+ val = 123
+
+ mtx = thread.mkmtx()
+ cv = thread.mkcond(&mtx)
+ thread.spawn(cvwait)
+ thread.spawn(cvwake)
+ while done == 0
+ /* nothing */
+ ;;
+ std.assert(nwoken == Nwakes, "wrong number of wakes")
+ std.assert(val == 123, "wrong val after all are done")
+
+ nwoken = 0
+ nready = 0
+ mkherd(100, cvwaitonce)
+
+ /* wait until the herd is ready */
+ while nready != 100 /* 0 to 99 */
+ /* nothing */
+ ;;
+ while locked == 0
+ /* nothing */
+ ;;
+ thread.condbroadcast(&cv)
+ while nwoken != 100
+ std.put("{}\n", nwoken)
+ /* nothing */
+ ;;
+ std.assert(nwoken == 100, "wrong thread count woken")
+
+}
+
+const cvwait = {
+ for var i = 0; i < Nwakes; i++
+ thread.mtxlock(&mtx)
+ thread.condwait(&cv)
+ std.assert(val == 456, "wrong val after signal\n")
+ val = 123
+ thread.mtxunlock(&mtx)
+
+ thread.xadd(&nwoken, 1)
+ ;;
+ val = 123
+ thread.xadd(&done, 1)
+
+}
+
+const cvwake = {
+ while true
+ thread.mtxlock(&mtx)
+ val = 456
+ thread.mtxunlock(&mtx)
+
+ thread.condsignal(&cv)
+ if nwoken >= Nwakes
+ break
+ ;;
+ ;;
+}
+
+const cvwaitonce = {
+ thread.xadd(&nready, 1)
+
+ thread.mtxlock(&mtx)
+ thread.xadd(&locked, 1)
+ thread.condwait(&cv)
+ thread.mtxunlock(&mtx)
+
+ thread.xadd(&nwoken, 1)
+}
+