-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6acbd4
commit 1f03db5
Showing
6 changed files
with
65 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
.envrc | ||
.ghc.environment.* | ||
Session.vim | ||
cabal.project.local | ||
dist/ | ||
dist-newstyle/ | ||
dist/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,57 @@ | ||
-- Some code modified from the atomic-primops library; license included below. | ||
-- | ||
-- Copyright (c)2012-2013, Ryan R. Newton | ||
-- | ||
-- All rights reserved. | ||
-- | ||
-- Redistribution and use in source and binary forms, with or without | ||
-- modification, are permitted provided that the following conditions are met: | ||
-- | ||
-- * Redistributions of source code must retain the above copyright | ||
-- notice, this list of conditions and the following disclaimer. | ||
-- | ||
-- * Redistributions in binary form must reproduce the above | ||
-- copyright notice, this list of conditions and the following | ||
-- disclaimer in the documentation and/or other materials provided | ||
-- with the distribution. | ||
-- | ||
-- * Neither the name of Ryan R. Newton nor the names of other | ||
-- contributors may be used to endorse or promote products derived | ||
-- from this software without specific prior written permission. | ||
{-# LANGUAGE MagicHash #-} | ||
{-# LANGUAGE UnboxedTuples #-} | ||
|
||
module TimerWheel.Internal.Supply | ||
( Supply, | ||
new, | ||
next, | ||
( Counter, | ||
newCounter, | ||
incrCounter, | ||
) | ||
where | ||
|
||
import Data.Atomics.Counter (AtomicCounter, incrCounter, newCounter) | ||
import Data.Coerce (coerce) | ||
import Data.Bits | ||
import GHC.Base | ||
|
||
newtype Supply | ||
= Supply AtomicCounter | ||
-- | A thread-safe counter implemented with atomic fetch-and-add. | ||
data Counter | ||
= Counter (MutableByteArray# RealWorld) | ||
|
||
new :: IO Supply | ||
new = | ||
coerce (newCounter 0) | ||
-- | Create a new counter initialized to 0. | ||
newCounter :: IO Counter | ||
newCounter = | ||
IO \s0# -> | ||
case newByteArray# size s0# of | ||
(# s1#, arr# #) -> | ||
case writeIntArray# arr# 0# 0# s1# of | ||
s2# -> (# s2#, Counter arr# #) | ||
where | ||
!(I# size) = | ||
finiteBitSize (undefined :: Int) `div` 8 | ||
{-# INLINE newCounter #-} | ||
|
||
next :: Supply -> IO Int | ||
next = | ||
coerce (incrCounter 1) | ||
{-# INLINE next #-} | ||
-- | Increment a counter and return the value prior to incrementing. | ||
incrCounter :: Counter -> IO Int | ||
incrCounter (Counter arr#) = | ||
IO \s0# -> | ||
case fetchAddIntArray# arr# 0# 1# s0# of | ||
(# s1#, n# #) -> (# s1#, I# n# #) | ||
{-# INLINE incrCounter #-} |
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