-- Tests ForeignPtrEnv finalizers

import Text.Printf
import Foreign.ForeignPtr
import Foreign
import GHC.TopHandler
import Control.Concurrent
import Data.List

foreign export ccall fin :: Ptr Int -> Ptr Int -> IO ()
foreign import ccall "&fin" finptr :: FinalizerEnvPtr Int Int

fin :: Ptr Int -> Ptr Int -> IO ()
fin envp ap = runIO $ do
  env <- peek envp
  a <- peek ap
  printf "%d %d\n" env a
  return ()

main = do
  a   <- new (55 :: Int)
  env <- new (66 :: Int)
  fp  <- newForeignPtrEnv finptr env a
  foldl' (+) 0 [1..500000] `seq` return ()

-- Tests ForeignPtrEnv finalizers

import Text.Printf
import Foreign.ForeignPtr
import Foreign
import GHC.TopHandler
import Control.Concurrent
import Data.List

foreign export ccall fin :: Ptr Int -> Ptr Int -> IO ()
foreign import ccall "&fin" finptr :: FinalizerEnvPtr Int Int

fin :: Ptr Int -> Ptr Int -> IO ()
fin envp ap = runIO $ do
  env <- peek envp
  a <- peek ap
  printf "%d %d\n" env a
  return ()

main = do
  a   <- new (55 :: Int)
  env <- new (66 :: Int)
  fp  <- newForeignPtrEnv finptr env a
  foldl' (+) 0 [1..1000000] `seq` return ()

Fix this test on a fast machine The artificial loop wasn't running for long enough for a context switch to happen, so the finalizer wasn't getting to run early enough.
Fix this test on a fast machine
The artificial loop wasn't running for long enough for a context
switch to happen, so the finalizer wasn't getting to run early enough.
