Safe Haskell | Safe |
---|---|
Language | Haskell98 |
Data.StateRef.Instances
Contents
Description
This module exports no new symbols of its own. It defines several basic class instances for creating, reading, and writing standard reference types, and re-exports the types for which it defines instances.
TODO: add millions of SPECIALIZE INSTANCE pragmas, for IO monad at a minimum.
- data IORef a :: * -> *
- data MVar a :: * -> *
- class Monad m => MonadIO (m :: * -> *) where
- data STRef s a :: * -> * -> *
- data ST s a :: * -> * -> *
- data RealWorld :: *
- data ForeignPtr a :: * -> *
- data STM a :: * -> *
- data TVar a :: * -> *
- data TMVar a :: * -> *
- atomically :: STM a -> IO a
- newtype UnsafeModifyRef sr = UnsafeModifyRef sr
Documentation
An MVar
(pronounced "em-var") is a synchronising variable, used
for communication between concurrent threads. It can be thought of
as a a box, which may be empty or full.
class Monad m => MonadIO (m :: * -> *) where #
Monads in which IO
computations may be embedded.
Any monad built by applying a sequence of monad transformers to the
IO
monad will be an instance of this class.
Instances should satisfy the following laws, which state that liftIO
is a transformer of monads:
Minimal complete definition
data STRef s a :: * -> * -> * #
a value of type STRef s a
is a mutable variable in state thread s
,
containing a value of type a
The strict state-transformer monad.
A computation of type
transforms an internal state indexed
by ST
s as
, and returns a value of type a
.
The s
parameter is either
- an uninstantiated type variable (inside invocations of
runST
), or RealWorld
(inside invocations ofstToIO
).
It serves to keep the internal states of different invocations
of runST
separate from each other and from invocations of
stToIO
.
The >>=
and >>
operations are strict in the state (though not in
values stored in the state). For example,
runST
(writeSTRef _|_ v >>= f) = _|_
RealWorld
is deeply magical. It is primitive, but it is not
unlifted (hence ptrArg
). We never manipulate values of type
RealWorld
; it's only used in the type system, to parameterise State#
.
data ForeignPtr a :: * -> * #
The type ForeignPtr
represents references to objects that are
maintained in a foreign language, i.e., that are not part of the
data structures usually managed by the Haskell storage manager.
The essential difference between ForeignPtr
s and vanilla memory
references of type Ptr a
is that the former may be associated
with finalizers. A finalizer is a routine that is invoked when
the Haskell storage manager detects that - within the Haskell heap
and stack - there are no more references left that are pointing to
the ForeignPtr
. Typically, the finalizer will, then, invoke
routines in the foreign language that free the resources bound by
the foreign object.
The ForeignPtr
is parameterised in the same way as Ptr
. The
type argument of ForeignPtr
should normally be an instance of
class Storable
.
Instances
Eq (ForeignPtr a) | Since: 2.1 |
Ord (ForeignPtr a) | Since: 2.1 |
Show (ForeignPtr a) | Since: 2.1 |
A monad supporting atomic memory transactions.
Instances
Monad STM | Since: 4.3.0.0 |
Functor STM | Since: 4.3.0.0 |
Applicative STM | Since: 4.8.0.0 |
Alternative STM | Since: 4.8.0.0 |
MonadPlus STM | Since: 4.3.0.0 |
Shared memory locations that support atomic memory transactions.
A TMVar
is a synchronising variable, used
for communication between concurrent threads. It can be thought of
as a box, which may be empty or full.
atomically :: STM a -> IO a #
Perform a series of STM actions atomically.
You cannot use atomically
inside an unsafePerformIO
or unsafeInterleaveIO
.
Any attempt to do so will result in a runtime error. (Reason: allowing
this would effectively allow a transaction inside a transaction, depending
on exactly when the thunk is evaluated.)
However, see newTVarIO
, which can be called inside unsafePerformIO
,
and which allows top-level TVars to be allocated.
newtype UnsafeModifyRef sr Source #
Wrap a state reference that supports reading and writing, and add a
potentially thread-unsafe ModifyRef
instance.
Constructors
UnsafeModifyRef sr |