shithub: mc

Download patch

ref: 205416ed364a55ee1792123c2fd73c949600aa50
parent: 183f3d40a1dbc2254f1e01de21eb022996f40ad2
author: Ori Bernstein <[email protected]>
date: Sat Nov 3 12:43:48 EDT 2018

Add tls hooks to libstd.

--- a/lib/std/threadhooks.myr
+++ b/lib/std/threadhooks.myr
@@ -1,6 +1,9 @@
 use "die"
 
 pkg std =
+	type thrdata = struct
+	;;
+
 	pkglocal var lock	: (l : void# -> void)
 	pkglocal var unlock	: (l : void# -> void)
 	pkglocal var memlck	: void#
@@ -12,7 +15,9 @@
 		net : void#, \
 		env : void#, \
 		lck : (l : void# -> void), \
-		unlck : (l : void# -> void) \
+		unlck : (l : void# -> void), \
+		tlsget : (-> thrdata#),  \
+		tlsset : (v : thrdata# -> void) \
 		-> void)
 ;;
 
@@ -19,11 +24,15 @@
 /* thread lock wrapper functions */
 var lock
 var unlock
+var tlsget
+var tlsset
 
 /* locks for various parts of libstd */
 var memlck
 var netlck
 var envlck
+var thrptr
+
 /*
 work around compiler bug: we don't generate
 syms for the funcs with an initializer
@@ -31,13 +40,17 @@
 const __init__ = {
 	lock = {l; }
 	unlock = {l; }
+	tlsget = {; -> thrptr}
+	tlsset = {v; thrptr = v}
 }
 
-const __lockinit = {mem, net, env, lck, unlck
+const __lockinit = {mem, net, env, lck, unlck, tlsget_, tlset_
 	memlck = mem
 	netlck = net
 	envlck = env
 	lock = lck
 	unlock = unlck
+	tlsget = tlsget_
+	tlsget = tlsget_
 }
 
--- a/lib/thread/hookstd.myr
+++ b/lib/thread/hookstd.myr
@@ -1,5 +1,6 @@
 use std
 use "mutex"
+use "tls"
 
 pkg thread =
 ;;
@@ -7,17 +8,21 @@
 var memlck
 var netlck
 var envlck
+var stdtls
 
 const __init__ = {
 	memlck = mkmtx()
 	netlck = mkmtx()
 	envlck = mkmtx()
+	stdtls = tlsalloc()
 	std.__lockinit( \
 		(&memlck : void#), \
 		(&netlck : void#), \
 		(&envlck : void#), \
 		{mtx; mtxlock((mtx : mutex#))}, \
-		{mtx; mtxunlock((mtx : mutex#))} \
+		{mtx; mtxunlock((mtx : mutex#))}, \
+		{; -> tlsget(stdtls)}, \
+		{val; tlsset(stdtls, val)} \
 	)
 }