-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy paththread_test.cw
More file actions
49 lines (39 loc) · 1.57 KB
/
Copy paththread_test.cw
File metadata and controls
49 lines (39 loc) · 1.57 KB
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
module:
import test
import os
import fmt
global gThreads uint = 10
global gIterations uint = 10000
global gStackSize uint = 8 * 1024
global gCounters [gThreads]uint
ref global! gStacks [gThreads + 1][gStackSize]u8
fun thread_runner(no uint) void:
fmt\print#("hi from ", no, "\n")
return
fun run() uint:
fmt\print#("running with ", gThreads, " threads\n")
ref let req = {os\TimeSpec: 0, 100000000}
ref let! rem os\TimeSpec = undef
ref let! params os\CloneArgs
set params.flags = os\CLONE_VM | os\CLONE_FS | os\CLONE_FILES | os\CLONE_SIGHAND |
os\CLONE_THREAD | os\CLONE_SYSVSEM
set params.stack_size = gStackSize
fmt\print#("stack ", wrap_as(bitwise_as(params.stack, uint), fmt\uint_hex), "\n")
for i = 0, gThreads, 1:
fmt\print#("spawning ", i, "\n")
; do os\nanosleep(@req, @!rem)
set params.stack = front!(gStacks[i + 1])
; Note: here the stack parameter is the top of the stack
trylet pid u32 = os\CloneWrapper(thread_runner, params.stack, 0, i, params.flags), err:
fmt\print#("clone failed at ", i, " with ", unwrap(err), "\n")
continue
; Note: here the stack parameter is the bottom of the stack
; trylet pid u32 = os\Clone3Wrapper(thread_runner, i, @!params), err:
; fmt\print#("clone failed at ", i, " with ", unwrap(err), "\n")
; continue
do os\nanosleep(@req, @!rem)
return gIterations * gThreads
fun main(argc s32, argv ^^u8) s32:
test\AssertEq#(run(), gIterations * gThreads)
test\Success#()
return 0