-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | A safe approach to CAS and other atomic ops in Haskell. -- -- After GHC 7.4 a new `casMutVar#` primop became available, but it's -- difficult to use safely, because pointer equality is a highly unstable -- property in Haskell. This library provides a safer method based on the -- concept of Tickets. -- -- Also, this library uses the foreign primop capability of GHC to -- add access to other variants that may be of interest, specifically, -- compare and swap inside an array. @package atomic-primops @version 0.1.0.2 -- | This module provides only the raw primops (and necessary types) for -- atomic operations. module Data.Atomics.Internal -- | Unsafe, machine-level atomic compare and swap on an element within an -- Array. casArray# :: MutableArray# RealWorld a -> Int# -> Ticket a -> Ticket a -> State# RealWorld -> (# State# RealWorld, Int#, Ticket a #) readForCAS# :: MutVar# RealWorld a -> State# RealWorld -> (# State# RealWorld, Ticket a #) casMutVarTicketed# :: MutVar# RealWorld a -> Ticket a -> Ticket a -> State# RealWorld -> (# State# RealWorld, Int#, 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. type Ticket a = Any a instance Eq (Ticket a) instance Show (Ticket a) -- | Provides atomic memory operations on IORefs and Mutable Arrays. -- -- Pointer equality need not be maintained by a Haskell compiler. For -- example, Int values will frequently be boxed and unboxed, changing the -- pointer identity of the thunk. To deal with this, the compare-and-swap -- (CAS) approach used in this module is uses a sealed -- representation of pointers into the Haskell heap (Tickets). -- Currently, the user cannot coin new tickets, rather a Ticket -- provides evidence of a past observation, and grants permission to make -- a future change. module Data.Atomics -- | 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. type Ticket a = Any a -- | A ticket contains or can get the usable Haskell value. peekTicket :: Ticket a -> a -- | Compare-and-swap casArrayElem :: MutableArray RealWorld a -> Int -> Ticket a -> a -> IO (Bool, Ticket a) -- | This variant takes two tickets: the new value is a ticket -- rather than an arbitrary, lifted, Haskell value. casArrayElem2 :: MutableArray RealWorld a -> Int -> Ticket a -> Ticket a -> IO (Bool, Ticket a) readArrayElem :: MutableArray RealWorld a -> Int -> IO (Ticket a) readForCAS :: IORef a -> IO (Ticket a) -- | Performs a machine-level compare and swap operation on an -- IORef. Returns a tuple containing a Bool which is -- True when a swap is performed, along with the current -- value from the IORef. -- -- Note "compare" here means pointer equality in the sense of -- reallyUnsafePtrEquality#. casIORef :: IORef a -> Ticket a -> a -> IO (Bool, Ticket a) -- | This variant takes two tickets, i.e. the new value is a -- ticket rather than an arbitrary, lifted, Haskell value. casIORef2 :: IORef a -> Ticket a -> Ticket a -> IO (Bool, Ticket a) readMutVarForCAS :: MutVar# RealWorld a -> IO (Ticket a) -- | MutVar counterpart of casIORef. casMutVar :: MutVar# RealWorld a -> Ticket a -> a -> IO (Bool, Ticket a) -- | This variant takes two tickets, i.e. the new value is a -- ticket rather than an arbitrary, lifted, Haskell value. casMutVar2 :: MutVar# RealWorld a -> Ticket a -> Ticket a -> IO (Bool, Ticket a)