hs-opentelemetry-api
Copyright(c) Ian Duncan 2024-2026
LicenseBSD-3
Stabilityexperimental
Safe HaskellNone
LanguageHaskell2010

OpenTelemetry.Internal.AtomicCounter

Description

Unlike atomicModifyIORef' which does a CAS retry loop on the boxed IORef closure, these operations compile down to a single lock xadd (x86) or ldadd (AArch64) instruction with no allocation and no retry.

Synopsis

Documentation

data AtomicCounter Source #

A mutable atomic counter backed by a single unboxed machine-word Int.

Uses hardware fetch-and-add (fetchAddIntArray#) instead of CAS retry loops, making increment/add O(1) regardless of contention.

newAtomicCounter :: Int -> IO AtomicCounter Source #

Create a new counter initialized to the given value.

incrAtomicCounter :: AtomicCounter -> IO Int Source #

Atomically increment the counter by 1. Returns the value after the increment.

addAtomicCounter :: Int -> AtomicCounter -> IO Int Source #

Atomically add to the counter. Returns the value after the add.

fetchAddAtomicCounter :: Int -> AtomicCounter -> IO Int Source #

Atomically add to the counter. Returns the value before the add. Useful for monotonic ID allocation.

readAtomicCounter :: AtomicCounter -> IO Int Source #

Read the current counter value.

This is a relaxed read; no ordering guarantee relative to concurrent adds on other cores. Fine for diagnostics and metrics.

writeAtomicCounter :: AtomicCounter -> Int -> IO () Source #

Overwrite the counter value. Not atomic with respect to concurrent adds.