planet-mitchell-0.1.0: Planet Mitchell

Safe HaskellNone
LanguageHaskell2010

Concurrency.IORef

Contents

Synopsis

IORef

data IORef a #

A mutable variable in the IO monad

Instances
NFData1 IORef

Since: deepseq-1.4.3.0

Instance details

Defined in Control.DeepSeq

Methods

liftRnf :: (a -> ()) -> IORef a -> () #

Eq (IORef a)

Pointer equality.

Since: base-4.1.0.0

Instance details

Defined in GHC.IORef

Methods

(==) :: IORef a -> IORef a -> Bool #

(/=) :: IORef a -> IORef a -> Bool #

NFData (IORef a)

NOTE: Only strict in the reference and not the referenced value.

Since: deepseq-1.4.2.0

Instance details

Defined in Control.DeepSeq

Methods

rnf :: IORef a -> () #

newIORef :: MonadIO m => a -> m (IORef a) #

Lifted newIORef.

Since: unliftio-0.1.0.0

readIORef :: MonadIO m => IORef a -> m a #

Lifted readIORef.

Since: unliftio-0.1.0.0

writeIORef :: MonadIO m => IORef a -> a -> m () #

Lifted writeIORef.

Since: unliftio-0.1.0.0

writeIORef' :: IORef a -> a -> IO () #

Evaluates the value before calling writeIORef.

modifyIORef :: MonadIO m => IORef a -> (a -> a) -> m () #

Lifted modifyIORef.

Since: unliftio-0.1.0.0

modifyIORef' :: MonadIO m => IORef a -> (a -> a) -> m () #

Lifted modifyIORef'.

Since: unliftio-0.1.0.0

atomicModifyIORef :: MonadIO m => IORef a -> (a -> (a, b)) -> m b #

Lifted atomicModifyIORef.

Since: unliftio-0.1.0.0

atomicModifyIORef' :: MonadIO m => IORef a -> (a -> (a, b)) -> m b #

Lifted atomicModifyIORef'.

Since: unliftio-0.1.0.0

atomicWriteIORef :: MonadIO m => IORef a -> a -> m () #

Lifted atomicWriteIORef.

Since: unliftio-0.1.0.0

atomicWriteIORef' :: IORef a -> a -> IO () #

Evaluates the value before calling atomicWriteIORef.

mkWeakIORef :: MonadUnliftIO m => IORef a -> m () -> m (Weak (IORef a)) #

Unlifted mkWeakIORef.

Since: unliftio-0.1.0.0

Atomic check-and-set

data Ticket a #

When performing compare-and-swaps, the ticket encapsulates proof that a thread observed a specific previous value of a mutable variable. It is provided in lieu of the "old" value to compare-and-swap.

Design note: Tickets exist to hide objects from the GHC compiler, which can normally perform many optimizations that change pointer equality. A Ticket, on the other hand, is a first-class object that can be handled by the user, but will not have its pointer identity changed by compiler optimizations (but will of course, change addresses during garbage collection).

Instances
Eq (Ticket a) 
Instance details

Defined in Data.Atomics.Internal

Methods

(==) :: Ticket a -> Ticket a -> Bool #

(/=) :: Ticket a -> Ticket a -> Bool #

Show (Ticket a) 
Instance details

Defined in Data.Atomics.Internal

Methods

showsPrec :: Int -> Ticket a -> ShowS #

show :: Ticket a -> String #

showList :: [Ticket a] -> ShowS #

peekTicket :: Ticket a -> a #

A ticket contains or can get the usable Haskell value. This function does just that.

readForCAS :: IORef a -> IO (Ticket a) #

Ordinary processor load instruction (non-atomic, not implying any memory barriers).

The difference between this function and readIORef, is that it returns a ticket, for use in future compare-and-swap operations.

casIORef #

Arguments

:: IORef a

The IORef containing a value current

-> Ticket a

A ticket for the old value

-> a

The new value to replace current if old == current

-> IO (Bool, Ticket a)

Success flag, plus ticket for the NEXT operation.

Performs a machine-level compare and swap (CAS) operation on an IORef. Returns a tuple containing a Bool which is True when a swap is performed, along with the most current value from the IORef. Note that this differs from the more common CAS behavior, which is to return the old value before the CAS occured.

The reason for the difference is the ticket API. This function always returns the ticket that you should use in your next CAS attempt. In case of success, this ticket corresponds to the new value which you yourself installed in the IORef, whereas in the case of failure it represents the preexisting value currently in the IORef.

Note "compare" here means pointer equality in the sense of reallyUnsafePtrEquality#. However, the ticket API absolves the user of this module from needing to worry about the pointer equality of their values, which in general requires reasoning about the details of the Haskell implementation (GHC).

By convention this function is strict in the "new" value argument. This isn't absolutely necesary, but we think it's a bad habit to use unevaluated thunks in this context.

casIORef2 #

Arguments

:: IORef a 
-> Ticket a

A ticket for the old value

-> Ticket a

A ticket for the new value

-> IO (Bool, Ticket a) 

This variant takes two tickets, i.e. the new value is a ticket rather than an arbitrary, lifted, Haskell value.

atomicModifyIORefCAS #

Arguments

:: IORef a

Mutable location to modify

-> (a -> (a, b))

Computation runs one or more times (speculation)

-> IO b 

A drop-in replacement for atomicModifyIORef that optimistically attempts to compute the new value and CAS it into place without introducing new thunks or locking anything. Note that this is more STRICT than its standard counterpart and will only place evaluated (WHNF) values in the IORef.

The upside is that sometimes we see a performance benefit. The downside is that this version is speculative -- when it retries, it must reexecute the compution.

atomicModifyIORefCAS_ :: IORef t -> (t -> t) -> IO () #

A simpler version that modifies the state but does not return anything.