shithub: mc

Download patch

ref: b4074b239d8dda2fccc500ee1467ed21987938e9
parent: 3821901ac0c5ef909818d887ffe3d4565ecd469d
author: Ori Bernstein <ori@eigenstate.org>
date: Mon Nov 5 18:30:56 EST 2018

TLS implementation for 9front.

--- a/lib/thread/hookstd.myr
+++ b/lib/thread/hookstd.myr
@@ -1,4 +1,5 @@
 use std
+use sys
 use "mutex"
 use "tls"
 
--- a/lib/thread/spawn+plan9.myr
+++ b/lib/thread/spawn+plan9.myr
@@ -1,5 +1,6 @@
 use std
 use sys
+use "tls"
 
 pkg thread =
 	type tid = uint64
@@ -8,11 +9,23 @@
 ;;
 
 const spawn = {fn
+	var p, s
+
 	fn = std.fndup(fn)
 	match sys.rfork(sys.Rfproc | sys.Rfmem)
 	| 0:
+		if ntlsslots > Staticcap
+			p = getpriv()
+			s = std.slalloc(ntlsslots)
+			p[0] = (s : void#)
+		;;
 		fn()
 		std.fnfree(fn)
+		if ntlsslots > Staticcap
+			p = getpriv()
+			s = (p[0] : void##)[:ntlsslots]
+			std.slfree(s)
+		;;
 		std.exit(0)
 	| -1:	
 		std.fnfree(fn)
--- /dev/null
+++ b/lib/thread/tls+plan9.myr
@@ -1,0 +1,73 @@
+use sys
+use std
+
+use "common"
+use "types"
+
+pkg thread =
+	generic tlsalloc	: (-> tlskey(@a#))
+	generic tlsset		: (k : tlskey(@a#), v : @a# -> void)
+	generic tlsget		: (k : tlskey(@a#) -> @a#)
+	const tid		: (-> tid)
+
+	pkglocal const getpriv	: (-> void#[:])
+
+	pkglocal var ntlsslots : std.size
+	pkglocal const Staticcap : std.size
+;;
+
+var ntlsslots
+const Staticcap = (sys.Npriv : std.size)
+
+const getpriv = {
+	var p
+
+	p = (sys.tosptr : std.size)
+	p -= sizeof(sys.tos)
+	-> (p : void##)[:sys.Npriv]
+}
+	
+
+generic tlsalloc = { -> tlskey(@a#)
+	var s, p
+
+	p = getpriv()
+	if ntlsslots == Staticcap
+		s = std.sldup(p[:sys.Npriv])
+		p[0] = (s : void#)
+	elif ntlsslots > Staticcap
+		s = (p[0] : void##)[:ntlsslots]
+		std.slpush(&s, Zptr)
+		p[0] = (s : void#)
+	;;
+	ntlsslots++
+	-> (ntlsslots - 1 : tlskey(@a#))
+}
+
+generic tlsset = {k : tlskey(@a#), v : @a#
+	var s, p
+
+	s = getpriv()
+	if ntlsslots < 8
+		s[k] = (v : void#)
+	else
+		p = (s[0] : void##)[:ntlsslots]
+		p[k] = (v : void#)
+	;;
+}
+
+generic tlsget = {k
+	var s, p
+
+	p = getpriv()
+	if ntlsslots < 8
+		-> (s[k] : @a#)
+	else
+		s = (p[0] : void##)[:ntlsslots]
+		-> (s[k] : @a#)
+	;;	
+}
+
+const tid = {
+	-> (sys.tosptr.pid : tid)
+}
--- /dev/null
+++ b/lib/thread/types+plan9.myr
@@ -1,0 +1,6 @@
+use sys
+
+pkg thread =
+	type tid        = uint32
+	type tlskey(@a) = uint32
+;;