Skip to content

Commit

Permalink
Use fromThreadId on GHC >=9.8 (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
amesgen authored Oct 11, 2023
1 parent 55d925e commit a21db49
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion effectful-core/src/Effectful/Internal/Utils.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ import Control.Exception
import Data.IORef
import Data.Primitive.ByteArray
import GHC.Conc.Sync (ThreadId(..))
import GHC.Exts (Addr#, Any, RealWorld, ThreadId#, unsafeCoerce#)
import GHC.Exts (Any, RealWorld)
import Unsafe.Coerce (unsafeCoerce)

#if MIN_VERSION_base(4,19,0)
import GHC.Conc.Sync (fromThreadId)
#else
import GHC.Exts (Addr#, ThreadId#, unsafeCoerce#)
#if __GLASGOW_HASKELL__ >= 904
import Data.Word
#else
import Foreign.C.Types
#endif
#endif

-- | Version of bracket with an INLINE pragma to work around
-- https://gitlab.haskell.org/ghc/ghc/-/issues/22824.
Expand All @@ -61,6 +66,9 @@ inlineBracket before after action = mask $ \unmask -> do

-- | Get an id of a thread that doesn't prevent its garbage collection.
weakThreadId :: ThreadId -> Int
#if MIN_VERSION_base(4,19,0)
weakThreadId = fromIntegral . fromThreadId
#else
weakThreadId (ThreadId t#) = fromIntegral $ rts_getThreadId (threadIdToAddr# t#)

foreign import ccall unsafe "rts_getThreadId"
Expand All @@ -84,6 +92,7 @@ foreign import ccall unsafe "rts_getThreadId"
-- The coercion is fine because GHC represents ThreadId# as a pointer.
threadIdToAddr# :: ThreadId# -> Addr#
threadIdToAddr# = unsafeCoerce#
#endif

----------------------------------------

Expand Down

0 comments on commit a21db49

Please sign in to comment.