Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Synopsis
- data Store ctx
- withNonEmptyStore :: forall m ctx a. (MonadIO m, MonadMask m) => ctx -> (Store ctx -> m a) -> m a
- withEmptyStore :: forall m ctx a. (MonadIO m, MonadMask m) => (Store ctx -> m a) -> m a
- use :: forall m ctx a. (MonadIO m, MonadMask m) => Store ctx -> ctx -> m a -> m a
- adjust :: forall m ctx a. (MonadIO m, MonadMask m) => Store ctx -> (ctx -> ctx) -> m a -> m a
- withAdjusted :: forall m ctx a. (MonadIO m, MonadMask m) => Store ctx -> (ctx -> ctx) -> (ctx -> m a) -> m a
- mine :: forall m ctx. (MonadIO m, MonadThrow m) => Store ctx -> m ctx
- mines :: forall m ctx a. (MonadIO m, MonadThrow m) => Store ctx -> (ctx -> a) -> m a
- mineMay :: forall m ctx. MonadIO m => Store ctx -> m (Maybe ctx)
- minesMay :: forall m ctx a. MonadIO m => Store ctx -> (ctx -> a) -> m (Maybe a)
- module Context.View
- data NotFoundException = NotFoundException {}
- module Context.Concurrent
- module Context.Storage
Introduction
This module provides an opaque Store
for thread-indexed storage around
arbitrary context values. The interface supports nesting context values per
thread, and at any point, the calling thread may ask for its current context.
Note that threads in Haskell have no explicit parent-child relationship. So if
you register a context in a Store
produced by withEmptyStore
, spin up a
separate thread, and from that thread you ask for a context, that thread will
not have a context in the Store
. Use Context.Concurrent as a drop-in
replacement for Control.Concurrent to have the library handle context
propagation from one thread to another automatically. Otherwise, you must
explicitly register contexts from each thread when using a Store
produced by
withEmptyStore
.
If you have a default context that is always applicable to all threads, you may
wish to use withNonEmptyStore
. All threads may access this default context
(without leveraging Context.Concurrent or explicitly registering context
for the threads) when using a Store
produced by withNonEmptyStore
.
Regardless of how you initialize your Store
, every thread is free to nest its
own specific context values.
This module is designed to be imported qualified:
import qualified Context
Storage
Opaque type that manages thread-indexed storage of context values.
Since: 0.1.0.0
withNonEmptyStore :: forall m ctx a. (MonadIO m, MonadMask m) => ctx -> (Store ctx -> m a) -> m a Source #
Operations
Registering context
use :: forall m ctx a. (MonadIO m, MonadMask m) => Store ctx -> ctx -> m a -> m a Source #
Register a context in the specified Store
on behalf of the calling
thread, for the duration of the specified action.
Since: 0.1.0.0
adjust :: forall m ctx a. (MonadIO m, MonadMask m) => Store ctx -> (ctx -> ctx) -> m a -> m a Source #
Adjust the calling thread's context in the specified Store
for the
duration of the specified action. Throws a NotFoundException
when the
calling thread has no registered context.
Since: 0.1.0.0
withAdjusted :: forall m ctx a. (MonadIO m, MonadMask m) => Store ctx -> (ctx -> ctx) -> (ctx -> m a) -> m a Source #
Convenience function to adjust
the context then supply the adjusted
context to the inner action. This function is equivalent to calling adjust
and then immediately calling mine
in the inner action of adjust
, e.g.:
doStuff :: Store Thing -> (Thing -> Thing) -> IO () doStuff store f = do adjust store f do adjustedThing <- mine store ...
Throws a NotFoundException
when the calling thread has no registered
context.
Since: 0.2.0.0
Asking for context
mine :: forall m ctx. (MonadIO m, MonadThrow m) => Store ctx -> m ctx Source #
Provide the calling thread its current context from the specified
Store
. Throws a NotFoundException
when the calling thread has no
registered context.
Since: 0.1.0.0
mines :: forall m ctx a. (MonadIO m, MonadThrow m) => Store ctx -> (ctx -> a) -> m a Source #
Provide the calling thread a selection from its current context in the
specified Store
. Throws a NotFoundException
when the calling
thread has no registered context.
Since: 0.1.0.0
mineMay :: forall m ctx. MonadIO m => Store ctx -> m (Maybe ctx) Source #
Provide the calling thread its current context from the specified
Store
, if present.
Since: 0.1.0.0
minesMay :: forall m ctx a. MonadIO m => Store ctx -> (ctx -> a) -> m (Maybe a) Source #
Provide the calling thread a selection from its current context in the
specified Store
, if present.
Since: 0.1.0.0
Views
module Context.View
Exceptions
data NotFoundException Source #
An exception which may be thrown when the calling thread does not have a registered context.
Since: 0.1.0.0
Instances
Exception NotFoundException Source # | |
Defined in Context.Internal | |
Generic NotFoundException Source # | |
Defined in Context.Internal type Rep NotFoundException :: Type -> Type # from :: NotFoundException -> Rep NotFoundException x # to :: Rep NotFoundException x -> NotFoundException # | |
Show NotFoundException Source # | |
Defined in Context.Internal showsPrec :: Int -> NotFoundException -> ShowS # show :: NotFoundException -> String # showList :: [NotFoundException] -> ShowS # | |
Eq NotFoundException Source # | |
Defined in Context.Internal (==) :: NotFoundException -> NotFoundException -> Bool # (/=) :: NotFoundException -> NotFoundException -> Bool # | |
type Rep NotFoundException Source # | |
Defined in Context.Internal type Rep NotFoundException = D1 ('MetaData "NotFoundException" "Context.Internal" "context-0.2.0.1-DiLV6VUVy9GGncT6FdoMXM" 'False) (C1 ('MetaCons "NotFoundException" 'PrefixI 'True) (S1 ('MetaSel ('Just "threadId") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedStrict) (Rec0 ThreadId))) |
Concurrency
module Context.Concurrent
Lower-level storage
module Context.Storage