summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOri Bernstein <ori@eigenstate.org>2017-04-09 02:09:15 -0700
committerOri Bernstein <ori@eigenstate.org>2017-04-09 02:23:39 -0700
commit4078e565e99dfb4abf07df16f0317c85a6bca3f1 (patch)
treee4e955cfb9007391ff2872d734b4bc2521c493dd
parent73518d58457d543258e344b96a8f4b13ccd16882 (diff)
downloadmc-4078e565e99dfb4abf07df16f0317c85a6bca3f1.tar.gz
Duplicate env on thread spawn.
-rw-r--r--lib/std/fndup.myr9
-rw-r--r--lib/thread/spawn+freebsd.myr7
-rw-r--r--lib/thread/spawn+linux.myr15
-rw-r--r--lib/thread/spawn+openbsd.myr7
-rw-r--r--lib/thread/spawn+osx.myr1
-rw-r--r--lib/thread/spawn+plan9.myr2
6 files changed, 28 insertions, 13 deletions
diff --git a/lib/std/fndup.myr b/lib/std/fndup.myr
index d8efddb..dce0837 100644
--- a/lib/std/fndup.myr
+++ b/lib/std/fndup.myr
@@ -27,10 +27,12 @@ generic fnenvsz = {fn
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 @@ const envslice = {ep : intptr
var env : byte#
var szp : intptr#
+ if ep == 0
+ -> [][:]
+ ;;
env = (ep : byte#)
szp = (env : intptr#)
-> env[:szp#]
diff --git a/lib/thread/spawn+freebsd.myr b/lib/thread/spawn+freebsd.myr
index 71ae87d..ebeb616 100644
--- a/lib/thread/spawn+freebsd.myr
+++ b/lib/thread/spawn+freebsd.myr
@@ -17,7 +17,7 @@ const spawn = {fn
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 @@ const spawnstk = {fn, sz
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#),
diff --git a/lib/thread/spawn+linux.myr b/lib/thread/spawn+linux.myr
index 54bac25..a56317f 100644
--- a/lib/thread/spawn+linux.myr
+++ b/lib/thread/spawn+linux.myr
@@ -27,7 +27,7 @@ const spawnstk = {fn, sz
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 spawnstk = {fn, sz
}
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
diff --git a/lib/thread/spawn+openbsd.myr b/lib/thread/spawn+openbsd.myr
index 0ccf759..1cdf230 100644
--- a/lib/thread/spawn+openbsd.myr
+++ b/lib/thread/spawn+openbsd.myr
@@ -15,7 +15,7 @@ const spawn = {fn;
}
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 @@ const spawnstk = {fn, sz
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#),
diff --git a/lib/thread/spawn+osx.myr b/lib/thread/spawn+osx.myr
index a301fb8..bedaa42 100644
--- a/lib/thread/spawn+osx.myr
+++ b/lib/thread/spawn+osx.myr
@@ -37,7 +37,6 @@ const spawnstk = {fn, sz
var tid : tid, ret
- std.put("...hi? fn={}\n", (fn : void#))
ret = sys.bsdthread_create( \
(fn : void#), \
envptr(&fn), \
diff --git a/lib/thread/spawn+plan9.myr b/lib/thread/spawn+plan9.myr
index bb23c2b..babcf02 100644
--- a/lib/thread/spawn+plan9.myr
+++ b/lib/thread/spawn+plan9.myr
@@ -10,7 +10,9 @@ pkg thread =
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)