summaryrefslogtreecommitdiff
path: root/lib/thread/test/tls.myr
blob: fa111111b9d16d38613e9b051276c9eb4407b071 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std
use sys
use thread

const Nelt = 100
const Nthr = 100

var elts : thread.tid[Nelt]
var start
var wg

const setget = {
	var tid = thread.tid()
	var localelts : thread.tid[Nelt]
	for var i = 0; i < Nelt; i++
		localelts[i] = elts[i] + tid
	;;

	var k = start
	for var i = 0; i < Nelt; i++
		thread.tlsset(k, &localelts[i])
		k++
	;;
	k = start
	for var i = 0; i < Nelt; i++
		std.assert(thread.tlsget(k)# == localelts[i], "tls is broken\n")
		k++
	;;
	thread.wgpost(&wg)
}

const main = {
	for var i = 0; i < Nelt; i++
		elts[i] = std.randnum()
	;;

	start = thread.tlsalloc()
	for var i = 1; i < Nelt; i++
		var k : thread.tlskey(thread.tid#) = thread.tlsalloc()
	;;

	wg = thread.mkwg(Nthr)
	for var i = 1; i < 100; i++
		thread.spawn(setget)
	;;
	setget()

	thread.wgwait(&wg)
}