@@ -21,7 +21,7 @@ import Data.Time
2121import Data.Bits
2222import Data.Word
2323
24−import Data.IORef
24+import Control.Concurrent.MVar
2525import System.IO
2626import System.IO.Unsafe
2727
@@ -65,29 +65,27 @@ type instance ByteSink MACSource g = Takes3Bytes (Takes3Bytes g)
6565-- the clock value on initialization.
6666stepTime :: IO (Maybe (Word16, Word64))
6767stepTime = do
68− State c0 h0 <- readIORef state
6968 h1 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
70− if h1 > h0
71− then do
72− writeIORef state $ State c0 h1
73− return $ Just (c0, h1)
74− else do
69+ modifyMVar state $ \s@(State c0 h0) ->
70+ if h1 > h0
71+ then
72+ return (State c0 h1, Just (c0, h1))
73+ else
7574 let
7675 c1 = succ c0
77− if c1 <= 0x3fff -- when clock is initially randomized,
76+ in if c1 <= 0x3fff -- when clock is initially randomized,
7877 -- then this test will need to change
79− then do
80− writeIORef state $ State c1 h1
81− return $ Just (c1, h1)
82− else do
83− return Nothing
78+ then
79+ return (State c1 h1, Just (c1, h1))
80+ else
81+ return (s, Nothing)
8482
8583
8684{-# NOINLINE state #-}
87−state :: IORef State
85+state :: MVar State
8886state = unsafePerformIO $ do
8987 h0 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
90− newIORef $ State 0 h0 -- the 0 should be a random number
88+ newMVar $ State 0 h0 -- the 0 should be a random number
9189
9290
9391data State = State