atomic-counter-0.1.2.4: 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.IO

Contents

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

Description

Lifted `Counter` specialized to operate in the `IO` monad.

Synopsis

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

# Documentation

data Counter Source #

Memory location that supports select few atomic operations.

Isomorphic to `IORef Int`.

#### Instances

Instances details

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

# Create

new :: Int -> IO Counter Source #

Create new counter with initial value.

# Read/write

get :: Counter -> IO Int Source #

Atomically read the counter's value.

set :: Counter -> Int -> IO () Source #

Atomically assign new value to the counter.

cas Source #

Arguments

:: Counter |
-> Int | Expected old value
-> Int | New value
-> IO 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

Since: 0.1.2

# Arithmetic operations

add :: Counter -> Int -> IO Int Source #

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

sub :: Counter -> Int -> IO Int Source #

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

# Bitwise operations

and :: Counter -> Int -> IO Int Source #

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

or :: Counter -> Int -> IO Int Source #

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

xor :: Counter -> Int -> IO Int Source #

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

nand :: Counter -> Int -> IO Int Source #

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