Control.Concurrent.Counter.Lifted.ST

atomic-counter-0.1.2.3: Mutable counters that can be modified with atomic operatinos

Copyright | (c) Sergey Vinokurov 2022
License | Apache-2.0 (see LICENSE)
Maintainer | serg.foo@gmail.com
Safe Haskell | None
Language | Haskell2010

Control.Concurrent.Counter.Lifted.ST

Contents

- Create
- Read/write
- Arithmetic operations
- Bitwise operations

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).

Synopsis

- data Counter s
- new :: Int -> ST s (Counter s)
- get :: Counter s -> ST s Int
- set :: Counter s -> Int -> ST s ()
- cas :: Counter s -> Int -> Int -> ST s Int
- add :: Counter s -> Int -> ST s Int
- sub :: Counter s -> Int -> ST s Int
- and :: Counter s -> Int -> ST s Int
- or :: Counter s -> Int -> ST s Int
- xor :: Counter s -> Int -> ST s Int
- nand :: Counter s -> Int -> ST s Int

# Documentation

data Counter s #

Memory location that supports select few atomic operations.

Isomorphic to `STRef s Int`.

#### Instances

Instances details

Eq (Counter s) # | Pointer equality
Instance details Defined in Control.Concurrent.Counter.Lifted.ST Methods (==) :: Counter s -> Counter s -> Bool # (/=) :: Counter s -> Counter s -> Bool #

# Create

new :: Int -> ST s (Counter s) #

Create new counter with initial value.

# Read/write

get :: Counter s -> ST s Int #

Atomically read the counter's value.

set :: Counter s -> Int -> ST s () #

Atomically assign new value to the counter.

cas #

Arguments

:: Counter s |
-> Int | Expected old value
-> Int | New value
-> ST 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 -> ST s Int #

Atomically add an amount to the counter and return its old value.

sub :: Counter s -> Int -> ST s Int #

Atomically subtract an amount from the counter and return its old value.

# Bitwise operations

and :: Counter s -> Int -> ST s Int #

Atomically combine old value with a new one via bitwise and. Returns old counter value.

or :: Counter s -> Int -> ST s Int #

Atomically combine old value with a new one via bitwise or. Returns old counter value.

xor :: Counter s -> Int -> ST s Int #

Atomically combine old value with a new one via bitwise xor. Returns old counter value.

nand :: Counter s -> Int -> ST s Int #

Atomically combine old value with a new one via bitwise nand. Returns old counter value.