Control.Concurrent.Chan.Unagi

unagi-chan-0.4.1.4: Fast concurrent queues with a Chan-like API, and more

Safe Haskell | None
Language | Haskell2010

Control.Concurrent.Chan.Unagi

Contents

- Creating channels
- Channel operations
  - Reading
  - Writing
  - Broadcasting

Synopsis

- newChan :: IO (InChan a, OutChan a)
- data InChan a
- data OutChan a
- readChan :: OutChan a -> IO a
- readChanOnException :: OutChan a -> (IO a -> IO ()) -> IO a
- tryReadChan :: OutChan a -> IO (Element a, IO a)
- newtype Element a = Element {
  - tryRead :: IO (Maybe a)
  }
- getChanContents :: OutChan a -> IO [a]
- writeChan :: InChan a -> a -> IO ()
- writeList2Chan :: InChan a -> [a] -> IO ()
- dupChan :: InChan a -> IO (OutChan a)

# Documentation

General-purpose concurrent FIFO queue. If you are trying to send messages of a primitive unboxed type, you may wish to use Control.Concurrent.Chan.Unagi.Unboxed which should be slightly faster and perform better when a queue grows very large. If you need a bounded queue, see Control.Concurrent.Chan.Unagi.Bounded. And if your application doesn't require blocking reads, or is single-producer or single-consumer, then Control.Concurrent.Chan.Unagi.NoBlocking will offer lowest latency.

# Creating channels

newChan :: IO (InChan a, OutChan a) #

Create a new channel, returning its write and read ends.

data InChan a #

The write end of a channel created with `newChan`.

#### Instances

Instances details

Eq (InChan a) # |
Instance details Defined in Control.Concurrent.Chan.Unagi.Internal Methods (==) :: InChan a -> InChan a -> Bool # (/=) :: InChan a -> InChan a -> Bool #

data OutChan a #

The read end of a channel created with `newChan`.

#### Instances

Instances details

Eq (OutChan a) # |
Instance details Defined in Control.Concurrent.Chan.Unagi.Internal Methods (==) :: OutChan a -> OutChan a -> Bool # (/=) :: OutChan a -> OutChan a -> Bool #

# Channel operations

## Reading

readChan :: OutChan a -> IO a #

Read an element from the chan, blocking if the chan is empty.

Note re. exceptions: When an async exception is raised during a `readChan` the message that the read would have returned is likely to be lost, even when the read is known to be blocked on an empty queue. If you need to handle this scenario, you can use `readChanOnException`.

readChanOnException :: OutChan a -> (IO a -> IO ()) -> IO a #

Like `readChan` but allows recovery of the queue element which would have been read, in the case that an async exception is raised during the read. To be precise exceptions are raised, and the handler run, only when `readChanOnException` is blocking.

The second argument is a handler that takes a blocking IO action returning the element, and performs some recovery action. When the handler is called, the passed `IO a` is the only way to access the element.

tryReadChan :: OutChan a -> IO (Element a, IO a) #

Returns immediately with:

- an ``Element` a` future, which returns one unique element when it becomes available via `tryRead`.
- a blocking `IO` action that returns the element when it becomes available.

Note: This is a destructive operation. See `Element` for more details.

If you're using this function exclusively you might find the implementation in Control.Concurrent.Chan.Unagi.NoBlocking is faster.

Note re. exceptions: When an async exception is raised during a `tryReadChan` the message that the read would have returned is likely to be lost, just as it would be when raised directly after this function returns.

newtype Element a #

An `IO` action that returns a particular enqueued element when and if it becomes available.

Each `Element` corresponds to a particular enqueued element, i.e. a returned `Element` always offers the only means to access one particular enqueued item. The value returned by `tryRead` moves monotonically from `Nothing` to `Just a` when and if an element becomes available, and is idempotent at that point.

So for instance:

       (in, out) <- newChan
       (el, _) <- tryReadChan out  -- READ FROM EMPTY CHAN
       writeChan in "msg1"
       writeChan in "msg2"
       readChan out        -- RETURNS "msg2"
       tryRead el          -- RETURNS "msg1" (which would otherwise be lost)

Constructors

Element |
Fields tryRead :: IO (Maybe a)

#### Instances

Instances details

Alternative Element # |
Instance details Defined in Control.Concurrent.Chan.Unagi.NoBlocking.Types Methods empty :: Element a # (<|>) :: Element a -> Element a -> Element a # some :: Element a -> Element [a] # many :: Element a -> Element [a] #
Applicative Element # |
Instance details Defined in Control.Concurrent.Chan.Unagi.NoBlocking.Types Methods pure :: a -> Element a # (<*>) :: Element (a -> b) -> Element a -> Element b # liftA2 :: (a -> b -> c) -> Element a -> Element b -> Element c # (*>) :: Element a -> Element b -> Element b # (<*) :: Element a -> Element b -> Element a #
Functor Element # |
Instance details Defined in Control.Concurrent.Chan.Unagi.NoBlocking.Types Methods fmap :: (a -> b) -> Element a -> Element b # (<$) :: a -> Element b -> Element a #
Monad Element # |
Instance details Defined in Control.Concurrent.Chan.Unagi.NoBlocking.Types Methods (>>=) :: Element a -> (a -> Element b) -> Element b # (>>) :: Element a -> Element b -> Element b # return :: a -> Element a #
MonadPlus Element # |
Instance details Defined in Control.Concurrent.Chan.Unagi.NoBlocking.Types Methods mzero :: Element a # mplus :: Element a -> Element a -> Element a #
MonadFail Element # |
Instance details Defined in Control.Concurrent.Chan.Unagi.NoBlocking.Types Methods fail :: String -> Element a #
MonadFix Element # |
Instance details Defined in Control.Concurrent.Chan.Unagi.NoBlocking.Types Methods mfix :: (a -> Element a) -> Element a #

getChanContents :: OutChan a -> IO [a] #

Return a lazy infinite list representing the contents of the supplied OutChan, much like System.IO.hGetContents.

## Writing

writeChan :: InChan a -> a -> IO () #

Write a value to the channel.

writeList2Chan :: InChan a -> [a] -> IO () #

Write an entire list of items to a chan type. Writes here from multiple threads may be interleaved, and infinite lists are supported.

## Broadcasting

dupChan :: InChan a -> IO (OutChan a) #

Duplicate a chan: the returned `OutChan` begins empty, but data written to the argument `InChan` from then on will be available from both the original `OutChan` and the one returned here, creating a kind of broadcast channel.