Remove Control.Parallel*, now in package parallel
[haskell-directory.git] / Foreign / ForeignPtr.hs
index 53275c1..21485db 100644 (file)
@@ -20,14 +20,14 @@ module Foreign.ForeignPtr
        -- * Finalised data pointers
          ForeignPtr
        , FinalizerPtr
-#ifdef __HUGS__
+#if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
        , FinalizerEnvPtr
 #endif
        -- ** Basic operations
         , newForeignPtr
         , newForeignPtr_
         , addForeignPtrFinalizer
-#ifdef __HUGS__
+#if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
        , newForeignPtrEnv
        , addForeignPtrFinalizerEnv
 #endif
@@ -105,8 +105,9 @@ newForeignPtr :: FinalizerPtr a -> Ptr a -> IO (ForeignPtr a)
 -- after the last reference to the foreign object is dropped.  Note that there
 -- is no guarantee on how soon the finaliser is executed after the last
 -- reference was dropped; this depends on the details of the Haskell storage
--- manager. The only guarantee is that the finaliser runs before the program
--- terminates.
+-- manager.  Indeed, there is no guarantee that the finalizer is executed at
+-- all; a program may exit with finalizers outstanding.  (This is true
+-- of GHC, other implementations may give stronger guarantees).
 newForeignPtr finalizer p
   = do fObj <- newForeignPtr_ p
        addForeignPtrFinalizer finalizer fObj
@@ -138,7 +139,7 @@ withForeignPtr fo io
        return r
 #endif /* ! __NHC__ */
 
-#ifdef __HUGS__
+#if defined(__HUGS__) || defined(__GLASGOW_HASKELL__)
 -- | This variant of 'newForeignPtr' adds a finalizer that expects an
 -- environment in addition to the finalized pointer.  The environment
 -- that will be passed to the finalizer is fixed by the second argument to
@@ -151,6 +152,24 @@ newForeignPtrEnv finalizer env p
        return fObj
 #endif /* __HUGS__ */
 
+#ifdef __GLASGOW_HASKELL__
+type FinalizerEnvPtr env a = FunPtr (Ptr env -> Ptr a -> IO ())
+
+-- | like 'addForeignPtrFinalizerEnv' but allows the finalizer to be
+-- passed an additional environment parameter to be passed to the
+-- finalizer.  The environment passed to the finalizer is fixed by the
+-- second argument to 'addForeignPtrFinalizerEnv'
+addForeignPtrFinalizerEnv ::
+  FinalizerEnvPtr env a -> Ptr env -> ForeignPtr a -> IO ()
+addForeignPtrFinalizerEnv finalizer env fptr = 
+  addForeignPtrConcFinalizer fptr 
+       (mkFinalizerEnv finalizer env (unsafeForeignPtrToPtr fptr))
+
+foreign import ccall "dynamic" 
+  mkFinalizerEnv :: FinalizerEnvPtr env a -> Ptr env -> Ptr a -> IO ()
+#endif
+
+
 #ifndef __GLASGOW_HASKELL__
 mallocForeignPtr :: Storable a => IO (ForeignPtr a)
 mallocForeignPtr = do