2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
6 -- Copyright : (c) The University of Glasgow, 1998-2002
7 -- License : see libraries/base/LICENSE
9 -- Maintainer : cvs-ghc@haskell.org
10 -- Stability : internal
11 -- Portability : non-portable (GHC Extensions)
15 -----------------------------------------------------------------------------
21 import GHC.IOBase ( IO(..), unIO )
24 A weak pointer object with a key and a value. The value has type @v@.
26 A weak pointer expresses a relationship between two objects, the
27 /key/ and the /value/: if the key is considered to be alive by the
28 garbage collector, then the value is also alive. A reference from
29 the value to the key does /not/ keep the key alive.
31 A weak pointer may also have a finalizer of type @IO ()@; if it does,
32 then the finalizer will be run once, and once only, at a time after
33 the key has become unreachable by the program (\"dead\"). The storage
34 manager attempts to run the finalizer(s) for an object soon after the
35 object dies, but promptness is not guaranteed.
37 References from the finalizer to the key are treated in the same way
38 as references from the value to the key: they do not keep the key
39 alive. A finalizer may therefore ressurrect the key, perhaps by
40 storing it in the same data structure.
42 The finalizer, and the relationship between the key and the value,
43 exist regardless of whether the program keeps a reference to the
46 There may be multiple weak pointers with the same key. In this
47 case, the finalizers for each of these weak pointers will all be
48 run in some arbitrary order, or perhaps concurrently, when the key
49 dies. If the programmer specifies a finalizer that assumes it has
50 the only reference to an object (for example, a file that it wishes
51 to close), then the programmer must ensure that there is only one
54 If there are no other threads to run, the runtime system will check
55 for runnable finalizers before declaring the system to be deadlocked.
57 data Weak v = Weak (Weak# v)
59 -- | Establishes a weak pointer to @k@, with value @v@ and a finalizer.
61 -- This is the most general interface for building a weak pointer.
65 -> Maybe (IO ()) -- ^ finalizer
66 -> IO (Weak v) -- ^ returns: a weak pointer object
68 mkWeak key val (Just finalizer) = IO $ \s ->
69 case mkWeak# key val finalizer s of { (# s1, w #) -> (# s1, Weak w #) }
70 mkWeak key val Nothing = IO $ \s ->
71 case mkWeak# key val (unsafeCoerce# 0#) s of { (# s1, w #) -> (# s1, Weak w #) }
74 A specialised version of 'mkWeak', where the key and the value are the
77 > mkWeakPtr key finalizer = mkWeak key key finalizer
79 mkWeakPtr :: k -> Maybe (IO ()) -> IO (Weak k)
80 mkWeakPtr key finalizer = mkWeak key key finalizer
83 A specialised version of 'mkWeakPtr', where the 'Weak' object
84 returned is simply thrown away (however the finalizer will be
85 remembered by the garbage collector, and will still be run
86 when the key becomes unreachable).
88 Note: adding a finalizer to a 'Foreign.ForeignPtr.ForeignPtr' using
89 'addFinalizer' won't work as well as using the specialised version
90 'Foreign.ForeignPtr.addForeignPtrFinalizer' because the latter
91 version adds the finalizer to the primitive 'ForeignPtr#' object
92 inside, whereas the generic 'addFinalizer' will add the finalizer to
93 the box. Optimisations tend to remove the box, which may cause the
94 finalizer to run earlier than you intended. The same motivation
95 justifies the existence of
96 'Control.Concurrent.MVar.addMVarFinalizer' and
97 'Data.IORef.mkWeakIORef' (the non-unformity is accidental).
99 addFinalizer :: key -> IO () -> IO ()
100 addFinalizer key finalizer = do
101 mkWeakPtr key (Just finalizer) -- throw it away
105 Instance Eq (Weak v) where
106 (Weak w1) == (Weak w2) = w1 `sameWeak#` w2
110 -- run a batch of finalizers from the garbage collector. We're given
111 -- an array of finalizers and the length of the array, and we just
112 -- call each one in turn.
114 -- the IO primitives are inlined by hand here to get the optimal
115 -- code (sigh) --SDM.
117 runFinalizerBatch :: Int -> Array# (IO ()) -> IO ()
118 runFinalizerBatch (I# n) arr =
119 let go m = IO $ \s ->
122 _ -> let m' = m -# 1# in
123 case indexArray# arr m' of { (# io #) ->
124 case unIO io s of { (# s, _ #) ->