summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2018-11-03 16:26:58 -0700
committerOri Bernstein <ori@eigenstate.org>2018-11-03 16:26:58 -0700
commit183f3d40a1dbc2254f1e01de21eb022996f40ad2 (patch)
tree5c1a10596ea852a16bca3bb27ac4e6e7f812e82d
parente70e83ae4362b60e9eb2134fb5d388576ee90bdf (diff)
downloadmc-183f3d40a1dbc2254f1e01de21eb022996f40ad2.tar.gz
First attempt at a queue API.
Simple, naive, but seems to work.
-rw-r--r--lib/thread/bld.sub1
-rw-r--r--lib/thread/queue.myr65
-rw-r--r--lib/thread/test/queue.myr46
3 files changed, 112 insertions, 0 deletions
diff --git a/lib/thread/bld.sub b/lib/thread/bld.sub
index 57aed13..5cc94a1 100644
--- a/lib/thread/bld.sub
+++ b/lib/thread/bld.sub
@@ -5,6 +5,7 @@ lib thread =
# higher level apis
future.myr
do.myr
+ queue.myr
# generic fallbacks
condvar.myr
diff --git a/lib/thread/queue.myr b/lib/thread/queue.myr
new file mode 100644
index 0000000..f65862e
--- /dev/null
+++ b/lib/thread/queue.myr
@@ -0,0 +1,65 @@
+use std
+use "mutex"
+use "condvar"
+
+pkg thread =
+ type queue(@a) = struct
+ hd : qnode(@a)#
+ tl : qnode(@a)#
+ mtx : mutex
+ cv : cond
+ ;;
+
+ type qnode(@a) = struct
+ v : @a
+ next : qnode(@a)#
+ ;;
+
+ generic qinit : (q : queue(@a)# -> void)
+ generic qget : (q : queue(@a)# -> @a)
+ generic qput : (q : queue(@a)#, v : @a -> void)
+;;
+
+generic Znode = (0 : qnode(@a)#)
+
+generic qinit = {q
+ q.hd = Znode
+ q.tl = Znode
+ q.mtx = mkmtx()
+ q.cv = mkcond(&q.mtx)
+}
+
+
+generic qput = {q, v : @a
+ var n : qnode(@a)#
+
+ n = std.mk([.next=Znode, .v=v])
+ mtxlock(&q.mtx)
+ if q.hd == Znode
+ q.hd = n
+ q.tl = n
+ else
+ q.tl.next = n
+ q.tl = n
+ ;;
+ condsignal(&q.cv)
+ mtxunlock(&q.mtx)
+}
+
+generic qget = {q
+ var n, v
+
+ mtxlock(&q.mtx)
+:again
+ if q.hd == Znode
+ condwait(&q.cv)
+ goto again
+ else
+ n = q.hd
+ q.hd = q.hd.next
+ ;;
+ mtxunlock(&q.mtx)
+ v = n.v
+ std.free(n)
+ -> v
+}
diff --git a/lib/thread/test/queue.myr b/lib/thread/test/queue.myr
new file mode 100644
index 0000000..c38bf5b
--- /dev/null
+++ b/lib/thread/test/queue.myr
@@ -0,0 +1,46 @@
+use std
+use thread
+use testr
+
+const Nproduced = 100
+
+const main = {
+ testr.run([
+ [.name="simple-singlethreaded", .fn=simple],
+ [.name="slow-producer", .fn={ctx; producerconsumer(ctx, 100, 100)}],
+ [.name="fast-producer", .fn={ctx; producerconsumer(ctx, 10_000, 0)}],
+ ][:])
+}
+
+const simple = {ctx
+ var q
+
+ /* simple test */
+ thread.qinit(&q)
+ thread.qput(&q, 123)
+ thread.qput(&q, 246)
+
+ testr.check(ctx, thread.qget(&q) == 123, "expected 123")
+ testr.check(ctx, thread.qget(&q) == 246, "expected 246")
+}
+
+const producerconsumer = {ctx, count, delay
+ var qp
+
+ qp = std.alloc()
+ thread.qinit(qp)
+ thread.spawn({; producer(qp, count, delay)})
+
+ for var i = 0; i < count; i++
+ testr.check(ctx, thread.qget(qp) == i, "expected {}\n", i)
+ ;;
+}
+
+const producer = {qp, count, delay
+ for var i = 0; i < count; i++
+ thread.qput(qp, i)
+ if delay > 0
+ std.usleep(delay)
+ ;;
+ ;;
+}