{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP, MagicHash, NoImplicitPrelude, UnboxedTuples #-}

module GHC.Event.IntVar
    ( IntVar
    , newIntVar
    , readIntVar
    , writeIntVar
    ) where

import GHC.Base
import GHC.Bits

data IntVar = IntVar (MutableByteArray# RealWorld)

newIntVar :: Int -> IO IntVar
newIntVar :: Int -> IO IntVar
newIntVar Int
n = do
  let !(I# Int#
size) = forall b. FiniteBits b => b -> Int
finiteBitSize (Int
0 :: Int) forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
3
  IntVar
iv <- forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
    case forall d. Int# -> State# d -> (# State# d, MutableByteArray# d #)
newByteArray# Int#
size State# RealWorld
s of
      (# State# RealWorld
s', MutableByteArray# RealWorld
mba #) -> (# State# RealWorld
s', MutableByteArray# RealWorld -> IntVar
IntVar MutableByteArray# RealWorld
mba #)
  IntVar -> Int -> IO ()
writeIntVar IntVar
iv Int
n
  forall (m :: * -> *) a. Monad m => a -> m a
return IntVar
iv

readIntVar :: IntVar -> IO Int
readIntVar :: IntVar -> IO Int
readIntVar (IntVar MutableByteArray# RealWorld
mba) = forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
  case forall d.
MutableByteArray# d -> Int# -> State# d -> (# State# d, Int# #)
readIntArray# MutableByteArray# RealWorld
mba Int#
0# State# RealWorld
s of
    (# State# RealWorld
s', Int#
n #) -> (# State# RealWorld
s', Int# -> Int
I# Int#
n #)

writeIntVar :: IntVar -> Int -> IO ()
writeIntVar :: IntVar -> Int -> IO ()
writeIntVar (IntVar MutableByteArray# RealWorld
mba) (I# Int#
n) = forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
  case forall d.
MutableByteArray# d -> Int# -> Int# -> State# d -> State# d
writeIntArray# MutableByteArray# RealWorld
mba Int#
0# Int#
n State# RealWorld
s of
    State# RealWorld
s' -> (# State# RealWorld
s', () #)