2020-05-18 15:59:07 +03:00
|
|
|
module System.Concurrency.Raw
|
|
|
|
|
|
|
|
-- At the moment this is pretty fundamentally tied to the Scheme RTS
|
|
|
|
-- Given that different back ends will have entirely different threading
|
|
|
|
-- models, it might be unavoidable, but we might want to think about possible
|
|
|
|
-- primitives that back ends should support.
|
|
|
|
|
|
|
|
%foreign "scheme:blodwen-thisthread"
|
|
|
|
prim__threadID : PrimIO ThreadID
|
|
|
|
%foreign "scheme:blodwen-set-thread-data"
|
|
|
|
prim__setThreadData : {a : Type} -> a -> PrimIO ()
|
|
|
|
%foreign "scheme:blodwen-get-thread-data"
|
|
|
|
prim__getThreadData : (a : Type) -> PrimIO a
|
|
|
|
|
|
|
|
-- Mutexes and condition variables.
|
|
|
|
|
|
|
|
export
|
|
|
|
threadID : IO ThreadID
|
|
|
|
threadID = primIO prim__threadID
|
|
|
|
|
|
|
|
export
|
|
|
|
setThreadData : {a : Type} -> a -> IO ()
|
|
|
|
setThreadData val = primIO (prim__setThreadData val)
|
|
|
|
|
|
|
|
export
|
|
|
|
getThreadData : (a : Type) -> IO a
|
|
|
|
getThreadData a = primIO (prim__getThreadData a)
|
|
|
|
|
|
|
|
export
|
|
|
|
data Mutex : Type where [external]
|
|
|
|
|
|
|
|
%foreign "scheme:blodwen-mutex"
|
|
|
|
prim__makeMutex : PrimIO Mutex
|
|
|
|
%foreign "scheme:blodwen-lock"
|
|
|
|
prim__mutexAcquire : Mutex -> PrimIO ()
|
|
|
|
%foreign "scheme:blodwen-unlock"
|
|
|
|
prim__mutexRelease : Mutex -> PrimIO ()
|
|
|
|
|
|
|
|
export
|
2021-01-11 14:24:43 +03:00
|
|
|
makeMutex : HasIO io => io Mutex
|
2020-05-18 15:59:07 +03:00
|
|
|
makeMutex = primIO prim__makeMutex
|
|
|
|
|
|
|
|
export
|
2021-01-11 14:24:43 +03:00
|
|
|
mutexAcquire : HasIO io => Mutex -> io ()
|
2020-05-18 15:59:07 +03:00
|
|
|
mutexAcquire m = primIO (prim__mutexAcquire m)
|
|
|
|
|
|
|
|
export
|
2021-01-11 14:24:43 +03:00
|
|
|
mutexRelease : HasIO io => Mutex -> io ()
|
2020-05-18 15:59:07 +03:00
|
|
|
mutexRelease m = primIO (prim__mutexRelease m)
|
|
|
|
|
|
|
|
export
|
|
|
|
data Condition : Type where [external]
|
|
|
|
|
|
|
|
%foreign "scheme:blodwen-condition"
|
|
|
|
prim__makeCondition : PrimIO Condition
|
|
|
|
%foreign "scheme:blodwen-condition-wait"
|
|
|
|
prim__conditionWait : Condition -> Mutex -> PrimIO ()
|
2020-06-23 21:03:56 +03:00
|
|
|
%foreign "scheme:blodwen-condition-wait-timeout"
|
2020-05-18 15:59:07 +03:00
|
|
|
prim__conditionWaitTimeout : Condition -> Mutex -> Int -> PrimIO ()
|
|
|
|
%foreign "scheme:blodwen-condition-signal"
|
|
|
|
prim__conditionSignal : Condition -> PrimIO ()
|
|
|
|
%foreign "scheme:blodwen-condition-broadcast"
|
|
|
|
prim__conditionBroadcast : Condition -> PrimIO ()
|
|
|
|
|
|
|
|
export
|
2021-01-11 14:24:43 +03:00
|
|
|
makeCondition : HasIO io => io Condition
|
2020-05-18 15:59:07 +03:00
|
|
|
makeCondition = primIO prim__makeCondition
|
|
|
|
|
|
|
|
export
|
2021-01-11 14:24:43 +03:00
|
|
|
conditionWait : HasIO io => Condition -> Mutex -> io ()
|
2020-05-18 15:59:07 +03:00
|
|
|
conditionWait c m = primIO (prim__conditionWait c m)
|
|
|
|
|
2020-06-23 21:03:56 +03:00
|
|
|
||| Timeout is in microseconds
|
2020-05-18 15:59:07 +03:00
|
|
|
export
|
2021-01-11 14:24:43 +03:00
|
|
|
conditionWaitTimeout : HasIO io => Condition -> Mutex -> Int -> io ()
|
2020-05-18 15:59:07 +03:00
|
|
|
conditionWaitTimeout c m t = primIO (prim__conditionWaitTimeout c m t)
|
|
|
|
|
|
|
|
export
|
2021-01-11 14:24:43 +03:00
|
|
|
conditionSignal : HasIO io => Condition -> io ()
|
2020-05-18 15:59:07 +03:00
|
|
|
conditionSignal c = primIO (prim__conditionSignal c)
|
|
|
|
|
|
|
|
export
|
2021-01-11 14:24:43 +03:00
|
|
|
conditionBroadcast : HasIO io => Condition -> io ()
|
2020-05-18 15:59:07 +03:00
|
|
|
conditionBroadcast c = primIO (prim__conditionBroadcast c)
|