Copyright | (c) Sergey Vinokurov 2022 |
---|---|
License | Apache-2.0 (see LICENSE) |
Maintainer | serg.foo@gmail.com |
Safe Haskell | None |
Language | Haskell2010 |
Control.Concurrent.Counter.Unlifted
Description
Counters that support some atomic operations. Safe to use from
multiple threads and likely faster than using IORef
or
TVar
for the same operation (terms and
conditions apply).
This module defines unlifted newtype wrapper and corresponding operations,
they're not suitable for use with e.g. monads or being stored in other
data structures that expect lifted types. For general use start with
Counter
module.
Synopsis
- data Counter s :: UnliftedType
- new :: Int# -> State# s -> (# State# s, Counter s #)
- get :: Counter s -> State# s -> (# State# s, Int# #)
- set :: Counter s -> Int# -> State# s -> (# State# s #)
- cas :: Counter s -> Int# -> Int# -> State# s -> (# State# s, Int# #)
- add :: Counter s -> Int# -> State# s -> (# State# s, Int# #)
- sub :: Counter s -> Int# -> State# s -> (# State# s, Int# #)
- and :: Counter s -> Int# -> State# s -> (# State# s, Int# #)
- or :: Counter s -> Int# -> State# s -> (# State# s, Int# #)
- xor :: Counter s -> Int# -> State# s -> (# State# s, Int# #)
- nand :: Counter s -> Int# -> State# s -> (# State# s, Int# #)
- sameCounter :: Counter s -> Counter s -> Bool
Documentation
data Counter s :: UnliftedType #
Memory location that supports select few atomic operations.
Create
Read/write
cas :: Counter s -> Int# -> Int# -> State# s -> (# State# s, Int# #) #
Atomic compare and swap, i.e. write the new value if the current value matches the provided old value. Returns the value of the element before the operation
Arithmetic operations
add :: Counter s -> Int# -> State# s -> (# State# s, Int# #) #
Atomically add an amount to the counter and return its old value.
sub :: Counter s -> Int# -> State# s -> (# State# s, Int# #) #
Atomically subtract an amount from the counter and return its old value.
Bitwise operations
and :: Counter s -> Int# -> State# s -> (# State# s, Int# #) #
Atomically combine old value with a new one via bitwise and. Returns old counter value.
or :: Counter s -> Int# -> State# s -> (# State# s, Int# #) #
Atomically combine old value with a new one via bitwise or. Returns old counter value.
xor :: Counter s -> Int# -> State# s -> (# State# s, Int# #) #
Atomically combine old value with a new one via bitwise xor. Returns old counter value.
nand :: Counter s -> Int# -> State# s -> (# State# s, Int# #) #
Atomically combine old value with a new one via bitwise nand. Returns old counter value.
Compare
sameCounter :: Counter s -> Counter s -> Bool #
Compare the underlying pointers of two counters.