rev: 3827f53bae88e48a594610914ba74b9512ead6dd tukan/testing/test_threading.sc -rw-r--r-- 930 bytes View raw Log this file
3827f53bae88 — Leonard Ritter * more work on module system 3 months ago
                                                                                
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
using import ..tukan.thread
using import Capture

let Threadx64 = (array Thread 64)

run-stage;

global tls = (LocalStorage 0)
print tls (@ tls)
assert (tls == 0)
tls = 303
assert (tls == 303)
print "TLS ID =" ('id tls)
print tls

# a mutable counter
local x : i32
let x = (& x)
let N = 64
local threads = (nullof Threadx64)
for i in (range N)
    threads @ i =
        Thread
            capture [i x] ()
                assert (tls == 0)
                tls = 42
                print "thread #" i (active-thread-id)
                # TODO: fix references going through captures
                (@ x) += 1
                assert (tls == 42)
                123
# wait for all threads to finish
for i in (range N)
    'wait (threads @ i)
assert ((@ x) == N)

local counter : Atomic
'lock counter
assert (counter.value == 1)
'unlock counter
assert (counter.value == 0)
do
    let mut = (Mutex)
    'lock mut
    'unlock mut

true