module Data.Atomics.Counter.IORef
where
import Data.IORef
import Data.Atomics as A
newtype AtomicCounter = AtomicCounter (IORef Int)
type CTicket = Ticket Int
newCounter :: IO AtomicCounter
newCounter = fmap AtomicCounter $ newIORef 0
readCounterForCAS :: AtomicCounter -> IO CTicket
readCounterForCAS (AtomicCounter r) = readForCAS r
peekCTicket :: CTicket -> Int
peekCTicket = peekTicket
readCounter :: AtomicCounter -> IO Int
readCounter (AtomicCounter r) = readIORef r
writeCounter :: AtomicCounter -> Int -> IO ()
writeCounter (AtomicCounter r) !new = writeIORef r new
casCounter :: AtomicCounter -> CTicket -> Int -> IO (Bool, CTicket)
casCounter (AtomicCounter r) tick !new = casIORef r tick new