Safe Haskell | None |
---|
This version uses a boxed IORef representation, but it can be somewhat cheaper because it uses raw CAS rather than full atomicModifyIORef.
- newtype AtomicCounter = AtomicCounter (IORef Int)
- type CTicket = Ticket Int
- newCounter :: IO AtomicCounter
- readCounterForCAS :: AtomicCounter -> IO CTicket
- peekCTicket :: CTicket -> Int
- readCounter :: AtomicCounter -> IO Int
- writeCounter :: AtomicCounter -> Int -> IO ()
- casCounter :: AtomicCounter -> CTicket -> Int -> IO (Bool, CTicket)
Documentation
newtype AtomicCounter Source
newCounter :: IO AtomicCounterSource
Create a new counter initialized to zero.
readCounterForCAS :: AtomicCounter -> IO CTicketSource
Try repeatedly until we successfully increment the counter. incrCounter =
peekCTicket :: CTicket -> IntSource
readCounter :: AtomicCounter -> IO IntSource
writeCounter :: AtomicCounter -> Int -> IO ()Source
Make a non-atomic write to the counter. No memory-barrier.
casCounter :: AtomicCounter -> CTicket -> Int -> IO (Bool, CTicket)Source