2014-12-19 09:45:50 +03:00
|
|
|
{-# LANGUAGE ExistentialQuantification #-}
|
|
|
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
|
|
|
{-# LANGUAGE MultiParamTypeClasses #-}
|
2014-12-20 10:45:18 +03:00
|
|
|
{-# LANGUAGE RankNTypes #-}
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- | Concurrent monads with a fixed scheduler.
|
|
|
|
module Control.Monad.Conc.Fixed
|
|
|
|
( -- * The Conc Monad
|
|
|
|
Conc
|
2014-12-25 00:41:32 +03:00
|
|
|
, Trace
|
|
|
|
, ThreadAction(..)
|
2014-12-19 09:45:50 +03:00
|
|
|
, runConc
|
2014-12-19 10:12:22 +03:00
|
|
|
, runConc'
|
2014-12-19 09:59:41 +03:00
|
|
|
, liftIO
|
|
|
|
, spawn
|
|
|
|
, fork
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- * Communication: CVars
|
|
|
|
, CVar
|
2014-12-21 10:47:45 +03:00
|
|
|
, newEmptyCVar
|
|
|
|
, putCVar
|
2014-12-21 12:37:52 +03:00
|
|
|
, tryPutCVar
|
2014-12-21 10:47:45 +03:00
|
|
|
, readCVar
|
|
|
|
, takeCVar
|
|
|
|
, tryTakeCVar
|
2014-12-19 10:09:26 +03:00
|
|
|
|
|
|
|
-- * Scheduling
|
|
|
|
, Scheduler
|
2014-12-20 00:33:50 +03:00
|
|
|
, ThreadId
|
2014-12-19 10:09:26 +03:00
|
|
|
, randomSched
|
|
|
|
, randomSchedNP
|
2014-12-19 10:21:00 +03:00
|
|
|
, roundRobinSched
|
|
|
|
, roundRobinSchedNP
|
2014-12-19 09:45:50 +03:00
|
|
|
) where
|
|
|
|
|
|
|
|
import Control.Applicative (Applicative(..), (<$>))
|
2014-12-20 23:31:25 +03:00
|
|
|
import Control.Concurrent.MVar (MVar, newEmptyMVar, putMVar, takeMVar)
|
2014-12-19 09:45:50 +03:00
|
|
|
import Control.Monad.Cont (Cont, cont, runCont)
|
|
|
|
import Data.Map (Map)
|
2014-12-25 00:41:32 +03:00
|
|
|
import Data.Maybe (catMaybes, fromJust, isNothing)
|
2014-12-21 19:34:55 +03:00
|
|
|
import Data.IORef (IORef, newIORef, readIORef, writeIORef)
|
2014-12-19 10:09:26 +03:00
|
|
|
import System.Random (RandomGen, randomR)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-19 09:59:41 +03:00
|
|
|
import qualified Control.Monad.Conc.Class as C
|
|
|
|
import qualified Control.Monad.IO.Class as IO
|
2014-12-19 09:45:50 +03:00
|
|
|
import qualified Data.Map as M
|
|
|
|
|
|
|
|
-- | Scheduling is done in terms of a trace of 'Action's. Blocking can
|
|
|
|
-- only occur as a result of an action, and they cover (most of) the
|
|
|
|
-- primitives of the concurrency. `spawn` is absent as it can be
|
|
|
|
-- derived from `new`, `fork` and `put`.
|
|
|
|
data Action =
|
2014-12-25 00:41:32 +03:00
|
|
|
AFork Action Action
|
|
|
|
| forall t a. APut (CVar t a) a Action
|
|
|
|
| forall t a. ATryPut (CVar t a) a (Bool -> Action)
|
|
|
|
| forall t a. AGet (CVar t a) (a -> Action)
|
|
|
|
| forall t a. ATake (CVar t a) (a -> Action)
|
|
|
|
| forall t a. ATryTake (CVar t a) (Maybe a -> Action)
|
|
|
|
| ALift (IO Action)
|
|
|
|
| AStop
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- | The @Conc@ monad itself. Under the hood, this uses continuations
|
|
|
|
-- so it's able to interrupt and resume a monadic computation at any
|
|
|
|
-- point where a primitive is used.
|
2014-12-20 10:45:18 +03:00
|
|
|
--
|
|
|
|
-- This uses the same universally-quantified indexing state trick as
|
|
|
|
-- used by 'ST' and 'STRef's to prevent mutable references from
|
|
|
|
-- leaking out of the monad. See 'runConc' for an example of what this
|
|
|
|
-- means.
|
2014-12-23 18:51:46 +03:00
|
|
|
newtype Conc t a = C (Cont Action a) deriving (Functor, Applicative, Monad)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-20 10:45:18 +03:00
|
|
|
instance IO.MonadIO (Conc t) where
|
2014-12-19 09:59:41 +03:00
|
|
|
liftIO = liftIO
|
|
|
|
|
2014-12-20 10:45:18 +03:00
|
|
|
instance C.ConcFuture (CVar t) (Conc t) where
|
2014-12-21 10:47:45 +03:00
|
|
|
spawn = spawn
|
|
|
|
readCVar = readCVar
|
2014-12-19 09:59:41 +03:00
|
|
|
|
2014-12-20 10:45:18 +03:00
|
|
|
instance C.ConcCVar (CVar t) (Conc t) where
|
2014-12-21 10:47:45 +03:00
|
|
|
fork = fork
|
|
|
|
newEmptyCVar = newEmptyCVar
|
|
|
|
putCVar = putCVar
|
2014-12-21 12:37:52 +03:00
|
|
|
tryPutCVar = tryPutCVar
|
2014-12-21 10:47:45 +03:00
|
|
|
takeCVar = takeCVar
|
|
|
|
tryTakeCVar = tryTakeCVar
|
2014-12-19 09:59:41 +03:00
|
|
|
|
2014-12-19 09:45:50 +03:00
|
|
|
-- | The concurrent variable type used with the 'Conc'
|
|
|
|
-- monad. Internally, these are implemented as 'IORef's, but they are
|
|
|
|
-- structured to behave fairly similarly to 'MVar's. One notable
|
|
|
|
-- difference is that 'MVar's are single-wakeup, and wake up in a FIFO
|
2014-12-20 13:42:58 +03:00
|
|
|
-- order. Writing to a @CVar@ wakes up all threads blocked on reading
|
|
|
|
-- it, and it is up to the scheduler which one runs next. Taking from
|
|
|
|
-- a @CVar@ behaves analogously.
|
|
|
|
newtype CVar t a = V (IORef (Maybe a, [Block])) deriving Eq
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-19 09:59:41 +03:00
|
|
|
-- | Lift an 'IO' action into the 'Conc' monad.
|
|
|
|
--
|
|
|
|
-- Caution! Blocking on the action of another thread in @liftIO@
|
|
|
|
-- cannot be detected! So if you perform some potentially blocking
|
|
|
|
-- action in a 'liftIO' the entire collection of threads may deadlock!
|
|
|
|
-- You should therefore keep 'IO' blocks small, and only perform
|
|
|
|
-- blocking operations with the supplied primitives, insofar as
|
|
|
|
-- possible.
|
2014-12-20 10:45:18 +03:00
|
|
|
liftIO :: IO a -> Conc t a
|
2014-12-19 09:59:41 +03:00
|
|
|
liftIO ma = C $ cont lifted where
|
2014-12-25 00:41:32 +03:00
|
|
|
lifted c = ALift $ c <$> ma
|
2014-12-19 09:59:41 +03:00
|
|
|
|
|
|
|
-- | Run the provided computation concurrently, returning the result.
|
2014-12-20 10:45:18 +03:00
|
|
|
spawn :: Conc t a -> Conc t (CVar t a)
|
2014-12-19 09:59:41 +03:00
|
|
|
spawn ma = do
|
2014-12-21 10:47:45 +03:00
|
|
|
cvar <- newEmptyCVar
|
|
|
|
fork $ ma >>= putCVar cvar
|
2014-12-19 09:59:41 +03:00
|
|
|
return cvar
|
|
|
|
|
|
|
|
-- | Block on a 'CVar' until it is full, then read from it (without
|
|
|
|
-- emptying).
|
2014-12-21 10:47:45 +03:00
|
|
|
readCVar :: CVar t a -> Conc t a
|
2014-12-25 00:41:32 +03:00
|
|
|
readCVar cvar = C $ cont $ AGet cvar
|
2014-12-19 09:59:41 +03:00
|
|
|
|
|
|
|
-- | Run the provided computation concurrently.
|
2014-12-20 10:45:18 +03:00
|
|
|
fork :: Conc t () -> Conc t ()
|
2014-12-25 00:41:32 +03:00
|
|
|
fork (C ma) = C $ cont $ \c -> AFork (runCont ma $ const AStop) $ c ()
|
2014-12-19 09:59:41 +03:00
|
|
|
|
|
|
|
-- | Create a new empty 'CVar'.
|
2014-12-21 10:47:45 +03:00
|
|
|
newEmptyCVar :: Conc t (CVar t a)
|
|
|
|
newEmptyCVar = liftIO $ do
|
2014-12-20 13:42:58 +03:00
|
|
|
ioref <- newIORef (Nothing, [])
|
2014-12-19 09:59:41 +03:00
|
|
|
return $ V ioref
|
|
|
|
|
|
|
|
-- | Block on a 'CVar' until it is empty, then write to it.
|
2014-12-21 10:47:45 +03:00
|
|
|
putCVar :: CVar t a -> a -> Conc t ()
|
2014-12-25 00:41:32 +03:00
|
|
|
putCVar cvar a = C $ cont $ \c -> APut cvar a $ c ()
|
2014-12-19 09:59:41 +03:00
|
|
|
|
2014-12-21 12:37:52 +03:00
|
|
|
-- | Put a value into a 'CVar' if there isn't one, without blocking.
|
|
|
|
tryPutCVar :: CVar t a -> a -> Conc t Bool
|
2014-12-25 00:41:32 +03:00
|
|
|
tryPutCVar cvar a = C $ cont $ ATryPut cvar a
|
2014-12-21 12:37:52 +03:00
|
|
|
|
2014-12-19 09:59:41 +03:00
|
|
|
-- | Block on a 'CVar' until it is full, then read from it (with
|
|
|
|
-- emptying).
|
2014-12-21 10:47:45 +03:00
|
|
|
takeCVar :: CVar t a -> Conc t a
|
2014-12-25 00:41:32 +03:00
|
|
|
takeCVar cvar = C $ cont $ ATake cvar
|
2014-12-19 09:59:41 +03:00
|
|
|
|
|
|
|
-- | Read a value from a 'CVar' if there is one, without blocking.
|
2014-12-21 10:47:45 +03:00
|
|
|
tryTakeCVar :: CVar t a -> Conc t (Maybe a)
|
2014-12-25 00:41:32 +03:00
|
|
|
tryTakeCVar cvar = C $ cont $ ATryTake cvar
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- | Every thread has a unique identitifer. These are implemented as
|
|
|
|
-- integers, but you shouldn't assume they are necessarily contiguous.
|
|
|
|
type ThreadId = Int
|
|
|
|
|
2014-12-20 00:33:50 +03:00
|
|
|
-- | A @Scheduler@ maintains some internal state, `s`, takes the
|
|
|
|
-- 'ThreadId' of the last thread scheduled, and the list of runnable
|
|
|
|
-- threads (which will never be empty). It produces a 'ThreadId' to
|
|
|
|
-- schedule, and a new state.
|
2014-12-19 09:45:50 +03:00
|
|
|
--
|
2014-12-20 00:33:50 +03:00
|
|
|
-- Note: In order to prevent deadlock, the 'Conc' runtime will assume
|
|
|
|
-- that a deadlock situation has arisen if the scheduler attempts to
|
|
|
|
-- (a) schedule a blocked thread, or (b) schedule a nonexistant
|
|
|
|
-- thread. In either of those cases, the computation will be halted.
|
2014-12-19 09:45:50 +03:00
|
|
|
type Scheduler s = s -> ThreadId -> [ThreadId] -> (ThreadId, s)
|
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
-- | One of the outputs of the runner is a @Trace@, which is just a
|
|
|
|
-- log of threads and actions they have taken.
|
|
|
|
type Trace = [(ThreadId, ThreadAction)]
|
|
|
|
|
|
|
|
-- | All the actions that a thread can perform.
|
|
|
|
data ThreadAction =
|
|
|
|
Fork ThreadId
|
|
|
|
-- ^ Start a new thread.
|
|
|
|
| Put [ThreadId]
|
|
|
|
-- ^ Put into a 'CVar', possibly waking up some threads.
|
|
|
|
| BlockedPut
|
|
|
|
-- ^ Get blocked on a put.
|
|
|
|
| TryPut Bool [ThreadId]
|
|
|
|
-- ^ Try to put into a 'CVar', possibly waking up some threads.
|
|
|
|
| Read
|
|
|
|
-- ^ Read from a 'CVar'.
|
|
|
|
| BlockedRead
|
|
|
|
-- ^ Get blocked on a read.
|
|
|
|
| Take [ThreadId]
|
|
|
|
-- ^ Take from a 'CVar', possibly waking up some threads.
|
|
|
|
| BlockedTake
|
|
|
|
-- ^ Get blocked on a take.
|
|
|
|
| TryTake Bool [ThreadId]
|
|
|
|
-- ^ try to take from a 'CVar', possibly waking up some threads.
|
|
|
|
| IO
|
|
|
|
-- ^ Perform some IO.
|
|
|
|
deriving (Eq, Show)
|
|
|
|
|
2014-12-19 09:45:50 +03:00
|
|
|
-- | Run a concurrent computation with a given 'Scheduler' and initial
|
|
|
|
-- state, returning `Just result` if it terminates, and `Nothing` if a
|
|
|
|
-- deadlock is detected.
|
2014-12-20 10:45:18 +03:00
|
|
|
--
|
|
|
|
-- Note how the `t` in 'Conc' is universally quantified, what this
|
|
|
|
-- means in practice is that you can't do something like this:
|
|
|
|
--
|
|
|
|
-- > runConc (\s _ (x:_) -> (x, s)) () $ new >>= return
|
|
|
|
--
|
|
|
|
-- So 'CVar's cannot leak out of the 'Conc' computation. If this is
|
|
|
|
-- making your head hurt, check out the \"How `runST` works\" section
|
|
|
|
-- of <https://ocharles.org.uk/blog/guest-posts/2014-12-18-rank-n-types.html>
|
|
|
|
runConc :: Scheduler s -> s -> (forall t. Conc t a) -> IO (Maybe a)
|
2014-12-25 00:41:32 +03:00
|
|
|
runConc sched s ma = (\(a,_,_) -> a) <$> runConc' sched s ma
|
2014-12-19 10:12:22 +03:00
|
|
|
|
|
|
|
-- | variant of 'runConc' which returns the final state of the
|
2014-12-25 00:41:32 +03:00
|
|
|
-- scheduler and an execution trace.
|
|
|
|
runConc' :: Scheduler s -> s -> (forall t. Conc t a) -> IO (Maybe a, s, Trace)
|
2014-12-19 10:12:22 +03:00
|
|
|
runConc' sched s ma = do
|
2014-12-19 09:45:50 +03:00
|
|
|
mvar <- newEmptyMVar
|
|
|
|
let (C c) = ma >>= liftIO . putMVar mvar . Just
|
2014-12-25 00:41:32 +03:00
|
|
|
(s', trace) <- runThreads [] (negate 1) sched s (M.fromList [(0, (runCont c $ const AStop, False))]) mvar
|
2014-12-19 10:12:22 +03:00
|
|
|
out <- takeMVar mvar
|
2014-12-25 00:41:32 +03:00
|
|
|
return (out, s', reverse trace)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-19 10:09:26 +03:00
|
|
|
-- | A simple random scheduler which, at every step, picks a random
|
|
|
|
-- thread to run.
|
|
|
|
randomSched :: RandomGen g => Scheduler g
|
|
|
|
randomSched g _ threads = (threads !! choice, g') where
|
2014-12-20 12:20:48 +03:00
|
|
|
(choice, g') = randomR (0, length threads - 1) g
|
2014-12-19 10:09:26 +03:00
|
|
|
|
|
|
|
-- | A random scheduler which doesn't pre-empt the running
|
|
|
|
-- thread. That is, if the last thread scheduled is still runnable,
|
|
|
|
-- run that, otherwise schedule randomly.
|
|
|
|
randomSchedNP :: RandomGen g => Scheduler g
|
2014-12-19 10:21:00 +03:00
|
|
|
randomSchedNP = makeNP randomSched
|
|
|
|
|
|
|
|
-- | A round-robin scheduler which, at every step, schedules the
|
2014-12-20 00:39:06 +03:00
|
|
|
-- thread with the next 'ThreadId'.
|
2014-12-19 10:21:00 +03:00
|
|
|
roundRobinSched :: Scheduler ()
|
2014-12-21 19:34:55 +03:00
|
|
|
roundRobinSched _ prior threads
|
|
|
|
| prior >= maximum threads = (minimum threads, ())
|
|
|
|
| otherwise = (minimum $ filter (>prior) threads, ())
|
2014-12-19 10:21:00 +03:00
|
|
|
|
|
|
|
-- | A round-robin scheduler which doesn't pre-empt the running
|
|
|
|
-- thread.
|
|
|
|
roundRobinSchedNP :: Scheduler ()
|
|
|
|
roundRobinSchedNP = makeNP roundRobinSched
|
|
|
|
|
|
|
|
-- | Turn a potentially pre-emptive scheduler into a non-preemptive
|
|
|
|
-- one.
|
|
|
|
makeNP :: Scheduler s -> Scheduler s
|
|
|
|
makeNP sched = newsched where
|
2014-12-21 19:34:55 +03:00
|
|
|
newsched s prior threads
|
|
|
|
| prior `elem` threads = (prior, s)
|
|
|
|
| otherwise = sched s prior threads
|
2014-12-19 10:09:26 +03:00
|
|
|
|
2014-12-19 09:45:50 +03:00
|
|
|
-------------------- Internal stuff --------------------
|
|
|
|
|
|
|
|
-- | A @Block@ is used to determine what sort of block a thread is
|
|
|
|
-- experiencing.
|
2014-12-20 13:42:58 +03:00
|
|
|
data Block = WaitFull ThreadId | WaitEmpty ThreadId deriving Eq
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- | Run a collection of threads, until there are no threads left.
|
2014-12-20 13:42:58 +03:00
|
|
|
--
|
|
|
|
-- A thread is represented as a tuple of (next action, is blocked).
|
2014-12-25 00:41:32 +03:00
|
|
|
--
|
|
|
|
-- Note: this returns the trace in reverse order, because it's more
|
|
|
|
-- efficient to prepend to a list than append. As this function isn't
|
|
|
|
-- exposed to users of the library, this is just an internal gotcha to
|
|
|
|
-- watch out for.
|
|
|
|
runThreads :: Trace -> ThreadId -> Scheduler s -> s -> Map ThreadId (Action, Bool) -> MVar (Maybe a) -> IO (s, Trace)
|
|
|
|
runThreads sofar prior sched s threads mvar
|
|
|
|
| isTerminated = return (s, sofar)
|
|
|
|
| isDeadlocked = putMVar mvar Nothing >> return (s, sofar)
|
|
|
|
| isBlocked = putStrLn "Attempted to run a blocked thread, assuming deadlock." >> putMVar mvar Nothing >> return (s, sofar)
|
|
|
|
| isNonexistant = putStrLn "Attempted to run a nonexistant thread, assuming deadlock." >> putMVar mvar Nothing >> return (s, sofar)
|
2014-12-19 09:45:50 +03:00
|
|
|
| otherwise = do
|
2014-12-25 00:41:32 +03:00
|
|
|
(threads', act) <- runThread (fst $ fromJust thread, chosen) threads
|
|
|
|
let sofar' = maybe sofar (\a -> (chosen, a) : sofar) act
|
|
|
|
runThreads sofar' chosen sched s' threads' mvar
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
where
|
2014-12-21 19:34:55 +03:00
|
|
|
(chosen, s') = if prior == -1 then (0, s) else sched s prior $ M.keys runnable
|
2014-12-20 23:31:25 +03:00
|
|
|
runnable = M.filter (not . snd) threads
|
|
|
|
thread = M.lookup chosen threads
|
|
|
|
isBlocked = snd . fromJust $ M.lookup chosen threads
|
|
|
|
isNonexistant = isNothing thread
|
|
|
|
isTerminated = 0 `notElem` M.keys threads
|
|
|
|
isDeadlocked = M.null runnable
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- | Run a single thread one step, by dispatching on the type of
|
|
|
|
-- 'Action'.
|
2014-12-25 00:41:32 +03:00
|
|
|
runThread :: (Action, ThreadId) -> Map ThreadId (Action, Bool) -> IO (Map ThreadId (Action, Bool), Maybe ThreadAction)
|
|
|
|
runThread (AFork a b, i) threads =
|
|
|
|
let (threads', newid) = launch a threads
|
|
|
|
in return (goto b i threads', Just $ Fork newid)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
runThread (APut v a c, i) threads = do
|
2014-12-19 09:45:50 +03:00
|
|
|
let (V ref) = v
|
2014-12-20 13:42:58 +03:00
|
|
|
(val, blocks) <- readIORef ref
|
2014-12-19 09:45:50 +03:00
|
|
|
case val of
|
2014-12-25 00:41:32 +03:00
|
|
|
Just _ -> do
|
|
|
|
threads' <- block v WaitEmpty i threads
|
|
|
|
return (threads', Just BlockedPut)
|
2014-12-19 09:45:50 +03:00
|
|
|
Nothing -> do
|
2014-12-20 13:42:58 +03:00
|
|
|
writeIORef ref (Just a, blocks)
|
2014-12-25 00:41:32 +03:00
|
|
|
(threads', woken) <- wake v WaitFull threads
|
|
|
|
return (goto c i threads', Just $ Put woken)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
runThread (ATryPut v a c, i) threads = do
|
2014-12-21 12:37:52 +03:00
|
|
|
let (V ref) = v
|
|
|
|
(val, blocks) <- readIORef ref
|
|
|
|
case val of
|
2014-12-25 00:41:32 +03:00
|
|
|
Just _ -> return (goto (c False) i threads, Just $ TryPut False [])
|
2014-12-21 12:37:52 +03:00
|
|
|
Nothing -> do
|
|
|
|
writeIORef ref (Just a, blocks)
|
2014-12-25 00:41:32 +03:00
|
|
|
(threads', woken) <- wake v WaitFull threads
|
|
|
|
return (goto (c True) i threads', Just $ TryPut True woken)
|
2014-12-21 12:37:52 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
runThread (AGet v c, i) threads = do
|
2014-12-19 09:45:50 +03:00
|
|
|
let (V ref) = v
|
2014-12-20 13:42:58 +03:00
|
|
|
(val, _) <- readIORef ref
|
2014-12-19 09:45:50 +03:00
|
|
|
case val of
|
2014-12-25 00:41:32 +03:00
|
|
|
Just val' -> return (goto (c val') i threads, Just Read)
|
|
|
|
Nothing -> do
|
|
|
|
threads' <- block v WaitFull i threads
|
|
|
|
return (threads', Just BlockedRead)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
runThread (ATake v c, i) threads = do
|
2014-12-19 09:45:50 +03:00
|
|
|
let (V ref) = v
|
2014-12-20 13:42:58 +03:00
|
|
|
(val, blocks) <- readIORef ref
|
2014-12-19 09:45:50 +03:00
|
|
|
case val of
|
|
|
|
Just val' -> do
|
2014-12-20 13:42:58 +03:00
|
|
|
writeIORef ref (Nothing, blocks)
|
2014-12-25 00:41:32 +03:00
|
|
|
(threads', woken) <- wake v WaitEmpty threads
|
|
|
|
return (goto (c val') i threads', Just $ Take woken)
|
|
|
|
Nothing -> do
|
|
|
|
threads' <- block v WaitFull i threads
|
|
|
|
return (threads', Just BlockedTake)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
runThread (ATryTake v c, i) threads = do
|
2014-12-19 09:45:50 +03:00
|
|
|
let (V ref) = v
|
2014-12-25 00:41:32 +03:00
|
|
|
(val, blocks) <- readIORef ref
|
|
|
|
case val of
|
|
|
|
Just _ -> do
|
|
|
|
writeIORef ref (Nothing, blocks)
|
|
|
|
(threads', woken) <- wake v WaitEmpty threads
|
|
|
|
return (goto (c val) i threads', Just $ TryTake True woken)
|
|
|
|
Nothing -> return (goto (c Nothing) i threads, Just $ TryTake False [])
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
runThread (ALift io, i) threads = do
|
2014-12-19 09:45:50 +03:00
|
|
|
a <- io
|
2014-12-25 00:41:32 +03:00
|
|
|
return (goto a i threads, Just IO)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
runThread (AStop, i) threads = return (kill i threads, Nothing)
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- | Replace the 'Action' of a thread.
|
|
|
|
goto :: Ord k => a -> k -> Map k (a, b) -> Map k (a, b)
|
|
|
|
goto a = M.alter $ \(Just (_, b)) -> Just (a, b)
|
|
|
|
|
2014-12-20 13:42:58 +03:00
|
|
|
-- | Block a thread on a 'CVar'.
|
|
|
|
block :: Ord k => CVar t v -> (k -> Block) -> k -> Map k (a, Bool) -> IO (Map k (a, Bool))
|
|
|
|
block (V ref) typ tid threads = do
|
|
|
|
(val, blocks) <- readIORef ref
|
|
|
|
writeIORef ref (val, typ tid : blocks)
|
|
|
|
return $ M.alter (\(Just (a, _)) -> Just (a, True)) tid threads
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- | Start a thread with the next free ID.
|
2014-12-25 00:41:32 +03:00
|
|
|
launch :: (Ord k, Enum k) => a -> Map k (a, Bool) -> (Map k (a, Bool), k)
|
|
|
|
launch a m = (M.insert k (a, False) m, k) where
|
|
|
|
k = succ . maximum $ M.keys m
|
2014-12-19 09:45:50 +03:00
|
|
|
|
|
|
|
-- | Kill a thread.
|
|
|
|
kill :: Ord k => k -> Map k (a, b) -> Map k (a, b)
|
|
|
|
kill = M.delete
|
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
-- | Wake every thread blocked on a 'CVar' read/write.
|
|
|
|
wake :: Ord k => CVar t v -> (k -> Block) -> Map k (a, Bool) -> IO (Map k (a, Bool), [k])
|
|
|
|
wake (V ref) typ m = do
|
|
|
|
(m', woken) <- unzip <$> mapM wake' (M.toList m)
|
|
|
|
|
|
|
|
return (M.fromList m', catMaybes woken)
|
|
|
|
|
|
|
|
where
|
|
|
|
wake' a@(tid, (act, True)) = do
|
|
|
|
let blck = typ tid
|
|
|
|
(val, blocks) <- readIORef ref
|
2014-12-20 13:42:58 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
if blck `elem` blocks
|
|
|
|
then writeIORef ref (val, filter (/= blck) blocks) >> return ((tid, (act, False)), Just tid)
|
|
|
|
else return (a, Nothing)
|
2014-12-20 13:42:58 +03:00
|
|
|
|
2014-12-25 00:41:32 +03:00
|
|
|
wake' a = return (a, Nothing)
|