atomic-primops-0.5: A safe approach to CAS and other atomic ops in Haskell.

Safe HaskellNone




newCounter :: Int -> IO AtomicCounterSource

Create a new counter initialized to the given value.

readCounterForCAS :: AtomicCounter -> IO CTicketSource

Just like the Data.Atomics CAS interface, this routine returns an opaque ticket that can be used in CAS operations.

peekCTicket :: CTicket -> IntSource

Opaque tickets cannot be constructed, but they can be destructed into values.

writeCounter :: AtomicCounter -> Int -> IO ()Source

Make a non-atomic write to the counter. No memory-barrier.

casCounter :: AtomicCounter -> CTicket -> Int -> IO (Bool, CTicket)Source

Compare and swap for the counter ADT.

incrCounter :: Int -> AtomicCounter -> IO IntSource

Increment the counter by a given amount. Returns the value AFTER the increment (in contrast with the behavior of the underlying instruction on architectures like x86.)

Note that UNLIKE with boxed implementations of counters, where increment is based on CAS, this increment is O(1). Fetch-and-add does not require a retry loop like CAS.

incrCounter_ :: Int -> AtomicCounter -> IO ()Source

An alternate version for when you don't care about the old value.