summaryrefslogtreecommitdiff
path: root/libraries/base/GHC/Event/Unique.hs
blob: 0363af20683ed2e8147a4d78fff3e65d35ea11c2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE BangPatterns, CPP, GeneralizedNewtypeDeriving, MagicHash,
  NoImplicitPrelude, UnboxedTuples #-}

module GHC.Event.Unique
    (
      UniqueSource
    , Unique(..)
    , newSource
    , newUnique
    ) where

import GHC.Base
import GHC.Num(Num)
import GHC.Show(Show(..))

#include "MachDeps.h"

data UniqueSource = US (MutableByteArray# RealWorld)

newtype Unique = Unique { asInt :: Int }
    deriving (Eq, Ord, Num)

-- | @since 4.3.1.0
instance Show Unique where
    show = show . asInt

newSource :: IO UniqueSource
newSource = IO $ \s ->
  case newByteArray# size s of
    (# s', mba #) -> (# s', US mba #)
  where
    !(I# size) = SIZEOF_HSINT

newUnique :: UniqueSource -> IO Unique
newUnique (US mba) = IO $ \s ->
  case fetchAddIntArray# mba 0# 1# s of
    (# s', a #) -> (# s', Unique (I# a) #)
{-# INLINE newUnique #-}