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

    module Control.Concurrent.Raw ( rawForkIO, rawForkOn ) where

    import Data.Function ( ($) )
    import GHC.IO        ( IO(IO) )
    import GHC.Exts      ( Int(I#), fork#, forkOn# )
    import GHC.Conc      ( ThreadId(ThreadId) )

    -- A version of forkIO that does not include the outer exception
    -- handler: saves a bit of time when we will be installing our own
    -- exception handler.
    {-# INLINE rawForkIO #-}
    rawForkIO :: IO () -> IO ThreadId
    #if MIN_VERSION_base(4,17,0)
    rawForkIO :: IO () -> IO ThreadId
    rawForkIO (IO State# RealWorld -> (# State# RealWorld, () #)
    action) = (State# RealWorld -> (# State# RealWorld, ThreadId #))
    -> IO ThreadId
    forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
    IO ((State# RealWorld -> (# State# RealWorld, ThreadId #))
     -> IO ThreadId)
    -> (State# RealWorld -> (# State# RealWorld, ThreadId #))
    -> IO ThreadId
    forall a b. (a -> b) -> a -> b
    $ \State# RealWorld
    s ->
    #else
    rawForkIO action = IO $ \s ->
    #endif
       case ((State# RealWorld -> (# State# RealWorld, () #))
    -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
    forall a.
    (State# RealWorld -> (# State# RealWorld, a #))
    -> State# RealWorld -> (# State# RealWorld, ThreadId# #)
    fork# State# RealWorld -> (# State# RealWorld, () #)
    action State# RealWorld
    s) of (# State# RealWorld
    s1, ThreadId#
    tid #) -> (# State# RealWorld
    s1, ThreadId# -> ThreadId
    ThreadId ThreadId#
    tid #)

    {-# INLINE rawForkOn #-}
    rawForkOn :: Int -> IO () -> IO ThreadId
    #if MIN_VERSION_base(4,17,0)
    rawForkOn :: Int -> IO () -> IO ThreadId
    rawForkOn (I# Int#
    cpu) (IO State# RealWorld -> (# State# RealWorld, () #)
    action) = (State# RealWorld -> (# State# RealWorld, ThreadId #))
    -> IO ThreadId
    forall a. (State# RealWorld -> (# State# RealWorld, a #)) -> IO a
    IO ((State# RealWorld -> (# State# RealWorld, ThreadId #))
     -> IO ThreadId)
    -> (State# RealWorld -> (# State# RealWorld, ThreadId #))
    -> IO ThreadId
    forall a b. (a -> b) -> a -> b
    $ \State# RealWorld
    s ->
    #else
    rawForkOn (I# cpu) action = IO $ \s ->
    #endif
       case (Int#
    -> (State# RealWorld -> (# State# RealWorld, () #))
    -> State# RealWorld
    -> (# State# RealWorld, ThreadId# #)
    forall a.
    Int#
    -> (State# RealWorld -> (# State# RealWorld, a #))
    -> State# RealWorld
    -> (# State# RealWorld, ThreadId# #)
    forkOn# Int#
    cpu State# RealWorld -> (# State# RealWorld, () #)
    action State# RealWorld
    s) of (# State# RealWorld
    s1, ThreadId#
    tid #) -> (# State# RealWorld
    s1, ThreadId# -> ThreadId
    ThreadId ThreadId#
    tid #)