module LLVM.Internal.Context where

import LLVM.Prelude

import Control.Exception
import Control.Concurrent

import Foreign.Ptr

import qualified LLVM.Internal.FFI.Context as FFI

-- | a Context object holds the state the of LLVM system needs for one thread of
-- | LLVM compilation. Once upon a time, in early versions of LLVM, this state was global.
-- | Then it got packed up in this object to allow multiple threads to compile at once.
data Context = Context (Ptr FFI.Context)

-- | Create a Context, run an action (to which it is provided), then destroy the Context.
withContext :: (Context -> IO a) -> IO a
withContext :: (Context -> IO a) -> IO a
withContext = IO a -> IO a
forall a. IO a -> IO a
runBound (IO a -> IO a)
-> ((Context -> IO a) -> IO a) -> (Context -> IO a) -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. IO (Ptr Context)
-> (Ptr Context -> IO ()) -> (Ptr Context -> IO a) -> IO a
forall a b c. IO a -> (a -> IO b) -> (a -> IO c) -> IO c
bracket IO (Ptr Context)
FFI.contextCreate Ptr Context -> IO ()
FFI.contextDispose ((Ptr Context -> IO a) -> IO a)
-> ((Context -> IO a) -> Ptr Context -> IO a)
-> (Context -> IO a)
-> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((Context -> IO a)
-> (Ptr Context -> Context) -> Ptr Context -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Ptr Context -> Context
Context)
  where runBound :: IO a -> IO a
runBound = if Bool
rtsSupportsBoundThreads then IO a -> IO a
forall a. IO a -> IO a
runInBoundThread else IO a -> IO a
forall a. a -> a
id