{-# 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) = Int -> Int
forall b. FiniteBits b => b -> Int
finiteBitSize (Int
0 :: Int) Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
3
  IntVar
iv <- (State# RealWorld -> (# State# RealWorld, IntVar #)) -> IO IntVar
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, IntVar #)) -> IO IntVar)
-> (State# RealWorld -> (# State# RealWorld, IntVar #))
-> IO IntVar
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#
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
  IntVar -> IO IntVar
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return IntVar
iv

readIntVar :: IntVar -> IO Int
readIntVar :: IntVar -> IO Int
readIntVar (IntVar MutableByteArray# RealWorld
mba) = (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int)
-> (State# RealWorld -> (# State# RealWorld, Int #)) -> IO Int
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
  case MutableByteArray# RealWorld
-> Int# -> State# RealWorld -> (# State# RealWorld, Int# #)
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) = (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
IO ((State# RealWorld -> (# State# RealWorld, () #)) -> IO ())
-> (State# RealWorld -> (# State# RealWorld, () #)) -> IO ()
forall a b. (a -> b) -> a -> b
$ \State# RealWorld
s ->
  case MutableByteArray# RealWorld
-> Int# -> Int# -> State# RealWorld -> State# RealWorld
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', () #)