Safe Haskell | None |
---|---|

Language | Haskell2010 |

Basic concurrency primitives.

To compile the C code resulting from `compile`

for programs with concurrency primitives, use something like

cc -std=c99 -Iinclude csrc/chan.c -lpthread YOURPROGRAM.c

## Synopsis

- data ThreadId
- data Chan t a
- data ChanSize exp pred i where
- data ThreadCMD fs a
- data ChanCMD fs a
- data Closeable
- data Uncloseable
- fork :: ThreadCMD :<: instr => ProgramT instr (Param2 exp pred) m () -> ProgramT instr (Param2 exp pred) m ThreadId
- forkWithId :: ThreadCMD :<: instr => (ThreadId -> ProgramT instr (Param2 exp pred) m ()) -> ProgramT instr (Param2 exp pred) m ThreadId
- asyncKillThread :: ThreadCMD :<: instr => ThreadId -> ProgramT instr (Param2 exp pred) m ()
- killThread :: (ThreadCMD :<: instr, Monad m) => ThreadId -> ProgramT instr (Param2 exp pred) m ()
- waitThread :: ThreadCMD :<: instr => ThreadId -> ProgramT instr (Param2 exp pred) m ()
- delayThread :: (Integral i, ThreadCMD :<: instr) => exp i -> ProgramT instr (Param2 exp pred) m ()
- timesSizeOf :: (pred a, Integral i) => exp i -> proxy a -> ChanSize exp pred i
- timesSize :: Integral i => exp i -> ChanSize exp pred i -> ChanSize exp pred i
- plusSize :: Integral i => ChanSize exp pred i -> ChanSize exp pred i -> ChanSize exp pred i
- newChan :: forall a i exp pred instr m. (pred a, Integral i, ChanCMD :<: instr) => exp i -> ProgramT instr (Param2 exp pred) m (Chan Uncloseable a)
- newCloseableChan :: forall a i exp pred instr m. (pred a, Integral i, ChanCMD :<: instr) => exp i -> ProgramT instr (Param2 exp pred) m (Chan Closeable a)
- readChan :: (Typeable a, pred a, FreeExp exp, FreePred exp a, ChanCMD :<: instr, Monad m) => Chan t a -> ProgramT instr (Param2 exp pred) m (exp a)
- writeChan :: (Typeable a, pred a, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan t a -> exp a -> ProgramT instr (Param2 exp pred) m (exp Bool)
- readChanBuf :: (Typeable a, pred a, Ix i, Integral i, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan t a -> exp i -> exp i -> Arr i a -> ProgramT instr (Param2 exp pred) m (exp Bool)
- writeChanBuf :: (Typeable a, pred a, Ix i, Integral i, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan t a -> exp i -> exp i -> Arr i a -> ProgramT instr (Param2 exp pred) m (exp Bool)
- closeChan :: ChanCMD :<: instr => Chan Closeable c -> ProgramT instr (Param2 exp pred) m ()
- lastChanReadOK :: (FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan Closeable c -> ProgramT instr (Param2 exp pred) m (exp Bool)
- newChan' :: (Integral i, ChanCMD :<: instr) => ChanSize exp pred i -> ProgramT instr (Param2 exp pred) m (Chan Uncloseable a)
- newCloseableChan' :: (Integral i, ChanCMD :<: instr) => ChanSize exp pred i -> ProgramT instr (Param2 exp pred) m (Chan Closeable a)
- readChan' :: (Typeable a, pred a, FreeExp exp, FreePred exp a, ChanCMD :<: instr, Monad m) => Chan t c -> ProgramT instr (Param2 exp pred) m (exp a)
- writeChan' :: (Typeable a, pred a, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan t c -> exp a -> ProgramT instr (Param2 exp pred) m (exp Bool)
- readChanBuf' :: (Typeable a, pred a, Ix i, Integral i, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan t c -> exp i -> exp i -> Arr i a -> ProgramT instr (Param2 exp pred) m (exp Bool)
- writeChanBuf' :: (Typeable a, pred a, Ix i, Integral i, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan t c -> exp i -> exp i -> Arr i a -> ProgramT instr (Param2 exp pred) m (exp Bool)

# Documentation

A bounded channel.

data ChanSize exp pred i where Source #

Channel size specification. For each possible element type, it shows how many elements of them could be stored in the given channel at once.

## Instances

(ThreadCMD :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) :<: instr => Reexpressible (ThreadCMD :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) (instr :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) env Source # | |

InterpBi (ThreadCMD :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) IO (Param1 pred :: (k2, Type)) Source # | |

CompExp exp => Interp (ThreadCMD :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) CGen (Param2 exp pred :: (Type -> Type, (k2, Type))) Source # | |

HFunctor (ThreadCMD :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) Source # | |

Defined in Language.Embedded.Concurrent.CMD | |

HBifunctor (ThreadCMD :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) Source # | |

DryInterp (ThreadCMD :: (Type -> Type, (Type -> Type, (k2, Type))) -> Type -> Type) Source # | |

Defined in Language.Embedded.Concurrent.CMD dryInterp :: MonadSupply m => ThreadCMD (m, fs) a -> m a Source # |

## Instances

data Uncloseable Source #

fork :: ThreadCMD :<: instr => ProgramT instr (Param2 exp pred) m () -> ProgramT instr (Param2 exp pred) m ThreadId Source #

Fork off a computation as a new thread.

forkWithId :: ThreadCMD :<: instr => (ThreadId -> ProgramT instr (Param2 exp pred) m ()) -> ProgramT instr (Param2 exp pred) m ThreadId Source #

Fork off a computation as a new thread, with access to its own thread ID.

asyncKillThread :: ThreadCMD :<: instr => ThreadId -> ProgramT instr (Param2 exp pred) m () Source #

Forcibly terminate a thread, then continue execution immediately.

killThread :: (ThreadCMD :<: instr, Monad m) => ThreadId -> ProgramT instr (Param2 exp pred) m () Source #

Forcibly terminate a thread. Blocks until the thread is actually dead.

waitThread :: ThreadCMD :<: instr => ThreadId -> ProgramT instr (Param2 exp pred) m () Source #

Wait for a thread to terminate.

delayThread :: (Integral i, ThreadCMD :<: instr) => exp i -> ProgramT instr (Param2 exp pred) m () Source #

Sleep for a given amount of microseconds. Implemented with `usleep`

.
A C compiler might require a feature test macro to be defined,
otherwise it emits a warning about an implicitly declared function.
For more details, see: http://man7.org/linux/man-pages/man3/usleep.3.html

timesSizeOf :: (pred a, Integral i) => exp i -> proxy a -> ChanSize exp pred i Source #

Takes `n`

times the size of type refered by proxy.

timesSize :: Integral i => exp i -> ChanSize exp pred i -> ChanSize exp pred i Source #

Multiplies a channel size specification with a scalar.

plusSize :: Integral i => ChanSize exp pred i -> ChanSize exp pred i -> ChanSize exp pred i Source #

Adds two channel size specifications together.

newChan :: forall a i exp pred instr m. (pred a, Integral i, ChanCMD :<: instr) => exp i -> ProgramT instr (Param2 exp pred) m (Chan Uncloseable a) Source #

Create a new channel. Writing a reference type to a channel will copy contents into the channel, so modifying it post-write is completely safe.

newCloseableChan :: forall a i exp pred instr m. (pred a, Integral i, ChanCMD :<: instr) => exp i -> ProgramT instr (Param2 exp pred) m (Chan Closeable a) Source #

readChan :: (Typeable a, pred a, FreeExp exp, FreePred exp a, ChanCMD :<: instr, Monad m) => Chan t a -> ProgramT instr (Param2 exp pred) m (exp a) Source #

Read an element from a channel. If channel is empty, blocks until there
is an item available.
If `closeChan`

has been called on the channel *and* if the channel is
empty, `readChan`

returns an undefined value immediately.

writeChan :: (Typeable a, pred a, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan t a -> exp a -> ProgramT instr (Param2 exp pred) m (exp Bool) Source #

Write a data element to a channel.
If `closeChan`

has been called on the channel, all calls to `writeChan`

become non-blocking no-ops and return `False`

, otherwise returns `True`

.
If the channel is full, this function blocks until there's space in the
queue.

:: (Typeable a, pred a, Ix i, Integral i, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) | |

=> Chan t a | |

-> exp i | Offset in array to start writing |

-> exp i | Elements to read |

-> Arr i a | |

-> ProgramT instr (Param2 exp pred) m (exp Bool) |

Read an arbitrary number of elements from a channel into an array.
The semantics are the same as for `readChan`

, where "channel is empty"
is defined as "channel contains less data than requested".
Returns `False`

without reading any data if the channel is closed.

:: (Typeable a, pred a, Ix i, Integral i, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) | |

=> Chan t a | |

-> exp i | Offset in array to start reading |

-> exp i | Elements to write |

-> Arr i a | |

-> ProgramT instr (Param2 exp pred) m (exp Bool) |

Write an arbitrary number of elements from an array into an channel.
The semantics are the same as for `writeChan`

, where "channel is full"
is defined as "channel has insufficient free space to store all written
data".

closeChan :: ChanCMD :<: instr => Chan Closeable c -> ProgramT instr (Param2 exp pred) m () Source #

Close a channel. All subsequent write operations will be no-ops. After the channel is drained, all subsequent read operations will be no-ops as well.

lastChanReadOK :: (FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan Closeable c -> ProgramT instr (Param2 exp pred) m (exp Bool) Source #

newChan' :: (Integral i, ChanCMD :<: instr) => ChanSize exp pred i -> ProgramT instr (Param2 exp pred) m (Chan Uncloseable a) Source #

newCloseableChan' :: (Integral i, ChanCMD :<: instr) => ChanSize exp pred i -> ProgramT instr (Param2 exp pred) m (Chan Closeable a) Source #

readChan' :: (Typeable a, pred a, FreeExp exp, FreePred exp a, ChanCMD :<: instr, Monad m) => Chan t c -> ProgramT instr (Param2 exp pred) m (exp a) Source #

writeChan' :: (Typeable a, pred a, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) => Chan t c -> exp a -> ProgramT instr (Param2 exp pred) m (exp Bool) Source #