on master help sign in
switch to MVar in .V1
473e034Antoine Latter
Data/UUID/V1.hs+13−15
@@ -21,7 +21,7 @@ import Data.Time
21import Data.Bits
21import Data.Bits
22import Data.Word
22import Data.Word
23
23
24−import Data.IORef
24+import Control.Concurrent.MVar
25import System.IO
25import System.IO
26import System.IO.Unsafe
26import System.IO.Unsafe
27
27
@@ -65,29 +65,27 @@ type instance ByteSink MACSource g = Takes3Bytes (Takes3Bytes g)
65-- the clock value on initialization.
65-- the clock value on initialization.
66stepTime :: IO (Maybe (Word16, Word64))
66stepTime :: IO (Maybe (Word16, Word64))
67stepTime = do
67stepTime = do
68− State c0 h0 <- readIORef state
69 h1 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
68 h1 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
69+ modifyMVar state $ \s@(State c0 h0) ->
70− if h1 > h0
70+ if h1 > h0
71− then do
71+ then
72− writeIORef state $ State c0 h1
73− return $ Just (c0, h1)
72+ return (State c0 h1, Just (c0, h1))
74− else do
73+ else
75 let
74 let
76 c1 = succ c0
75 c1 = succ c0
77− if c1 <= 0x3fff -- when clock is initially randomized,
76+ in if c1 <= 0x3fff -- when clock is initially randomized,
78 -- then this test will need to change
77 -- then this test will need to change
79− then do
78+ then
80− writeIORef state $ State c1 h1
81− return $ Just (c1, h1)
79+ return (State c1 h1, Just (c1, h1))
82− else do
80+ else
83− return Nothing
81+ return (s, Nothing)
84
82
85
83
86{-# NOINLINE state #-}
84{-# NOINLINE state #-}
87−state :: IORef State
85+state :: MVar State
88state = unsafePerformIO $ do
86state = unsafePerformIO $ do
89 h0 <- fmap hundredsOfNanosSinceGregorianReform getCurrentTime
87 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
91
89
92
90
93data State = State
91data State = State