Merge branch 'master' into type-nats
[ghc.git] / libraries / base / System / Mem / Weak.hs
1 {-# LANGUAGE Trustworthy #-}
2 {-# LANGUAGE CPP #-}
3
4 -----------------------------------------------------------------------------
5 -- |
6 -- Module : System.Mem.Weak
7 -- Copyright : (c) The University of Glasgow 2001
8 -- License : BSD-style (see the file libraries/base/LICENSE)
9 --
10 -- Maintainer : libraries@haskell.org
11 -- Stability : experimental
12 -- Portability : non-portable
13 --
14 -- In general terms, a weak pointer is a reference to an object that is
15 -- not followed by the garbage collector - that is, the existence of a
16 -- weak pointer to an object has no effect on the lifetime of that
17 -- object. A weak pointer can be de-referenced to find out
18 -- whether the object it refers to is still alive or not, and if so
19 -- to return the object itself.
20 --
21 -- Weak pointers are particularly useful for caches and memo tables.
22 -- To build a memo table, you build a data structure
23 -- mapping from the function argument (the key) to its result (the
24 -- value). When you apply the function to a new argument you first
25 -- check whether the key\/value pair is already in the memo table.
26 -- The key point is that the memo table itself should not keep the
27 -- key and value alive. So the table should contain a weak pointer
28 -- to the key, not an ordinary pointer. The pointer to the value must
29 -- not be weak, because the only reference to the value might indeed be
30 -- from the memo table.
31 --
32 -- So it looks as if the memo table will keep all its values
33 -- alive for ever. One way to solve this is to purge the table
34 -- occasionally, by deleting entries whose keys have died.
35 --
36 -- The weak pointers in this library
37 -- support another approach, called /finalization/.
38 -- When the key referred to by a weak pointer dies, the storage manager
39 -- arranges to run a programmer-specified finalizer. In the case of memo
40 -- tables, for example, the finalizer could remove the key\/value pair
41 -- from the memo table.
42 --
43 -- Another difficulty with the memo table is that the value of a
44 -- key\/value pair might itself contain a pointer to the key.
45 -- So the memo table keeps the value alive, which keeps the key alive,
46 -- even though there may be no other references to the key so both should
47 -- die. The weak pointers in this library provide a slight
48 -- generalisation of the basic weak-pointer idea, in which each
49 -- weak pointer actually contains both a key and a value.
50 --
51 -----------------------------------------------------------------------------
52
53 module System.Mem.Weak (
54 -- * The @Weak@ type
55 Weak, -- abstract
56
57 -- * The general interface
58 mkWeak, -- :: k -> v -> Maybe (IO ()) -> IO (Weak v)
59 deRefWeak, -- :: Weak v -> IO (Maybe v)
60 finalize, -- :: Weak v -> IO ()
61
62 -- * Specialised versions
63 mkWeakPtr, -- :: k -> Maybe (IO ()) -> IO (Weak k)
64 addFinalizer, -- :: key -> IO () -> IO ()
65 mkWeakPair, -- :: k -> v -> Maybe (IO ()) -> IO (Weak (k,v))
66 -- replaceFinaliser -- :: Weak v -> IO () -> IO ()
67
68 -- * A precise semantics
69
70 -- $precise
71 ) where
72
73 #ifdef __HUGS__
74 import Hugs.Weak
75 import Prelude
76 #endif
77
78 #ifdef __GLASGOW_HASKELL__
79 import GHC.Weak
80 #endif
81
82 -- | A specialised version of 'mkWeak', where the key and the value are
83 -- the same object:
84 --
85 -- > mkWeakPtr key finalizer = mkWeak key key finalizer
86 --
87 mkWeakPtr :: k -> Maybe (IO ()) -> IO (Weak k)
88 mkWeakPtr key finalizer = mkWeak key key finalizer
89
90 {-|
91 A specialised version of 'mkWeakPtr', where the 'Weak' object
92 returned is simply thrown away (however the finalizer will be
93 remembered by the garbage collector, and will still be run
94 when the key becomes unreachable).
95
96 Note: adding a finalizer to a 'Foreign.ForeignPtr.ForeignPtr' using
97 'addFinalizer' won't work as well as using the specialised version
98 'Foreign.ForeignPtr.addForeignPtrFinalizer' because the latter
99 version adds the finalizer to the primitive 'ForeignPtr#' object
100 inside, whereas the generic 'addFinalizer' will add the finalizer to
101 the box. Optimisations tend to remove the box, which may cause the
102 finalizer to run earlier than you intended. The same motivation
103 justifies the existence of
104 'Control.Concurrent.MVar.addMVarFinalizer' and
105 'Data.IORef.mkWeakIORef' (the non-uniformity is accidental).
106 -}
107 addFinalizer :: key -> IO () -> IO ()
108 addFinalizer key finalizer = do
109 _ <- mkWeakPtr key (Just finalizer) -- throw it away
110 return ()
111
112 -- | A specialised version of 'mkWeak' where the value is actually a pair
113 -- of the key and value passed to 'mkWeakPair':
114 --
115 -- > mkWeakPair key val finalizer = mkWeak key (key,val) finalizer
116 --
117 -- The advantage of this is that the key can be retrieved by 'deRefWeak'
118 -- in addition to the value.
119 mkWeakPair :: k -> v -> Maybe (IO ()) -> IO (Weak (k,v))
120 mkWeakPair key val finalizer = mkWeak key (key,val) finalizer
121
122
123 {- $precise
124
125 The above informal specification is fine for simple situations, but
126 matters can get complicated. In particular, it needs to be clear
127 exactly when a key dies, so that any weak pointers that refer to it
128 can be finalized. Suppose, for example, the value of one weak pointer
129 refers to the key of another...does that keep the key alive?
130
131 The behaviour is simply this:
132
133 * If a weak pointer (object) refers to an /unreachable/
134 key, it may be finalized.
135
136 * Finalization means (a) arrange that subsequent calls
137 to 'deRefWeak' return 'Nothing'; and (b) run the finalizer.
138
139 This behaviour depends on what it means for a key to be reachable.
140 Informally, something is reachable if it can be reached by following
141 ordinary pointers from the root set, but not following weak pointers.
142 We define reachability more precisely as follows A heap object is
143 reachable if:
144
145 * It is a member of the /root set/.
146
147 * It is directly pointed to by a reachable object, other than
148 a weak pointer object.
149
150 * It is a weak pointer object whose key is reachable.
151
152 * It is the value or finalizer of an object whose key is reachable.
153 -}
154