-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.cicili
More file actions
48 lines (35 loc) · 2.3 KB
/
Copy paththread.cicili
File metadata and controls
48 lines (35 loc) · 2.3 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
(import "lib/std/pthread/pthread.cicili")
(source "thread.c" (:std #t :compile #t :link "-L{$CWD} -lthread.o -lpthread -o thread_main")
(include <unistd.h>)
(include <pthread.h>)
(main
(let ((int a_value . 50)
(auto id0 . #'(go ((int a_value)) ; optinal out
(format #t "a value from thread: %lu -> %d\n" (cast ulong (self)) a_value)))
(auto id1 . #'(go ((int input . 12)) (out void *) ; optinal out
(format #t "early exit thread: %lu -> %d\n" (cast ulong (self)) input)
(exit-self nil)
(format #t "not seen section: %lu -> %d\n" (cast ulong (self)) (+ input 8))
(return nil)))
(auto runner . '(lambda () (out pthread_t)
(return (go () (out void *) ; output counter
(let ((int counter . 0)
(auto allocated . #'(malloc (sizeof int))))
(while (< (1+ counter) 5)
(format #t "long running thread: %lu counter: %d\n"
(cast ulong (self)) counter)
(sleep 1))
(memcpy allocated (aof counter) (sizeof int))
(return allocated))))))
(auto id2 . #'(runner))
(auto id3 . #'(runner)))
(join id0)
(join id1)
(cancel id2)
(cast void
(detach ((int input . 25)) ; returns id if it needs
(format #t "detached int input from thread: %lu -> %d\n" (cast ulong (self)) input)))
(join id3 (int * output))
(defer* ((int * output)) (free output))
(format #t "output value: %d\n" (cast int (cof output)))
(return 0))))