aboutsummaryrefslogtreecommitdiff
path: root/examples/threads/threads.tm
blob: 99bb83b4326c03429c4932a84f16d994b9412554 (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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use <pthread.h>

struct Mutex(_mutex:@Memory):
    func new(->Mutex):
        return Mutex(
            inline C : @Memory {
                pthread_mutex_t *mutex = new(pthread_mutex_t);
                pthread_mutex_init(mutex, NULL);
                GC_register_finalizer(mutex, (void*)pthread_mutex_destroy, NULL, NULL, NULL);
                mutex
            }
        )

    func do_locked(m:Mutex, fn:func(); inline):
        inline C {
            pthread_mutex_lock((pthread_mutex_t*)_$m.$_mutex);
        }
        fn()
        inline C {
            pthread_mutex_unlock((pthread_mutex_t*)_$m.$_mutex);
        }

struct ThreadCondition(_cond:@Memory):
    func new(->ThreadCondition):
        return ThreadCondition(
            inline C : @Memory {
                pthread_cond_t *cond = new(pthread_cond_t);
                pthread_cond_init(cond, NULL);
                GC_register_finalizer(cond, (void*)pthread_cond_destroy, NULL, NULL, NULL);
                cond
            }
        )

    func wait(c:ThreadCondition, m:Mutex; inline):
        inline C {
            pthread_cond_wait((pthread_cond_t*)_$c.$_cond, (pthread_mutex_t*)_$m.$_mutex);
        }

    func signal(c:ThreadCondition; inline):
        inline C {
            pthread_cond_signal((pthread_cond_t*)_$c.$_cond);
        }

    func broadcast(c:ThreadCondition; inline):
        inline C {
            pthread_cond_broadcast((pthread_cond_t*)_$c.$_cond);
        }

struct Guard(mutex=Mutex.new(), cond=ThreadCondition.new()):
    func guarded(g:Guard, fn:func(); inline):
        g.mutex:do_locked(fn)
        g.cond:signal()

    func wait(g:Guard):
        g.cond:wait(g.mutex)

struct PThread(_thread:@Memory):
    func new(fn:func() -> PThread):
        return PThread(
            inline C : @Memory {
                pthread_t *thread = new(pthread_t);
                pthread_create(thread, NULL, _$fn.fn, _$fn.userdata);
                thread
            }
        )

    func join(t:PThread):
        inline C {
            pthread_join(*(pthread_t*)_$t.$_thread, NULL);
        }

    func cancel(t:PThread):
        inline C {
            pthread_cancel(*(pthread_t*)_$t.$_thread);
        }

    func detatch(t:PThread):
        inline C {
            pthread_detach(*(pthread_t*)_$t.$_thread);
        }

func main():
    g := Guard()
    queue := @[10, 20]

    t := PThread.new(func():
        say("In another thread!")
        item := @none:Int
        while item[] != 30:
            g:guarded(func():
                while queue.length == 0:
                    g:wait()

                item[] = queue[1]
                queue:remove_at(1)
            )
            say("Processing: $item")
            sleep(0.01)
        say("All done!")
    )
    >> t
    >> sleep(1)
    >> g:guarded(func():
        queue:insert(30)
    )
    >> t:join()