Copyright | No rights reserved |
---|---|
License | UNLICENSE |
Maintainer | xenog@protonmail.com |
Stability | experimental |
Portability | POSIX |
Safe Haskell | None |
Language | Haskell2010 |
This is the core of the NQE library. It is composed of code to deal with
processes and mailboxes. Processes represent concurrent threads that receive
messages via a mailbox, also referred to as a channel. NQE is inspired by
Erlang/OTP and it stands for “Not Quite Erlang”. A process is analogous to an
actor in Scala, or an object in the original (Alan Kay) sense of the word. To
implement synchronous communication NQE makes use of STM
actions embedded in
asynchronous messages.
Synopsis
- type Listen a = a -> STM ()
- data Mailbox msg = OutChan mbox => Mailbox !(mbox msg) !Unique
- data Inbox msg = (OutChan mbox, InChan mbox) => Inbox !(mbox msg) !Unique
- data Process msg = Process {
- getProcessAsync :: Async ()
- getProcessMailbox :: Mailbox msg
- class InChan mbox where
- class OutChan mbox where
- inboxToMailbox :: Inbox msg -> Mailbox msg
- wrapChannel :: (MonadIO m, InChan mbox, OutChan mbox) => mbox msg -> m (Inbox msg)
- newInbox :: MonadIO m => m (Inbox msg)
- newBoundedInbox :: MonadIO m => Natural -> m (Inbox msg)
- send :: (MonadIO m, OutChan mbox) => msg -> mbox msg -> m ()
- receive :: (InChan mbox, MonadIO m) => mbox msg -> m msg
- query :: (MonadIO m, OutChan mbox) => (Listen response -> request) -> mbox request -> m response
- queryU :: (MonadUnliftIO m, OutChan mbox) => Int -> (Listen response -> request) -> mbox request -> m (Maybe response)
- queryS :: (MonadUnliftIO m, OutChan mbox) => Int -> (Listen response -> request) -> mbox request -> m (Maybe response)
- receiveMatch :: (MonadIO m, InChan mbox) => mbox msg -> (msg -> Maybe a) -> m a
- receiveMatchU :: (MonadUnliftIO m, InChan mbox) => Int -> mbox msg -> (msg -> Maybe a) -> m (Maybe a)
- receiveMatchS :: (MonadUnliftIO m, InChan mbox) => Int -> mbox msg -> (msg -> Maybe a) -> m (Maybe a)
- receiveMatchSTM :: InChan mbox => mbox msg -> (msg -> Maybe a) -> STM a
- mailboxEmpty :: (MonadIO m, InChan mbox) => mbox msg -> m Bool
- requeueListSTM :: InChan mbox => [msg] -> mbox msg -> STM ()
- withProcess :: MonadUnliftIO m => (Inbox msg -> m ()) -> (Process msg -> m a) -> m a
- process :: MonadUnliftIO m => (Inbox msg -> m ()) -> m (Process msg)
- newMailbox :: MonadUnliftIO m => m (Inbox msg, Mailbox msg)
Documentation
Channel that only allows messages to be sent to it.
Channel that allows to send or receive messages.
Process | |
|
class InChan mbox where Source #
Class for implementation of an Inbox
.
mailboxEmptySTM :: mbox msg -> STM Bool Source #
Are there messages queued?
receiveSTM :: mbox msg -> STM msg Source #
Receive a message.
requeueSTM :: msg -> mbox msg -> STM () Source #
Put a message in the mailbox such that it is received next.
Instances
InChan TQueue Source # | |
Defined in Control.Concurrent.NQE.Process | |
InChan TBQueue Source # | |
Defined in Control.Concurrent.NQE.Process | |
InChan Inbox Source # | |
Defined in Control.Concurrent.NQE.Process |
class OutChan mbox where Source #
Class for implementation of a Mailbox
.
mailboxFullSTM :: mbox msg -> STM Bool Source #
Is this bounded channel full? Always False
for unbounded channels.
sendSTM :: msg -> mbox msg -> STM () Source #
Send a message to this channel.
wrapChannel :: (MonadIO m, InChan mbox, OutChan mbox) => mbox msg -> m (Inbox msg) Source #
Wrap a channel in an Inbox
newBoundedInbox :: MonadIO m => Natural -> m (Inbox msg) Source #
Inbox
with upper bound on number of allowed queued messages.
query :: (MonadIO m, OutChan mbox) => (Listen response -> request) -> mbox request -> m response Source #
Send request to channel and wait for a response. The request
STM
action
will be created by this function.
queryU :: (MonadUnliftIO m, OutChan mbox) => Int -> (Listen response -> request) -> mbox request -> m (Maybe response) Source #
queryS :: (MonadUnliftIO m, OutChan mbox) => Int -> (Listen response -> request) -> mbox request -> m (Maybe response) Source #
receiveMatch :: (MonadIO m, InChan mbox) => mbox msg -> (msg -> Maybe a) -> m a Source #
Test all messages in a channel against the supplied function and return the first matching message. Will block until a match is found. Messages that do not match remain in the channel.
receiveMatchU :: (MonadUnliftIO m, InChan mbox) => Int -> mbox msg -> (msg -> Maybe a) -> m (Maybe a) Source #
Like receiveMatch
but with a timeout set at u
microseconds. Returns
Nothing
if timeout is reached.
receiveMatchS :: (MonadUnliftIO m, InChan mbox) => Int -> mbox msg -> (msg -> Maybe a) -> m (Maybe a) Source #
Like receiveMatch
but with a timeout set at s
seconds. Returns
Nothing
if timeout is reached.
receiveMatchSTM :: InChan mbox => mbox msg -> (msg -> Maybe a) -> STM a Source #
Match a message in the channel as an atomic STM
action.
mailboxEmpty :: (MonadIO m, InChan mbox) => mbox msg -> m Bool Source #
Check if the channel is empty.
requeueListSTM :: InChan mbox => [msg] -> mbox msg -> STM () Source #
Put a list of messages at the start of a channel, so that the last element of the list is the next message to be received.
withProcess :: MonadUnliftIO m => (Inbox msg -> m ()) -> (Process msg -> m a) -> m a Source #
Run a process in the background and pass it to a function. Stop the background process once the function returns. Background process exceptions are re-thrown in the current thread.
process :: MonadUnliftIO m => (Inbox msg -> m ()) -> m (Process msg) Source #
Run a process in the background and return the Process
handle. Background
process exceptions are re-thrown in the current thread.
newMailbox :: MonadUnliftIO m => m (Inbox msg, Mailbox msg) Source #
Create an unbounded inbox and corresponding mailbox.