-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | One mutable cell per capability. A writer updates its own capability's cell by compare-and-swap,
evaluating the new value first, so it races another writer only when one is preempted between its
read and its swap. The loop is atomic-primops' own, compiled in the library, because coverage
instrumentation in the caller breaks the swap's pointer test. After 30 failed swaps it falls back
to a lazy update, which the next writer or a drain evaluates.
-}
module Ecluse.Runtime.Telemetry.Batched.Cells (
    Cells,
    newCells,
    modifyOwnCell,
    drainCells,
) where

import Data.Atomics (atomicModifyIORefCAS_)
import Data.Vector (Vector)
import Data.Vector qualified as V
import GHC.Conc (getNumCapabilities, myThreadId, threadCapability)
import GHC.IORef (atomicSwapIORef)

-- | One cell per capability the runtime had when the cells were made, each starting from the blank value.
data Cells a = Cells
    { forall a. Cells a -> a
cellsBlank :: a
    , forall a. Cells a -> IORef a
cellsFirst :: IORef a
    -- ^ Capability 0's cell, which a capability added after construction shares.
    , forall a. Cells a -> Vector (IORef a)
cellsAll :: Vector (IORef a)
    }

-- | Make one cell per capability.
newCells :: a -> IO (Cells a)
newCells :: forall a. a -> IO (Cells a)
newCells a
blank = do
    count <- IO Int
getNumCapabilities
    home <- newIORef blank
    rest <- V.replicateM (count - 1) (newIORef blank)
    pure Cells{cellsBlank = blank, cellsFirst = home, cellsAll = V.cons home rest}

-- | Apply a change to the calling thread's cell. A change that builds a strict value is evaluated whole.
modifyOwnCell :: Cells a -> (a -> a) -> IO ()
modifyOwnCell :: forall a. Cells a -> (a -> a) -> IO ()
modifyOwnCell Cells a
cells a -> a
change = do
    (capability, _) <- ThreadId -> IO (Int, Bool)
threadCapability (ThreadId -> IO (Int, Bool)) -> IO ThreadId -> IO (Int, Bool)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< IO ThreadId
myThreadId
    atomicModifyIORefCAS_ (fromMaybe (cellsFirst cells) (cellsAll cells V.!? capability)) change

-- | Take every cell's value, leaving the blank value in its place.
drainCells :: Cells a -> IO [a]
drainCells :: forall a. Cells a -> IO [a]
drainCells Cells a
cells = (IORef a -> IO a) -> [IORef a] -> IO [a]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (IORef a -> a -> IO a
forall a. IORef a -> a -> IO a
`atomicSwapIORef` Cells a -> a
forall a. Cells a -> a
cellsBlank Cells a
cells) (Vector (IORef a) -> [IORef a]
forall a. Vector a -> [a]
V.toList (Cells a -> Vector (IORef a)
forall a. Cells a -> Vector (IORef a)
cellsAll Cells a
cells))