6 | module System.Concurrency
15 | %foreign "scheme:blodwen-set-thread-data"
16 | prim__setThreadData : {a : Type} -> a -> PrimIO ()
17 | %foreign "scheme:blodwen-get-thread-data"
18 | prim__getThreadData : (a : Type) -> PrimIO a
19 | %foreign "scheme,chez:get-thread-id"
20 | prim__getThreadId : PrimIO Int
25 | setThreadData : HasIO io => {a : Type} -> a -> io ()
26 | setThreadData val = primIO (prim__setThreadData val)
31 | getThreadData : HasIO io => (a : Type) -> io a
32 | getThreadData a = primIO (prim__getThreadData a)
36 | getThreadId : HasIO io => io Int
37 | getThreadId = primIO prim__getThreadId
43 | data Mutex : Type where [external]
45 | %foreign "scheme:blodwen-make-mutex"
46 | prim__makeMutex : PrimIO Mutex
47 | %foreign "scheme:blodwen-mutex-acquire"
48 | prim__mutexAcquire : Mutex -> PrimIO ()
49 | %foreign "scheme:blodwen-mutex-release"
50 | prim__mutexRelease : Mutex -> PrimIO ()
54 | makeMutex : HasIO io => io Mutex
55 | makeMutex = primIO prim__makeMutex
65 | mutexAcquire : HasIO io => Mutex -> io ()
66 | mutexAcquire m = primIO (prim__mutexAcquire m)
71 | mutexRelease : HasIO io => Mutex -> io ()
72 | mutexRelease m = primIO (prim__mutexRelease m)
78 | data Condition : Type where [external]
80 | %foreign "scheme,racket:blodwen-make-cv"
81 | "scheme,chez:blodwen-make-condition"
82 | prim__makeCondition : PrimIO Condition
83 | %foreign "scheme,racket:blodwen-cv-wait"
84 | "scheme,chez:blodwen-condition-wait"
85 | prim__conditionWait : Condition -> Mutex -> PrimIO ()
86 | %foreign "scheme,chez:blodwen-condition-wait-timeout"
88 | prim__conditionWaitTimeout : Condition -> Mutex -> Int -> PrimIO ()
89 | %foreign "scheme,racket:blodwen-cv-signal"
90 | "scheme,chez:blodwen-condition-signal"
91 | prim__conditionSignal : Condition -> PrimIO ()
92 | %foreign "scheme,racket:blodwen-cv-broadcast"
93 | "scheme,chez:blodwen-condition-broadcast"
94 | prim__conditionBroadcast : Condition -> PrimIO ()
99 | makeCondition : HasIO io => io Condition
100 | makeCondition = primIO prim__makeCondition
109 | conditionWait : HasIO io => Condition -> Mutex -> io ()
110 | conditionWait cond mutex = primIO (prim__conditionWait cond mutex)
116 | conditionWaitTimeout : HasIO io => Condition -> Mutex -> Int -> io ()
117 | conditionWaitTimeout cond mutex timeout = primIO (prim__conditionWaitTimeout cond mutex timeout)
121 | conditionSignal : HasIO io => Condition -> io ()
122 | conditionSignal c = primIO (prim__conditionSignal c)
126 | conditionBroadcast : HasIO io => Condition -> io ()
127 | conditionBroadcast c = primIO (prim__conditionBroadcast c)
133 | data Semaphore : Type where [external]
135 | %foreign "scheme:blodwen-make-semaphore"
136 | prim__makeSemaphore : Int -> PrimIO Semaphore
137 | %foreign "scheme:blodwen-semaphore-post"
138 | prim__semaphorePost : Semaphore -> PrimIO ()
139 | %foreign "scheme:blodwen-semaphore-wait"
140 | prim__semaphoreWait : Semaphore -> PrimIO ()
145 | makeSemaphore : HasIO io => Int -> io Semaphore
146 | makeSemaphore init = primIO (prim__makeSemaphore init)
150 | semaphorePost : HasIO io => Semaphore -> io ()
151 | semaphorePost sema = primIO (prim__semaphorePost sema)
156 | semaphoreWait : HasIO io => Semaphore -> io ()
157 | semaphoreWait sema = primIO (prim__semaphoreWait sema)
165 | data Barrier : Type where [external]
167 | %foreign "scheme:blodwen-make-barrier"
168 | prim__makeBarrier : Int -> PrimIO Barrier
169 | %foreign "scheme:blodwen-barrier-wait"
170 | prim__barrierWait : Barrier -> PrimIO ()
176 | makeBarrier : HasIO io => (numThreads : Int) -> io Barrier
177 | makeBarrier numThreads = primIO (prim__makeBarrier numThreads)
181 | barrierWait : HasIO io => Barrier -> io ()
182 | barrierWait barrier = primIO (prim__barrierWait barrier)
188 | data Channel : Type -> Type where [external]
190 | %foreign "scheme:blodwen-make-channel"
191 | prim__makeChannel : PrimIO (Channel a)
192 | %foreign "scheme:blodwen-channel-get"
193 | prim__channelGet : Channel a -> PrimIO a
194 | %foreign "scheme,chez:blodwen-channel-get-non-blocking"
195 | prim__channelGetNonBlocking : Channel a -> PrimIO (Maybe a)
196 | %foreign "scheme,chez:blodwen-channel-get-with-timeout"
197 | prim__channelGetWithTimeout : Channel a -> Int -> PrimIO (Maybe a)
198 | %foreign "scheme:blodwen-channel-put"
199 | prim__channelPut : Channel a -> a -> PrimIO ()
208 | makeChannel : HasIO io => io (Channel a)
209 | makeChannel = primIO prim__makeChannel
216 | channelGet : HasIO io => (chan : Channel a) -> io a
217 | channelGet chan = primIO (prim__channelGet chan)
224 | channelGetNonBlocking : HasIO io => (chan : Channel a) -> io (Maybe a)
225 | channelGetNonBlocking chan = primIO (prim__channelGetNonBlocking chan)
234 | channelGetWithTimeout : HasIO io => (chan : Channel a) -> (milliseconds : Nat) -> io (Maybe a)
235 | channelGetWithTimeout chan milliseconds =
238 | let clampedms = if milliseconds > 25 then cast milliseconds else 25
239 | in primIO (prim__channelGetWithTimeout chan clampedms)
241 | primIO (prim__channelGetWithTimeout chan (cast milliseconds))
248 | channelPut : HasIO io => (chan : Channel a) -> (val : a) -> io ()
249 | channelPut chan val = primIO (prim__channelPut chan val)