forked from miking-lang/miking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cond.mc
54 lines (42 loc) · 1.49 KB
/
cond.mc
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
-- Miking is licensed under the MIT license.
-- Copyright (C) David Broman. See file LICENSE.txt
--
-- Condition variables for thread synchronization.
include "thread.mc"
include "mutex.mc"
type Cond
-- 'condCreate ()' returns a new condition variable
external externalCondCreate ! : () -> Cond
let condCreate = lam.
externalCondCreate ()
-- 'condWait c m' releases the mutex 'm' and suspends the current thread until
-- condition variable 'c' is set
external externalCondWait ! : Cond -> Mutex -> ()
let condWait = lam c. lam m.
externalCondWait c m
-- 'condSignal c' signals the condition variable 'c', waking up ONE waiting
-- thread
external externalCondSignal ! : Cond -> ()
let condSignal = lam c.
externalCondSignal c
-- 'condBroadcast c' signals the condition variable 'c', waking up ALL waiting
-- threads.
external externalCondBroadcast ! : Cond -> ()
let condBroadcast = lam c.
externalCondBroadcast c
mexpr
utest
let c = condCreate () in
let m = mutexCreate () in
let t1 = threadSpawn (lam. mutexLock m; condWait c m; condSignal c; mutexRelease m) in
let t2 = threadSpawn (lam. mutexLock m; condSignal c; mutexRelease m) in
threadJoin t1;
threadJoin t2;
let t1 = threadSpawn (lam. mutexLock m; condWait c m; condSignal c; mutexRelease m) in
let t2 = threadSpawn (lam. mutexLock m; condWait c m; condSignal c; mutexRelease m) in
let t3 = threadSpawn (lam. mutexLock m; condBroadcast c; mutexRelease m) in
threadJoin t1;
threadJoin t2;
threadJoin t3;
()
with () in ()