import Foreign.Ptr
import Foreign.Storable
+import GHC.List ( null )
import GHC.Base
import GHC.IOBase
import GHC.STRef ( STRef(..) )
-- attached to is kept alive.
data ForeignPtrContents
- = PlainNoFinalizer
- | PlainWithFinalizer !(IORef [IO ()])
- | MallocPtrNoFinalizer (MutableByteArray# RealWorld)
- | MallocPtrWithFinalizer (MutableByteArray# RealWorld) !(IORef [IO ()])
- -- we optimise the no-finalizer case, which is especially common
- -- with a MallocPtr. mallocForeignPtr doesn't have to create an
- -- IORef, or set up a weak pointer.
+ = PlainForeignPtr !(IORef [IO ()])
+ | MallocPtr (MutableByteArray# RealWorld) !(IORef [IO ()])
instance Eq (ForeignPtr a) where
p == q = unsafeForeignPtrToPtr p == unsafeForeignPtrToPtr q
-- in fact there is no guarantee that the finalizer will eventually
-- run at all.
--
+-- Note that references from a finalizer do not necessarily prevent
+-- another object from being finalized. If A's finalizer refers to B
+-- (perhaps using 'touchForeignPtr', then the only guarantee is that
+-- B's finalizer will never be started before A's. If both A and B
+-- are unreachable, then both finalizers will start together. See
+-- 'touchForeignPtr' for more on finalizer ordering.
+--
newConcForeignPtr p finalizer
= do fObj <- newForeignPtr_ p
addForeignPtrConcFinalizer fObj finalizer
-- although it may be implemented differently internally: you may not
-- assume that the memory returned by 'mallocForeignPtr' has been
-- allocated with 'Foreign.Marshal.Alloc.malloc'.
+--
+-- GHC notes: 'mallocForeignPtr' has a heavily optimised
+-- implementation in GHC. It uses pinned memory in the garbage
+-- collected heap, so the 'ForeignPtr' does not require a finalizer to
+-- free the memory. Use of 'mallocForeignPtr' and associated
+-- functions is strongly recommended in preference to 'newForeignPtr'
+-- with a finalizer.
+--
mallocForeignPtr = doMalloc undefined
where doMalloc :: Storable b => b -> IO (ForeignPtr b)
doMalloc a = do
+ r <- newIORef []
IO $ \s ->
case newPinnedByteArray# size s of { (# s, mbarr# #) ->
(# s, ForeignPtr (byteArrayContents# (unsafeCoerce# mbarr#))
- (MallocPtrNoFinalizer mbarr#) #)
+ (MallocPtr mbarr# r) #)
}
where (I# size) = sizeOf a
-- size of the memory required is given explicitly as a number of bytes.
mallocForeignPtrBytes :: Int -> IO (ForeignPtr a)
mallocForeignPtrBytes (I# size) = do
+ r <- newIORef []
IO $ \s ->
case newPinnedByteArray# size s of { (# s, mbarr# #) ->
(# s, ForeignPtr (byteArrayContents# (unsafeCoerce# mbarr#))
- (MallocPtrNoFinalizer mbarr#) #)
+ (MallocPtr mbarr# r) #)
}
addForeignPtrFinalizer :: FinalizerPtr a -> ForeignPtr a -> IO ()
addForeignPtrConcFinalizer (ForeignPtr a c) finalizer =
addForeignPtrConcFinalizer_ c finalizer
-addForeignPtrConcFinalizer_ PlainNoFinalizer finalizer = do
- r <- newIORef []
- IO $ \s -> case r of { IORef (STRef r#) ->
+addForeignPtrConcFinalizer_ f@(PlainForeignPtr r) finalizer = do
+ fs <- readIORef r
+ writeIORef r (finalizer : fs)
+ if (null fs)
+ then IO $ \s ->
+ case r of { IORef (STRef r#) ->
case mkWeak# r# () (foreignPtrFinalizer r) s of { (# s1, w #) ->
(# s1, () #) }}
- addForeignPtrConcFinalizer_ (PlainWithFinalizer r) finalizer
-
-addForeignPtrConcFinalizer_ f@(PlainWithFinalizer r) finalizer = do
+ else return ()
+addForeignPtrConcFinalizer_ f@(MallocPtr fo r) finalizer = do
fs <- readIORef r
writeIORef r (finalizer : fs)
-
-addForeignPtrConcFinalizer_ f@(MallocPtrNoFinalizer fo) finalizer = do
- r <- newIORef []
- IO $ \s -> case mkWeak# fo () (do foreignPtrFinalizer r; touch f) s of
+ if (null fs)
+ then IO $ \s ->
+ case mkWeak# fo () (do foreignPtrFinalizer r; touch f) s of
(# s1, w #) -> (# s1, () #)
- addForeignPtrConcFinalizer_ (MallocPtrWithFinalizer fo r) finalizer
-
-addForeignPtrConcFinalizer_ f@(MallocPtrWithFinalizer fo r) finalizer = do
- fs <- readIORef r
- writeIORef r (finalizer : fs)
+ else return ()
foreign import ccall "dynamic"
mkFinalizer :: FinalizerPtr a -> Ptr a -> IO ()
newForeignPtr_ :: Ptr a -> IO (ForeignPtr a)
-- ^Turns a plain memory reference into a foreign pointer that may be
-- associated with finalizers by using 'addForeignPtrFinalizer'.
-newForeignPtr_ (Ptr obj) = return (ForeignPtr obj PlainNoFinalizer)
+newForeignPtr_ (Ptr obj) = do
+ r <- newIORef []
+ return (ForeignPtr obj (PlainForeignPtr r))
touchForeignPtr :: ForeignPtr a -> IO ()
-- ^This function ensures that the foreign object in
-- does a 'touchForeignPtr' after it
-- executes the user action.
--
--- Note that this function should not be used to express liveness
--- dependencies between 'ForeignPtr's. For example, if the finalizer
+-- Note that this function should not be used to express dependencies
+-- between finalizers on 'ForeignPtr's. For example, if the finalizer
-- for a 'ForeignPtr' @F1@ calls 'touchForeignPtr' on a second
-- 'ForeignPtr' @F2@, then the only guarantee is that the finalizer
-- for @F2@ is never started before the finalizer for @F1@. They
-- between the finalizers, but even then the runtime sometimes runs
-- multiple finalizers sequentially in a single thread (for
-- performance reasons), so synchronisation between finalizers could
--- result in artificial deadlock.
+-- result in artificial deadlock. Another alternative is to use
+-- explicit reference counting.
--
touchForeignPtr (ForeignPtr fo r) = touch r
-- | Causes the finalizers associated with a foreign pointer to be run
-- immediately.
finalizeForeignPtr :: ForeignPtr a -> IO ()
-finalizeForeignPtr (ForeignPtr _ contents) = do
- case contents of
- PlainNoFinalizer -> return ()
- PlainWithFinalizer r -> runFinalizers r
- MallocPtrNoFinalizer _ -> return ()
- MallocPtrWithFinalizer _ r -> runFinalizers r
- where
- runFinalizers r = do
- fs <- readIORef r
- sequence_ fs
- writeIORef r []
+finalizeForeignPtr (ForeignPtr _ foreignPtr) = do
+ finalizers <- readIORef refFinalizers
+ sequence_ finalizers
+ writeIORef refFinalizers []
+ where
+ refFinalizers = case foreignPtr of
+ (PlainForeignPtr ref) -> ref
+ (MallocPtr _ ref) -> ref