1 {-# LANGUAGE BangPatterns, GeneralizedNewtypeDeriving, NoImplicitPrelude #-}
2 module System.Event.Unique
10 import Data.Int (Int64)
12 import GHC.Conc.Sync (TVar, atomically, newTVarIO, readTVar, writeTVar)
13 import GHC.Num (Num(..))
14 import GHC.Show (Show(..))
16 -- We used to use IORefs here, but Simon switched us to STM when we
17 -- found that our use of atomicModifyIORef was subject to a severe RTS
18 -- performance problem when used in a tight loop from multiple
19 -- threads: http://hackage.haskell.org/trac/ghc/ticket/3838
21 -- There seems to be no performance cost to using a TVar instead.
23 newtype UniqueSource = US (TVar Int64)
25 newtype Unique = Unique { asInt64 :: Int64 }
26 deriving (Eq, Ord, Num)
28 instance Show Unique where
31 newSource :: IO UniqueSource
32 newSource = US `fmap` newTVarIO 0
34 newUnique :: UniqueSource -> IO Unique
35 newUnique (US ref) = atomically $ do
40 {-# INLINE newUnique #-}