summaryrefslogtreecommitdiff
path: root/src/Compile/Exec.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Compile/Exec.hs')
-rw-r--r--src/Compile/Exec.hs11
1 files changed, 9 insertions, 2 deletions
diff --git a/src/Compile/Exec.hs b/src/Compile/Exec.hs
index 7c5cb79..487ed8a 100644
--- a/src/Compile/Exec.hs
+++ b/src/Compile/Exec.hs
@@ -14,6 +14,7 @@ import Foreign.Ptr (FunPtr)
import System.Directory (removeDirectoryRecursive)
import System.Environment (lookupEnv)
import System.IO (hPutStrLn, stderr)
+import System.IO.Unsafe (unsafePerformIO)
import System.Posix.DynamicLinker
import System.Posix.Temp (mkdtemp)
import System.Process (readProcess)
@@ -31,14 +32,16 @@ buildKernel csource funnames = do
let args = ["-O3", "-march=native", "-shared", "-fPIC", "-std=c99", "-x", "c", "-o", outso, "-", "-Wall", "-Wextra", "-Wno-unused-parameter"]
_ <- readProcess "gcc" args csource
- hPutStrLn stderr $ "[chad] loading kernel " ++ path
+ numLoaded <- atomicModifyIORef' numLoadedCounter (\n -> (n+1, n+1))
+ hPutStrLn stderr $ "[chad] loading kernel " ++ path ++ " (" ++ show numLoaded ++ " total)"
dl <- dlopen outso [RTLD_LAZY, RTLD_LOCAL]
removeDirectoryRecursive path -- we keep a reference anyway because we have the file open now
ptrs <- Map.fromList <$> sequence [(name,) <$> dlsym dl name | name <- funnames]
ref <- newIORef ptrs
- _ <- mkWeakIORef ref (do hPutStrLn stderr $ "[chad] unloading kernel " ++ path
+ _ <- mkWeakIORef ref (do numLeft <- atomicModifyIORef' numLoadedCounter (\n -> (n-1, n-1))
+ hPutStrLn stderr $ "[chad] unloading kernel " ++ path ++ " (" ++ show numLeft ++ " left)"
dlclose dl)
return (KernelLib ref)
@@ -57,3 +60,7 @@ getTempDir =
lookupEnv "TMPDIR" >>= \case
Just s | not (null s) -> return s
_ -> return "/tmp"
+
+{-# NOINLINE numLoadedCounter #-}
+numLoadedCounter :: IORef Int
+numLoadedCounter = unsafePerformIO $ newIORef 0