Skip to content

Instantly share code, notes, and snippets.

@dminuoso
Last active March 21, 2023 00:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dminuoso/e17a7de0657cb707078176bdb081b806 to your computer and use it in GitHub Desktop.
Save dminuoso/e17a7de0657cb707078176bdb081b806 to your computer and use it in GitHub Desktop.
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE UnliftedFFITypes #-}
module Modgud.Unsafe
( threadId
)
where
import GHC.Exts (ThreadId#, myThreadId#)
import Data.Word (Word64)
import Foreign.C.Types (CULLong(..))
import GHC.IO (IO(..))
foreign import ccall unsafe "rts_getThreadId" getThreadId :: ThreadId# -> CULLong
-- | Convenience version of 'getThreadId' that we can pretty print without roundtripping through Show.
threadId :: IO Word64
threadId = IO $ \s ->
case myThreadId# s of
(# s1, tid #) -> case getThreadId tid of
CULLong ll -> (# s1, ll #)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment