summaryrefslogtreecommitdiff
path: root/lib/thread
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2016-05-02 23:45:28 -0700
committerOri Bernstein <ori@eigenstate.org>2016-05-02 23:45:28 -0700
commit36002110e45db5e89efe4bd664f1926dced83545 (patch)
treefffe734bcac5f4669d472d46024b1caaa7a770eb /lib/thread
parentd8bd5599e0e4b7919e3987ad80cb39f4a8aff63d (diff)
downloadmc-36002110e45db5e89efe4bd664f1926dced83545.tar.gz
Get OSX spawn to almost work.
We don't free the stack yet.
Diffstat (limited to 'lib/thread')
-rw-r--r--lib/thread/bld.proj2
-rw-r--r--lib/thread/spawn+osx.myr38
-rw-r--r--lib/thread/start+osx-x64.s22
3 files changed, 52 insertions, 10 deletions
diff --git a/lib/thread/bld.proj b/lib/thread/bld.proj
index 62cbd0b..f2532a1 100644
--- a/lib/thread/bld.proj
+++ b/lib/thread/bld.proj
@@ -18,7 +18,7 @@ lib thread =
#condvar+osx.myr
#mutex+osx.myr
spawn+osx.myr
- #exit+osx-x64.s
+ start+osx-x64.s
# 9front impl of thread primitives
#condvar+plan9.myr
diff --git a/lib/thread/spawn+osx.myr b/lib/thread/spawn+osx.myr
index 3c2f391..b9d5457 100644
--- a/lib/thread/spawn+osx.myr
+++ b/lib/thread/spawn+osx.myr
@@ -10,6 +10,24 @@ pkg thread =
const Stacksz = 8*std.MiB
extern const exit : (-> void)
+extern const start : (-> void)
+
+const __init__ = {
+ var ret
+
+ ret = sys.bsdthread_register(\
+ start castto(void#), \ /* start */
+ 0 castto(void#), \ /* wqthread */
+ 0 castto(uint32), \ /* sz */
+ 0 castto(uint32), \ /* dummy */
+ 0 castto(void#), \ /* targconc */
+ 0 castto(uint32)) /* queueoff */
+ if ret < 0
+ std.fatal("unable to init threads: {}", ret)
+ ;;
+}
+
+
const spawn = {fn
-> spawnstk(fn, Stacksz)
@@ -19,22 +37,24 @@ const spawnstk = {fn, sz
var tid : tid, ret
+ std.put("...hi? fn={}\n", fn castto(void#))
ret = sys.bsdthread_create( \
- startthread castto(void#), \
- &fn castto(void#), \
+ fn castto(void#), \
+ envptr(&fn), \
sz castto(void#), \
- &tid castto(void#), \
+ 0 castto(void#), \
0)
- if ret == -1 castto(void#)
+ if ret == (-1 castto(void#))
-> `std.Fail "couldn't spawn thread"
;;
- -> `std.Ok tid castto(tid)
+ -> `std.Ok ret castto(tid)
}
-const startthread = {fn : (-> void)# -> void
- fn#()
- std.write(1, "...bye mom\n")
- sys.bsdthread_terminate(0 castto(void#), 0, 0, 0)
+const envptr = {fn
+ var repr : std.intptr[2]
+
+ repr = (fn castto(std.intptr[2]#))#
+ -> repr[0] castto(void#)
}
diff --git a/lib/thread/start+osx-x64.s b/lib/thread/start+osx-x64.s
new file mode 100644
index 0000000..bc62d08
--- /dev/null
+++ b/lib/thread/start+osx-x64.s
@@ -0,0 +1,22 @@
+// The entry point for thread start, registered with bsdthread_register
+// %rdi: pthread (0, for us)
+// %rsi: mach thread port (ignored)
+// %rdx: func
+// %rcx: env
+// %r8: stack
+// %r9: flags (= 0)
+// %rsp: stack - C_64_REDZONE_LEN (= stack - 128)
+.globl _thread$start
+_thread$start:
+ /* call the function */
+# movq %r8, %rsp /* set up stack */
+ movq %rcx, %rax /* set up env */
+ callq *%rdx /* call function */
+
+ /* exit the thread */
+ movq $0x2000169, %rax /* Sysbsdthread_terminate */
+ movq %rsp, %rdi /* stack */
+ movq $0, %rsi /* len */
+ movq $0, %rdx /* sem */
+ syscall
+