diff options
author | Ori Bernstein <ori@eigenstate.org> | 2016-01-05 18:32:47 -0800 |
---|---|---|
committer | Ori Bernstein <ori@eigenstate.org> | 2016-01-05 18:32:47 -0800 |
commit | 0fd71287884cec6e5d3a34307970a354ae99fc92 (patch) | |
tree | cd44dadfbc7b4e7ccb669f1449473b11e672c277 /lib/thread | |
parent | ca1d3da271cc83157b0e0578942a71e6abd130da (diff) | |
download | mc-0fd71287884cec6e5d3a34307970a354ae99fc92.tar.gz |
Add thread spawning, and a broken attempt at mutexes.
Also removes a reference to stack data in the tests, and
works around a crash in mbld test on plan9, where the printing
was confusing things.
TODO: fix mbld test properly.
Diffstat (limited to 'lib/thread')
-rw-r--r-- | lib/thread/bld.proj | 4 | ||||
-rw-r--r-- | lib/thread/mutex+plan9.myr | 44 | ||||
-rw-r--r-- | lib/thread/spawn+plan9.myr | 18 | ||||
-rw-r--r-- | lib/thread/test/atomic.myr | 1 | ||||
-rw-r--r-- | lib/thread/test/spawn.myr | 3 |
5 files changed, 66 insertions, 4 deletions
diff --git a/lib/thread/bld.proj b/lib/thread/bld.proj index e5adfae..56ee535 100644 --- a/lib/thread/bld.proj +++ b/lib/thread/bld.proj @@ -16,8 +16,8 @@ lib thread = # 9front impl of thread primitives #condvar+plan9.myr - #mutex+plan9.myr - #spawn+plan9.myr + mutex+plan9.myr + spawn+plan9.myr atomic-impl+plan9-x64.s atomic-impl+x64.s diff --git a/lib/thread/mutex+plan9.myr b/lib/thread/mutex+plan9.myr new file mode 100644 index 0000000..3ec8f8c --- /dev/null +++ b/lib/thread/mutex+plan9.myr @@ -0,0 +1,44 @@ +use std +use sys + + +use "atomic.use" +use "common.use" + +pkg thread = + type mutex = struct + _state : uint32 + _sem : uint32 + ;; + + const mkmtx : (-> mutex) + const mtxlock : (mtx : mutex# -> void) + const mtxtrylock : (mtx : mutex# -> bool) + const mtxunlock : (mtx : mutex# -> void) +;; + +const mkmtx = { + -> [._state = 0] +} + +const mtxlock = {mtx + if xadd(&mtx._state, 1) == 1 + -> void + ;; + + while sys.semacquire(&mtx._sem, 1) < 0 + /* interrupted; retry */ + ;; +} + +const mtxtrylock = {mtx + -> xcas(&mtx._state, 0, 1) == 0 +} + + +const mtxunlock = {mtx + if xadd(&mtx._state, -1) == 0 + -> void + ;; + sys.semrelease(&mtx._sem, 1) +} diff --git a/lib/thread/spawn+plan9.myr b/lib/thread/spawn+plan9.myr new file mode 100644 index 0000000..4397851 --- /dev/null +++ b/lib/thread/spawn+plan9.myr @@ -0,0 +1,18 @@ +use std +use sys + +pkg thread = + type tid = uint64 + + const spawn : (fn : (-> void) -> std.result(tid, byte[:])) +;; + +const spawn = {fn + match sys.rfork(sys.Rfproc | sys.Rfmem | sys.Rfnowait) + | 0: + fn() + std.exit(0) + | -1: -> `std.Fail "unable to spawn thread" + | thr: -> `std.Ok thr castto(tid) + ;; +}
\ No newline at end of file diff --git a/lib/thread/test/atomic.myr b/lib/thread/test/atomic.myr index 97cf448..c005677 100644 --- a/lib/thread/test/atomic.myr +++ b/lib/thread/test/atomic.myr @@ -24,7 +24,6 @@ const incvar = { for i = 0; i < 100_000; i++ thread.xadd(&val, 1) ;; - std.write(1, "done\n") thread.xadd(&done, 1) } diff --git a/lib/thread/test/spawn.myr b/lib/thread/test/spawn.myr index 6032ff0..2bd3b24 100644 --- a/lib/thread/test/spawn.myr +++ b/lib/thread/test/spawn.myr @@ -2,9 +2,10 @@ use std use thread var done : int32 +var capture const main = { - var capture, ptr + var ptr capture = 666 ptr = &capture |