summaryrefslogtreecommitdiff
path: root/lib/thread
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-05-09 08:53:59 +1200
committerOri Bernstein <ori@eigenstate.org>2016-05-09 08:55:15 +1200
commitd2a28cc46d5101760733f1b831287f6b9fcf1be1 (patch)
tree7828a69856bca119d4240916c30c7de8695c865b /lib/thread
parentc08505fb46fd8cdfee6b6f425709e5bc536f6a75 (diff)
downloadmc-d2a28cc46d5101760733f1b831287f6b9fcf1be1.tar.gz
Add support for OpenBSD thread spawning.
Diffstat (limited to 'lib/thread')
-rw-r--r--lib/thread/bld.sub1
-rw-r--r--lib/thread/spawn+linux.myr1
-rw-r--r--lib/thread/spawn+openbsd.myr55
3 files changed, 54 insertions, 3 deletions
diff --git a/lib/thread/bld.sub b/lib/thread/bld.sub
index 3d0ce27..35cc5a6 100644
--- a/lib/thread/bld.sub
+++ b/lib/thread/bld.sub
@@ -27,6 +27,7 @@ lib thread =
# openbsd impl of thread primitives
spawn+openbsd.myr
+ exit+openbsd-x64.s
atomic-impl+x64.s
diff --git a/lib/thread/spawn+linux.myr b/lib/thread/spawn+linux.myr
index 21ab654..a144374 100644
--- a/lib/thread/spawn+linux.myr
+++ b/lib/thread/spawn+linux.myr
@@ -42,7 +42,6 @@ const spawnstk = {fn, sz
&ctid, 0 castto(byte#), \
startthread castto(void#)) castto(tid)
if ret < 0
- std.put("errno={}\n", -ret)
-> `std.Fail "couldn't spawn thread"
;;
-> `std.Ok ret
diff --git a/lib/thread/spawn+openbsd.myr b/lib/thread/spawn+openbsd.myr
index 92cca94..d330b56 100644
--- a/lib/thread/spawn+openbsd.myr
+++ b/lib/thread/spawn+openbsd.myr
@@ -1,4 +1,5 @@
use std
+use sys
pkg thread =
type tid = uint64
@@ -6,7 +7,57 @@ pkg thread =
const spawn : (fn : (-> void) -> std.result(tid, byte[:]))
;;
+const Stacksz = 8*std.MiB
+extern const exit : (-> void)
+
const spawn = {fn;
- std.die("threads not supported yet on openbsd\n")
- -> `std.Fail "not supported"
+ -> spawnstk(fn, Stacksz)
+}
+
+const spawnstk = {fn, sz
+ var stk, szp, fp, tos, tfp
+ var ret
+
+ stk = getstk(sz)
+ if stk == sys.Mapbad
+ -> `std.Fail "couldn't get stack"
+ ;;
+ /* store size */
+ tos = stk castto(std.intptr)
+ tos -= sizeof(int64)
+ szp = tos castto(sys.size#)
+ szp# = Stacksz
+
+ /* store func */
+ tos -= sizeof((->void))
+ fp = tos castto((->void)#)
+ fp# = fn
+
+ tfp = [
+ .tcb = 0 castto(void#),
+ .tid = &ret,
+ .stk = tos castto(byte#),
+ ]
+ if sys.__tfork_thread(&tfp, sizeof(sys.tforkparams), startthread castto(void#), 0 castto(void#)) < 0
+ -> `std.Fail "couldn't spawn thread"
+ ;;
+ -> `std.Ok (ret castto(tid))
+}
+
+const getstk = {sz
+ var p, m
+
+ p = sys.mmap(0 castto(byte#), sz, sys.Mprotrw, sys.Mpriv | sys.Manon, -1, 0)
+ if p == sys.Mapbad
+ -> p
+ ;;
+ /* stack starts at the top of memory and grows down. */
+ m = p castto(std.intptr)
+ m += sz castto(std.intptr)
+ -> m castto(byte#)
+}
+
+const startthread = {fn : (-> void)
+ fn()
+ exit()
}