{-# LANGUAGE CPP #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE Trustworthy #-}
module Control.Concurrent.Unique
( Unique, newUnique
) where
import Data.Hashable (Hashable (..))
import GHC.IO
import GHC.Exts
data Unique = Unique !Int (MutableByteArray# RealWorld)
instance Eq Unique where
#if MIN_VERSION_base(4,7,0)
Unique Int
_ MutableByteArray# RealWorld
p == :: Unique -> Unique -> Bool
== Unique Int
_ MutableByteArray# RealWorld
q = Int# -> Bool
isTrue# (MutableByteArray# RealWorld -> MutableByteArray# RealWorld -> Int#
forall d. MutableByteArray# d -> MutableByteArray# d -> Int#
sameMutableByteArray# MutableByteArray# RealWorld
p MutableByteArray# RealWorld
q)
#else
Unique _ p == Unique _ q = sameMutableByteArray# p q
#endif
instance Hashable Unique where
hash :: Unique -> Int
hash (Unique Int
i MutableByteArray# RealWorld
_) = Int
i
hashWithSalt :: Int -> Unique -> Int
hashWithSalt Int
d (Unique Int
i MutableByteArray# RealWorld
_) = Int -> Int -> Int
forall a. Hashable a => Int -> a -> Int
hashWithSalt Int
d Int
i
newUnique :: IO Unique
newUnique :: IO Unique
newUnique = (State# RealWorld -> (# State# RealWorld, Unique #)) -> IO Unique
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Unique #)) -> IO Unique)
-> (State# RealWorld -> (# State# RealWorld, Unique #))
-> IO Unique
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s -> case Int#
-> State# RealWorld
-> (# State# RealWorld, MutableByteArray# RealWorld #)
forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
0# State# RealWorld
s of
(# State# RealWorld
s', MutableByteArray# RealWorld
ba #) -> (# State# RealWorld
s', Int -> MutableByteArray# RealWorld -> Unique
Unique (Int# -> Int
I# (Addr# -> Int#
addr2Int# (MutableByteArray# RealWorld -> Addr#
unsafeCoerce# MutableByteArray# RealWorld
ba))) MutableByteArray# RealWorld
ba #)