imperative-edsl-0.8.2: Deep embedding of imperative programs with code generation
Safe HaskellNone
LanguageHaskell2010

Language.Embedded.Concurrent

Description

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

Documentation

data ThreadId Source #

Constructors

TIDRun ThreadId (Flag ()) 
TIDComp TID 

Instances

Instances details
Show ThreadId Source # 
Instance details

Defined in Language.Embedded.Concurrent.CMD

ToIdent ThreadId Source # 
Instance details

Defined in Language.Embedded.Concurrent.Backend.C

Methods

toIdent :: ThreadId -> SrcLoc -> Id #

data Chan t a Source #

A bounded channel.

Constructors

ChanRun (Chan Dynamic) 
ChanComp CID 

Instances

Instances details
ToIdent (Chan t a) Source # 
Instance details

Defined in Language.Embedded.Concurrent.Backend.C

Methods

toIdent :: Chan t a -> SrcLoc -> Id #

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.

Constructors

OneSize :: (pred a, Integral i) => proxy a -> exp i -> 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 

data ThreadCMD fs a Source #

Instances

Instances details
(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 # 
Instance details

Defined in Language.Embedded.Concurrent.CMD

Methods

reexpressInstrEnv :: forall (m :: Type -> Type) exp1 exp2 (fs :: k1) a. Monad m => (forall (b :: k). exp1 b -> ReaderT env (ProgramT instr '(exp2, fs) m) (exp2 b)) -> ThreadCMD '(ReaderT env (ProgramT instr '(exp2, fs) m), '(exp1, fs)) a -> ReaderT env (ProgramT instr '(exp2, fs) m) a #

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

Defined in Language.Embedded.Concurrent.CMD

Methods

interpBi :: forall (a :: k). ThreadCMD '(IO, '(IO, Param1 pred)) a -> IO a #

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

Defined in Language.Embedded.Concurrent.Backend.C

Methods

interp :: forall (a :: k). ThreadCMD '(CGen, Param2 exp pred) a -> CGen a #

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

Defined in Language.Embedded.Concurrent.CMD

Methods

hfmap :: forall f g (fs :: k1) (a :: k20). (forall (b :: k). f b -> g b) -> ThreadCMD '(f, fs) a -> ThreadCMD '(g, fs) a #

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

Defined in Language.Embedded.Concurrent.CMD

Methods

hbimap :: forall f g i j (fs :: k1) (a :: k20). (Functor f, Functor g) => (forall b. f b -> g b) -> (forall (b :: k). i b -> j b) -> ThreadCMD '(f, '(i, fs)) a -> ThreadCMD '(g, '(j, fs)) a #

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

Defined in Language.Embedded.Concurrent.CMD

Methods

dryInterp :: forall m (fs :: k) a. MonadSupply m => ThreadCMD '(m, fs) a -> m a Source #

data ChanCMD fs a Source #

Instances

Instances details
HFunctor (ChanCMD :: (k -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) Source # 
Instance details

Defined in Language.Embedded.Concurrent.CMD

Methods

hfmap :: forall f g (fs :: k1) (a :: k2). (forall (b :: k0). f b -> g b) -> ChanCMD '(f, fs) a -> ChanCMD '(g, fs) a #

(ChanCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) :<: instr => Reexpressible (ChanCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) (instr :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) env Source # 
Instance details

Defined in Language.Embedded.Concurrent.CMD

Methods

reexpressInstrEnv :: forall (m :: Type -> Type) exp1 exp2 (fs :: k1) a. Monad m => (forall (b :: k). exp1 b -> ReaderT env (ProgramT instr '(exp2, fs) m) (exp2 b)) -> ChanCMD '(ReaderT env (ProgramT instr '(exp2, fs) m), '(exp1, fs)) a -> ReaderT env (ProgramT instr '(exp2, fs) m) a #

InterpBi (ChanCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) IO (Param1 pred :: (Type -> Constraint, Type)) Source # 
Instance details

Defined in Language.Embedded.Concurrent.CMD

Methods

interpBi :: forall (a :: k). ChanCMD '(IO, '(IO, Param1 pred)) a -> IO a #

(CompExp exp, CompTypeClass ct, ct Bool) => Interp (ChanCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) CGen (Param2 exp ct :: (Type -> Type, (Type -> Constraint, Type))) Source # 
Instance details

Defined in Language.Embedded.Concurrent.Backend.C

Methods

interp :: forall (a :: k). ChanCMD '(CGen, Param2 exp ct) a -> CGen a #

HBifunctor (ChanCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) Source # 
Instance details

Defined in Language.Embedded.Concurrent.CMD

Methods

hbimap :: forall f g i j (fs :: k1) (a :: k2). (Functor f, Functor g) => (forall b. f b -> g b) -> (forall (b :: k). i b -> j b) -> ChanCMD '(f, '(i, fs)) a -> ChanCMD '(g, '(j, fs)) a #

DryInterp (ChanCMD :: (Type -> Type, (Type -> Type, (Type -> Constraint, Type))) -> Type -> Type) Source # 
Instance details

Defined in Language.Embedded.Concurrent.CMD

Methods

dryInterp :: forall m (fs :: k) a. MonadSupply m => ChanCMD '(m, fs) a -> m a 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.

readChanBuf Source #

Arguments

:: (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.

writeChanBuf Source #

Arguments

:: (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 #

When readChan was last called on the given channel, did the read succeed? Always returns True unless closeChan has been called on the channel. Always returns True if the channel has never been read.

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 #

readChanBuf' Source #

Arguments

:: (Typeable a, pred a, Ix i, Integral i, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) 
=> Chan t c 
-> exp i

Offset in array to start writing

-> exp i

Elements to read

-> Arr i a 
-> ProgramT instr (Param2 exp pred) m (exp Bool) 

writeChanBuf' Source #

Arguments

:: (Typeable a, pred a, Ix i, Integral i, FreeExp exp, FreePred exp Bool, ChanCMD :<: instr, Monad m) 
=> Chan t c 
-> exp i

Offset in array to start reading

-> exp i

Elements to write

-> Arr i a 
-> ProgramT instr (Param2 exp pred) m (exp Bool)