{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnliftedFFITypes #-}
{-# OPTIONS_HADDOCK not-home #-}
module Effectful.Internal.Utils
( inlineBracket
, weakThreadId
, eqThreadId
, Any
, toAny
, fromAny
, IORef'
, newIORef'
, readIORef'
, writeIORef'
, MVar'
, toMVar'
, newMVar'
, readMVar'
, modifyMVar'
, modifyMVar_'
, Unique
, newUnique
) where
import Control.Concurrent.MVar
import Control.Exception
import Data.IORef
import Data.Primitive.ByteArray
import GHC.Conc.Sync (ThreadId(..))
import GHC.Exts (Addr#, Any, RealWorld, ThreadId#, unsafeCoerce#)
import Unsafe.Coerce (unsafeCoerce)
#if __GLASGOW_HASKELL__ >= 904
import Data.Word
#else
import Foreign.C.Types
#endif
inlineBracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c
inlineBracket :: forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
inlineBracket IO a
before a -> IO b
after a -> IO c
action = forall b. ((forall a. IO a -> IO a) -> IO b) -> IO b
mask forall a b. (a -> b) -> a -> b
$ \forall a. IO a -> IO a
unmask -> do
a
a <- IO a
before
c
r <- forall a. IO a -> IO a
unmask (a -> IO c
action a
a) forall a b. IO a -> IO b -> IO a
`onException` a -> IO b
after a
a
b
_ <- a -> IO b
after a
a
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure c
r
{-# INLINE inlineBracket #-}
weakThreadId :: ThreadId -> Int
weakThreadId :: ThreadId -> Int
weakThreadId (ThreadId ThreadId#
t#) = forall a b. (Integral a, Num b) => a -> b
fromIntegral forall a b. (a -> b) -> a -> b
$ Addr# -> CLong
rts_getThreadId (ThreadId# -> Addr#
threadIdToAddr# ThreadId#
t#)
foreign import ccall unsafe "rts_getThreadId"
#if __GLASGOW_HASKELL__ >= 904
rts_getThreadId :: Addr# -> Word64
#elif __GLASGOW_HASKELL__ >= 900
rts_getThreadId :: Addr# -> CLong
#else
rts_getThreadId :: Addr# -> CInt
#endif
threadIdToAddr# :: ThreadId# -> Addr#
threadIdToAddr# :: ThreadId# -> Addr#
threadIdToAddr# = unsafeCoerce# :: forall a b. a -> b
unsafeCoerce#
#if __GLASGOW_HASKELL__ < 900
eqThreadId :: ThreadId -> ThreadId -> Bool
eqThreadId (ThreadId t1#) (ThreadId t2#) =
eq_thread (threadIdToAddr# t1#) (threadIdToAddr# t2#) == 1
foreign import ccall unsafe "effectful_eq_thread"
eq_thread :: Addr# -> Addr# -> CLong
#else
eqThreadId :: ThreadId -> ThreadId -> Bool
eqThreadId :: ThreadId -> ThreadId -> Bool
eqThreadId = forall a. Eq a => a -> a -> Bool
(==)
#endif
toAny :: a -> Any
toAny :: forall a. a -> Any
toAny = forall a b. a -> b
unsafeCoerce
fromAny :: Any -> a
fromAny :: forall a. Any -> a
fromAny = forall a b. a -> b
unsafeCoerce
newtype IORef' a = IORef' (IORef a)
deriving IORef' a -> IORef' a -> Bool
forall a. IORef' a -> IORef' a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: IORef' a -> IORef' a -> Bool
$c/= :: forall a. IORef' a -> IORef' a -> Bool
== :: IORef' a -> IORef' a -> Bool
$c== :: forall a. IORef' a -> IORef' a -> Bool
Eq
newIORef' :: a -> IO (IORef' a)
newIORef' :: forall a. a -> IO (IORef' a)
newIORef' a
a = a
a seq :: forall a b. a -> b -> b
`seq` (forall a. IORef a -> IORef' a
IORef' forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. a -> IO (IORef a)
newIORef a
a)
readIORef' :: IORef' a -> IO a
readIORef' :: forall a. IORef' a -> IO a
readIORef' (IORef' IORef a
var) = forall a. IORef a -> IO a
readIORef IORef a
var
writeIORef' :: IORef' a -> a -> IO ()
writeIORef' :: forall a. IORef' a -> a -> IO ()
writeIORef' (IORef' IORef a
var) a
a = a
a seq :: forall a b. a -> b -> b
`seq` forall a. IORef a -> a -> IO ()
writeIORef IORef a
var a
a
newtype MVar' a = MVar' (MVar a)
deriving MVar' a -> MVar' a -> Bool
forall a. MVar' a -> MVar' a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MVar' a -> MVar' a -> Bool
$c/= :: forall a. MVar' a -> MVar' a -> Bool
== :: MVar' a -> MVar' a -> Bool
$c== :: forall a. MVar' a -> MVar' a -> Bool
Eq
toMVar' :: MVar a -> IO (MVar' a)
toMVar' :: forall a. MVar a -> IO (MVar' a)
toMVar' MVar a
var = do
let var' :: MVar' a
var' = forall a. MVar a -> MVar' a
MVar' MVar a
var
forall a. MVar' a -> (a -> IO a) -> IO ()
modifyMVar_' MVar' a
var' forall (f :: Type -> Type) a. Applicative f => a -> f a
pure
forall (f :: Type -> Type) a. Applicative f => a -> f a
pure MVar' a
var'
newMVar' :: a -> IO (MVar' a)
newMVar' :: forall a. a -> IO (MVar' a)
newMVar' a
a = a
a seq :: forall a b. a -> b -> b
`seq` (forall a. MVar a -> MVar' a
MVar' forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall a. a -> IO (MVar a)
newMVar a
a)
readMVar' :: MVar' a -> IO a
readMVar' :: forall a. MVar' a -> IO a
readMVar' (MVar' MVar a
var) = forall a. MVar a -> IO a
readMVar MVar a
var
modifyMVar' :: MVar' a -> (a -> IO (a, r)) -> IO r
modifyMVar' :: forall a r. MVar' a -> (a -> IO (a, r)) -> IO r
modifyMVar' (MVar' MVar a
var) a -> IO (a, r)
action = forall a b. MVar a -> (a -> IO (a, b)) -> IO b
modifyMVar MVar a
var forall a b. (a -> b) -> a -> b
$ \a
a0 -> do
(a
a, r
r) <- a -> IO (a, r)
action a
a0
a
a seq :: forall a b. a -> b -> b
`seq` forall (f :: Type -> Type) a. Applicative f => a -> f a
pure (a
a, r
r)
modifyMVar_' :: MVar' a -> (a -> IO a) -> IO ()
modifyMVar_' :: forall a. MVar' a -> (a -> IO a) -> IO ()
modifyMVar_' (MVar' MVar a
var) a -> IO a
action = forall a. MVar a -> (a -> IO a) -> IO ()
modifyMVar_ MVar a
var forall a b. (a -> b) -> a -> b
$ \a
a0 -> do
a
a <- a -> IO a
action a
a0
a
a seq :: forall a b. a -> b -> b
`seq` forall (f :: Type -> Type) a. Applicative f => a -> f a
pure a
a
newtype Unique = Unique (MutableByteArray RealWorld)
instance Eq Unique where
Unique MutableByteArray RealWorld
a == :: Unique -> Unique -> Bool
== Unique MutableByteArray RealWorld
b = forall s. MutableByteArray s -> MutableByteArray s -> Bool
sameMutableByteArray MutableByteArray RealWorld
a MutableByteArray RealWorld
b
newUnique :: IO Unique
newUnique :: IO Unique
newUnique = MutableByteArray RealWorld -> Unique
Unique forall (f :: Type -> Type) a b. Functor f => (a -> b) -> f a -> f b
<$> forall (m :: Type -> Type).
PrimMonad m =>
Int -> m (MutableByteArray (PrimState m))
newByteArray Int
0