-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy paththreads.af
More file actions
74 lines (60 loc) · 1.99 KB
/
Copy paththreads.af
File metadata and controls
74 lines (60 loc) · 1.99 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use' lang.af
use' io.af
use' pthread.af
\ Passed to `pthread_spawn` by raw instruction address.
\ We have no hidden adapters between Forth and libc.
fun: callback { inout -- Err }
inout @ { path }
" [child] checking length of `%s`..." path .logf .lf
alloca' Fstat { stat }
path stat .path_stat { err }
ifz err .then
\ Store the output back.
stat .Fstat_size @ inout !
end
\ `main` will check this.
err
end
fun: main
instr' callback { instr } \ Raw instruction address.
" missing/none" { path0 } \ Input for `thread0`: missing file.
" forth/lang.af" { path1 } \ Input for `thread1`: existing file.
path0 >slot { val0 } \ Input-output for `thread0`.
path1 >slot { val1 } \ Input-output for `thread1`.
" [main] spawning thread 0..." .log .lf
instr val0 .thread_spawn { thread0 err0 }
" [main] spawning thread 1..." .log .lf
instr val1 .thread_spawn { thread1 err1 }
if err0 .then
" [main] unable to spawn thread 0: %s" err0 .logf .lf
end
if err1 .then
" [main] unable to spawn thread 1: %s" err1 .logf .lf
end
\ After spawning a thread, we must either join or detach it.
\ Since this word handles thread creation errors explicitly,
\ it won't terminate until done with both thread handles.
ifz err0 .then
" [main] waiting on thread 0..." .log .lf
thread0 .thread_join { err0 join_err }
if join_err .then
" [main] when joining with thread 0: %s" join_err .logf .lf
elif err0 .then
" [main] error from thread 0: %s" err0 .logf .lf
else
" [main] result from thread 0: length of `%s`: %zd" path0 val0 @ .logf .lf
end
end
ifz err1 .then
" [main] waiting on thread 1..." .log .lf
thread1 .thread_join { err1 join_err }
if join_err .then
" [main] when joining with thread 1: %s" join_err .logf .lf
elif err1 .then
" [main] error from thread 1: %s" err1 .logf .lf
else
" [main] result from thread 1: length of `%s`: %zd" path1 val1 @ .logf .lf
end
end
end
.main