shithub: mc

Download patch

ref: 36002110e45db5e89efe4bd664f1926dced83545
parent: d8bd5599e0e4b7919e3987ad80cb39f4a8aff63d
author: Ori Bernstein <ori@eigenstate.org>
date: Mon May 2 19:45:28 EDT 2016

Get OSX spawn to almost work.

    We don't free the stack yet.

--- a/lib/thread/bld.proj
+++ b/lib/thread/bld.proj
@@ -18,7 +18,7 @@
 	#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
--- a/lib/thread/spawn+osx.myr
+++ b/lib/thread/spawn+osx.myr
@@ -10,7 +10,25 @@
 
 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 @@
 	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#)
 }
 
--- /dev/null
+++ b/lib/thread/start+osx-x64.s
@@ -1,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
+