shithub: mc

Download patch

ref: 4078e565e99dfb4abf07df16f0317c85a6bca3f1
parent: 73518d58457d543258e344b96a8f4b13ccd16882
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Apr 8 22:09:15 EDT 2017

Duplicate env on thread spawn.

--- a/lib/std/fndup.myr
+++ b/lib/std/fndup.myr
@@ -27,10 +27,12 @@
 
 generic fnbdup = {fn, buf
 	var repr : intptr[2]
+	var env
 
 	repr = (&fn : intptr[2]#)#
-	slcp(buf, envslice(repr[0]))
-	repr[0] = (buf[0] : intptr)
+	env = envslice(repr[0])
+	slcp(buf[:env.len], env)
+	repr[0] = (buf : intptr)
 	-> (&repr : @fn::function#)#
 }
 
@@ -45,6 +47,9 @@
 	var env : byte#
 	var szp : intptr#
 
+	if ep == 0
+		-> [][:]
+	;;
 	env = (ep : byte#)
 	szp = (env : intptr#)
 	-> env[:szp#]
--- a/lib/thread/spawn+freebsd.myr
+++ b/lib/thread/spawn+freebsd.myr
@@ -17,7 +17,7 @@
 
 const spawnstk = {fn, sz
 	var stk : byte#, tid, ctid, ret
-	var szp, fp, tos
+	var szp, fp, tos, env, envsz
 
 	stk = getstk(sz)
 	if stk == sys.Mapbad
@@ -34,10 +34,13 @@
 	szp# = Stacksz
 
 	/* store the function we call */
+	envsz = std.fnenvsz(fn)
+	tos -= (envsz : std.intptr)
+	env = tos
 	tos -= sizeof((->void))
 	sz -= sizeof((->void))
 	fp = (tos : (->void)#)
-	fp# = fn
+	fp# = std.fnbdup(fn, (env : byte#)[:envsz])
 
 	ret = sys.thr_new(&[
 		.startfn = (startthread : void#),
--- a/lib/thread/spawn+linux.myr
+++ b/lib/thread/spawn+linux.myr
@@ -27,7 +27,7 @@
 	if stk == sys.Mapbad
 		-> `std.Err "couldn't get stack"
 	;;
-        stk = initstack(stk, fn, Stacksz)
+	stk = initstack(stk, fn, Stacksz)
 
 	ret = sys.fnclone(Thrflag, \
 		(stk : byte#),\
@@ -41,16 +41,19 @@
 }
 
 const initstack = {stk, fn, sz
-	var tos, szp, fp
+	var tos, szp, fp, env, envsz
 
+	envsz = std.fnenvsz(fn)
 	tos = (stk : std.intptr)
 	tos -= sizeof(int64)
 	szp = (tos : sys.size#)
 	szp# = sz
-        tos -= sizeof((->void))
-        fp = (tos : (->void)#)
-        fp# = fn
-        -> (tos : byte#)
+	tos -= (envsz : std.intptr)
+	env = tos
+	tos -= sizeof((->void))
+	fp = (tos : (->void)#)
+	fp# = std.fnbdup(fn, (env : byte#)[:envsz])
+	-> (tos : byte#)
 }
 
 const getstk = {sz
--- a/lib/thread/spawn+openbsd.myr
+++ b/lib/thread/spawn+openbsd.myr
@@ -15,7 +15,7 @@
 }
 
 const spawnstk = {fn, sz
-	var stk, szp, fp, tos, tfp
+	var stk, szp, fp, tos, tfp, env, envsz
 	var ret
 
 	stk = getstk(sz)
@@ -29,9 +29,12 @@
 	szp# = Stacksz
 
 	/* store func */
+	envsz = std.fnenvsz(fn)
+	tos -= (envsz : std.intptr)
+	env = tos
 	tos -= sizeof((->void))
 	fp = (tos : (->void)#)
-	fp# = fn
+	fp# = std.fnbdup(fn, (env : byte#)[:envsz])
 
 	tfp = [
 		.tcb = (0 : void#),
--- a/lib/thread/spawn+osx.myr
+++ b/lib/thread/spawn+osx.myr
@@ -37,7 +37,6 @@
 	var tid : tid, ret
 
 
-	std.put("...hi? fn={}\n", (fn : void#))
 	ret = sys.bsdthread_create( \
 		(fn	: void#), \
 		envptr(&fn), \
--- a/lib/thread/spawn+plan9.myr
+++ b/lib/thread/spawn+plan9.myr
@@ -10,7 +10,9 @@
 const spawn = {fn
 	match sys.rfork(sys.Rfproc | sys.Rfmem | sys.Rfnowait)
 	| 0:
+		fn = std.fndup(fn)
 		fn()
+		std.fnfree(fn)
 		std.exit(0)
 	| -1:	-> `std.Err "unable to spawn thread"
 	| thr:	-> `std.Ok (thr : tid)