Fix docs, and add a defaultSpawn function for ConcCVar monads

This commit is contained in:
Michael Walker 2015-01-21 11:26:27 +00:00
parent aa921a0423
commit 8aa15b5496
3 changed files with 16 additions and 19 deletions

View File

@ -22,7 +22,7 @@ class Monad m => ConcFuture future m | m -> future where
--
-- For monads which also implement 'ConcCVar', it is expected to
-- implement 'spawn' in terms of 'newEmptyCVar', 'fork', and
-- 'putCVar'.
-- 'putCVar'. The 'defaultSpawn' function provides this.
--
-- > spawn ma = do
-- > cvar <- newEmptyCVar
@ -31,16 +31,12 @@ class Monad m => ConcFuture future m | m -> future where
spawn :: m a -> m (future a)
-- | Block until a value is present in the future, and then return
-- it. This does not \"remove\" the value from the future, multiple
-- 'get's are possible, unlike 'takeMVar' for example.
-- it. As with 'readMVar', this does not \"remove\" the value from
-- the future, multiple reads are possible.
readCVar :: future a -> m a
instance ConcFuture MVar IO where
spawn ma = do
cvar <- newEmptyCVar
fork $ ma >>= putCVar cvar
return cvar
spawn = defaultSpawn
readCVar = readMVar
-- | @ConcCVar@ builds on futures by allowing @CVar@s which threads
@ -63,7 +59,7 @@ class ConcFuture cvar m => ConcCVar cvar m | m -> cvar where
newEmptyCVar :: m (cvar a)
-- | Put a value into a @CVar@. If there is already a value there,
-- this will block until that value has been 'take'n, at which point
-- this will block until that value has been taken, at which point
-- the value will be stored.
--
-- > putCVar cvar a = tryPutCVar cvar a >>= \b -> unless b $ putCVar cvar a
@ -76,8 +72,8 @@ class ConcFuture cvar m => ConcCVar cvar m | m -> cvar where
tryPutCVar :: cvar a -> a -> m Bool
-- | Take a value from a @CVar@. This \"empties\" the @CVar@,
-- allowing a new value to be 'put' in. This will block if there is
-- no value in the @CVar@ already, until one has been 'put'.
-- allowing a new value to be put in. This will block if there is no
-- value in the @CVar@ already, until one has been put.
--
-- > takeCVar cvar = tryTakeCVar cvar >>= maybe (takeCVar cvar) return
takeCVar :: cvar a -> m a
@ -95,3 +91,10 @@ instance ConcCVar MVar IO where
tryPutCVar = tryPutMVar
takeCVar = takeMVar
tryTakeCVar = tryTakeMVar
-- | A default implementation of 'spawn' for 'ConcCVar' monads.
defaultSpawn :: ConcCVar cvar m => m a -> m (cvar a)
defaultSpawn ma = do
cvar <- newEmptyCVar
fork $ ma >>= putCVar cvar
return cvar

View File

@ -85,10 +85,7 @@ liftIO ma = C $ cont lifted where
-- | Run the provided computation concurrently, returning the result.
spawn :: Conc t a -> Conc t (CVar t a)
spawn ma = do
cvar <- newEmptyCVar
fork $ ma >>= putCVar cvar
return cvar
spawn = C.defaultSpawn
-- | Block on a 'CVar' until it is full, then read from it (without
-- emptying).

View File

@ -81,10 +81,7 @@ liftST ma = C $ cont lifted where
-- | Run the provided computation concurrently, returning the result.
spawn :: Conc t a -> Conc t (CVar t a)
spawn ma = do
cvar <- newEmptyCVar
fork $ ma >>= putCVar cvar
return cvar
spawn = C.defaultSpawn
-- | Block on a 'CVar' until it is full, then read from it (without
-- emptying).