-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(runtime): use atomic counter instead of ioref sequence number
- Loading branch information
1 parent
38932e9
commit 573487b
Showing
12 changed files
with
145 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
86 changes: 86 additions & 0 deletions
86
src/runtime-prototype/src/Disruptor/AtomicCounterPadded.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE MagicHash #-} | ||
{-# LANGUAGE UnboxedTuples #-} | ||
|
||
-- Inspired by: | ||
-- * https://github.com/jberryman/unagi-chan/blob/master/src/Data/Atomics/Counter/Fat.hs | ||
-- * https://hackage.haskell.org/package/atomic-primops-0.8.4/docs/src/Data.Atomics.Counter.html | ||
-- * https://hackage.haskell.org/package/unboxed-ref-0.4.0.0/docs/src/Data-STRef-Unboxed-Internal.html#STRefU | ||
-- * https://hackage.haskell.org/package/ghc-8.10.2/docs/src/FastMutInt.html#FastMutInt | ||
|
||
module Disruptor.AtomicCounterPadded | ||
( AtomicCounter() | ||
, newCounter | ||
, incrCounter | ||
, incrCounter_ | ||
, decrCounter | ||
, decrCounter_ | ||
, readCounter | ||
, casCounter | ||
) where | ||
|
||
import Control.Monad.Primitive (RealWorld) | ||
import Data.Bits (finiteBitSize) | ||
import GHC.Exts | ||
import GHC.Types | ||
|
||
------------------------------------------------------------------------ | ||
|
||
data AtomicCounter = AtomicCounter !(MutableByteArray# RealWorld) | ||
|
||
sIZEOF_CACHELINE :: Int | ||
sIZEOF_CACHELINE = 64 | ||
{-# INLINE sIZEOF_CACHELINE #-} | ||
-- ^ TODO: See | ||
-- https://github.com/NickStrupat/CacheLineSize/blob/93a57c094f71a2796714f7a28d74dd8776149193/CacheLineSize.c | ||
-- for how to get the cache line size on Windows, MacOS and Linux. | ||
|
||
-- | Create a new atomic counter padded with 64-bytes (an x86 cache line) to try | ||
-- to avoid false sharing. | ||
newCounter :: Int -> IO AtomicCounter | ||
newCounter (I# n) = IO $ \s -> | ||
case newAlignedPinnedByteArray# size alignment s of | ||
(# s', arr #) -> case writeIntArray# arr 0# n s' of | ||
s'' -> (# s'', AtomicCounter arr #) | ||
where | ||
!(I# size) = finiteBitSize (0 :: Int) | ||
!(I# alignment) = sIZEOF_CACHELINE | ||
{-# INLINE newCounter #-} | ||
|
||
incrCounter :: Int -> AtomicCounter -> IO Int | ||
incrCounter (I# incr) (AtomicCounter arr) = IO $ \s -> | ||
case fetchAddIntArray# arr 0# incr s of | ||
(# s', i #) -> (# s', I# (i +# incr) #) | ||
{-# INLINE incrCounter #-} | ||
|
||
incrCounter_ :: Int -> AtomicCounter -> IO () | ||
incrCounter_ (I# incr) (AtomicCounter arr) = IO $ \s -> | ||
case fetchAddIntArray# arr 0# incr s of | ||
(# s', _i #) -> (# s', () #) | ||
{-# INLINE incrCounter_ #-} | ||
|
||
decrCounter :: Int -> AtomicCounter -> IO Int | ||
decrCounter (I# decr) (AtomicCounter arr) = IO $ \s -> | ||
case fetchSubIntArray# arr 0# decr s of | ||
(# s', i #) -> (# s', I# (i -# decr) #) | ||
{-# INLINE decrCounter #-} | ||
|
||
decrCounter_ :: Int -> AtomicCounter -> IO () | ||
decrCounter_ (I# decr) (AtomicCounter arr) = IO $ \s -> | ||
case fetchSubIntArray# arr 0# decr s of | ||
(# s', _i #) -> (# s', () #) | ||
{-# INLINE decrCounter_ #-} | ||
|
||
readCounter :: AtomicCounter -> IO Int | ||
readCounter (AtomicCounter arr) = IO $ \s -> | ||
case readIntArray# arr 0# s of | ||
(# s', i #) -> (# s', I# i #) | ||
{-# INLINE readCounter #-} | ||
|
||
casCounter :: AtomicCounter -> Int -> Int -> IO Bool | ||
casCounter (AtomicCounter arr) (I# old) (I# new) = IO $ \s -> | ||
case casIntArray# arr 0# old new s of | ||
(# s', before #) -> case before ==# old of | ||
1# -> (# s', True #) | ||
0# -> (# s', False #) | ||
{-# INLINE casCounter #-} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.