Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
0433c5b
Bump base constraint.
TravisWhitaker Mar 6, 2020
420f559
Prototype atomic-memory-safe implementation.
TravisWhitaker Jul 19, 2020
2ac505c
Dependency version bumps.
TravisWhitaker Dec 24, 2023
16954da
Merge branch 'bumps' of github.com:TravisWhitaker/ekg-core into bumps
TravisWhitaker Dec 24, 2023
a52ddad
Merge branch 'bumps' of github.com:TravisWhitaker/ekg-core into memor…
TravisWhitaker Dec 24, 2023
74f2239
Update .gitignore
TravisWhitaker Dec 24, 2023
12ab6a7
Fix CPP
TravisWhitaker Dec 24, 2023
a4b7ff9
Merge branch 'master' of github.com:haskell-github-trust/ekg-core int…
TravisWhitaker Jun 8, 2025
53d0986
Clean up a bit.
TravisWhitaker Jun 8, 2025
d76fd8f
Fix ghc-prim constraint
TravisWhitaker Jun 8, 2025
db37322
Fix with 8.0.x
TravisWhitaker Jun 8, 2025
56ca464
fix it harder
TravisWhitaker Jun 8, 2025
0fc6787
fix it harder
TravisWhitaker Jun 8, 2025
316a206
fix it harder
TravisWhitaker Jun 8, 2025
0653030
Fast way on 64-bit, slow way on 32-bit.
TravisWhitaker Jun 19, 2025
5d6ad40
Remove some unnecessary changes, preserve some of the old explanatory…
TravisWhitaker Jun 19, 2025
e5439d2
Make it work on wasm32-wasi and older GHCs
TravisWhitaker Jun 19, 2025
a12f5bd
Int64# was added in GHC 9.4.x
TravisWhitaker Jun 19, 2025
3d3fc98
Make it build on WASM again.
TravisWhitaker Jun 19, 2025
90391c4
Clean up once more
TravisWhitaker Jun 19, 2025
2921d8c
Don't have to destructure boxed constants everywhere.
TravisWhitaker Jun 19, 2025
a89e220
yield in spinLock unhappy path
TravisWhitaker Jun 19, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ cabal.sandbox.config
examples/Group
*.sublime-*
dist-newstyle/
cabal.project.local
cabal.project.local~
122 changes: 93 additions & 29 deletions Data/Atomic.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{-# LANGUAGE BangPatterns, ForeignFunctionInterface #-}
{-# LANGUAGE BangPatterns
, CPP
, ForeignFunctionInterface
, MagicHash
, UnboxedTuples
#-}
-- | An atomic integer value. All operations are thread safe.
module Data.Atomic
(
Expand All @@ -12,53 +17,112 @@ module Data.Atomic
, subtract
) where

import Data.Int (Int64)
import Foreign.ForeignPtr (ForeignPtr, mallocForeignPtr, withForeignPtr)
import Foreign.Ptr (Ptr)
import Foreign.Storable (poke)
#include "MachDeps.h"
#ifndef SIZEOF_HSINT
#error "MachDeps.h didn't define SIZEOF_HSINT"
#endif

import Prelude hiding (read, subtract)

import GHC.Int

#if SIZEOF_HSINT == 8

-- 64-bit imports
import GHC.IO
import GHC.Prim

#else

-- 32-bit imports
import Data.IORef

#endif


-- 64-bit machine, Int ~ Int64, do it the fast way:
#if SIZEOF_HSINT == 8

#if MIN_VERSION_base(4,17,0)
int64ToInt :: Int64# -> Int#
int64ToInt = int64ToInt#

intToInt64 :: Int# -> Int64#
intToInt64 = intToInt64#
#else
int64ToInt :: Int# -> Int#
int64ToInt i = i

intToInt64 :: Int# -> Int#
intToInt64 i = i
#endif

-- | A mutable, atomic integer.
newtype Atomic = C (ForeignPtr Int64)
data Atomic = C (MutableByteArray# RealWorld)

-- | Create a new, zero initialized, atomic.
new :: Int64 -> IO Atomic
new n = do
fp <- mallocForeignPtr
withForeignPtr fp $ \ p -> poke p n
return $ C fp
new (I64# n64) = IO $ \s ->
case newByteArray# SIZEOF_HSINT# s of { (# s1, mba #) ->
case atomicWriteIntArray# mba 0# (int64ToInt n64) s1 of { s2 ->
(# s2, C mba #) }}

read :: Atomic -> IO Int64
read (C fp) = withForeignPtr fp cRead

foreign import ccall unsafe "hs_atomic_read" cRead :: Ptr Int64 -> IO Int64
read (C mba) = IO $ \s ->
case atomicReadIntArray# mba 0# s of { (# s1, n #) ->
(# s1, I64# (intToInt64 n) #)}

-- | Set the atomic to the given value.
write :: Atomic -> Int64 -> IO ()
write (C fp) n = withForeignPtr fp $ \ p -> cWrite p n
write (C mba) (I64# n64) = IO $ \s ->
case atomicWriteIntArray# mba 0# (int64ToInt n64) s of { s1 ->
(# s1, () #) }

foreign import ccall unsafe "hs_atomic_write" cWrite
:: Ptr Int64 -> Int64 -> IO ()
-- | Increase the atomic by the given amount.
add :: Atomic -> Int64 -> IO ()
add (C mba) (I64# n64) = IO $ \s ->
case fetchAddIntArray# mba 0# (int64ToInt n64) s of { (# s1, _ #) ->
(# s1, () #) }

-- | Increase the atomic by one.
inc :: Atomic -> IO ()
inc atomic = add atomic 1
-- | Decrease the atomic by the given amount.
subtract :: Atomic -> Int64 -> IO ()
subtract (C mba) (I64# n64) = IO $ \s ->
case fetchSubIntArray# mba 0# (int64ToInt n64) s of { (# s1, _ #) ->
(# s1, () #) }

-- | Decrease the atomic by one.
dec :: Atomic -> IO ()
dec atomic = subtract atomic 1
#else

-- 32-bit machine, Int ~ Int32, fall back to IORef. This could be replaced with
-- faster implementations for specific 32-bit machines in the future, but the
-- idea is to preserve 64-bit width for counters.

newtype Atomic = C (IORef Int64)

-- | Create a new, zero initialized, atomic.
new :: Int64 -> IO Atomic
new = fmap C . newIORef

read :: Atomic -> IO Int64
read (C ior) = readIORef ior

-- | Set the atomic to the given value.
write :: Atomic -> Int64 -> IO ()
write (C ior) !i = atomicWriteIORef ior i

-- | Increase the atomic by the given amount.
add :: Atomic -> Int64 -> IO ()
add (C fp) n = withForeignPtr fp $ \ p -> cAdd p n
add (C ior) !i = atomicModifyIORef' ior (\(!n) -> (n+i, ()))

-- | Decrease the atomic by the given amount.
subtract :: Atomic -> Int64 -> IO ()
subtract (C fp) n = withForeignPtr fp $ \ p -> cSubtract p n
subtract (C ior) !i = atomicModifyIORef' ior (\(!n) -> (n-i, ()))

-- | Increase the atomic by the given amount.
foreign import ccall unsafe "hs_atomic_add" cAdd :: Ptr Int64 -> Int64 -> IO ()
#endif

-- | Increase the atomic by the given amount.
foreign import ccall unsafe "hs_atomic_subtract" cSubtract
:: Ptr Int64 -> Int64 -> IO ()
-- | Increase the atomic by one.
inc :: Atomic -> IO ()
inc atomic = add atomic 1

-- | Decrease the atomic by one.
dec :: Atomic -> IO ()
dec atomic = subtract atomic 1
1 change: 0 additions & 1 deletion System/Metrics.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ module System.Metrics
, Value(..)
) where

import Control.Applicative ((<$>))
import Control.Monad (forM)
import Data.Int (Int64)
import qualified Data.IntMap.Strict as IM
Expand Down
Loading