Remove references to () from types of mkWeak# and friends
[ghc.git] / libraries / base / GHC / Weak.hs
1 {-# LANGUAGE Unsafe #-}
2 {-# LANGUAGE NoImplicitPrelude
3 , BangPatterns
4 , MagicHash
5 , UnboxedTuples
6 , StandaloneDeriving
7 #-}
8 {-# OPTIONS_HADDOCK hide #-}
9
10 -----------------------------------------------------------------------------
11 -- |
12 -- Module : GHC.Weak
13 -- Copyright : (c) The University of Glasgow, 1998-2002
14 -- License : see libraries/base/LICENSE
15 --
16 -- Maintainer : cvs-ghc@haskell.org
17 -- Stability : internal
18 -- Portability : non-portable (GHC Extensions)
19 --
20 -- Weak pointers.
21 --
22 -----------------------------------------------------------------------------
23
24 module GHC.Weak (
25 Weak(..),
26 mkWeak,
27 deRefWeak,
28 finalize,
29 runFinalizerBatch
30 ) where
31
32 import GHC.Base
33
34 {-|
35 A weak pointer object with a key and a value. The value has type @v@.
36
37 A weak pointer expresses a relationship between two objects, the
38 /key/ and the /value/: if the key is considered to be alive by the
39 garbage collector, then the value is also alive. A reference from
40 the value to the key does /not/ keep the key alive.
41
42 A weak pointer may also have a finalizer of type @IO ()@; if it does,
43 then the finalizer will be run at most once, at a time after the key
44 has become unreachable by the program (\"dead\"). The storage manager
45 attempts to run the finalizer(s) for an object soon after the object
46 dies, but promptness is not guaranteed.
47
48 It is not guaranteed that a finalizer will eventually run, and no
49 attempt is made to run outstanding finalizers when the program exits.
50 Therefore finalizers should not be relied on to clean up resources -
51 other methods (eg. exception handlers) should be employed, possibly in
52 addition to finalizers.
53
54 References from the finalizer to the key are treated in the same way
55 as references from the value to the key: they do not keep the key
56 alive. A finalizer may therefore ressurrect the key, perhaps by
57 storing it in the same data structure.
58
59 The finalizer, and the relationship between the key and the value,
60 exist regardless of whether the program keeps a reference to the
61 'Weak' object or not.
62
63 There may be multiple weak pointers with the same key. In this
64 case, the finalizers for each of these weak pointers will all be
65 run in some arbitrary order, or perhaps concurrently, when the key
66 dies. If the programmer specifies a finalizer that assumes it has
67 the only reference to an object (for example, a file that it wishes
68 to close), then the programmer must ensure that there is only one
69 such finalizer.
70
71 If there are no other threads to run, the runtime system will check
72 for runnable finalizers before declaring the system to be deadlocked.
73
74 WARNING: weak pointers to ordinary non-primitive Haskell types are
75 particularly fragile, because the compiler is free to optimise away or
76 duplicate the underlying data structure. Therefore attempting to
77 place a finalizer on an ordinary Haskell type may well result in the
78 finalizer running earlier than you expected. This is not a problem
79 for caches and memo tables where early finalization is benign.
80
81 Finalizers /can/ be used reliably for types that are created explicitly
82 and have identity, such as @IORef@ and @MVar@. However, to place a
83 finalizer on one of these types, you should use the specific operation
84 provided for that type, e.g. @mkWeakIORef@ and @addMVarFinalizer@
85 respectively (the non-uniformity is accidental). These operations
86 attach the finalizer to the primitive object inside the box
87 (e.g. @MutVar#@ in the case of @IORef@), because attaching the
88 finalizer to the box itself fails when the outer box is optimised away
89 by the compiler.
90
91 -}
92 data Weak v = Weak (Weak# v)
93
94 -- | Establishes a weak pointer to @k@, with value @v@ and a finalizer.
95 --
96 -- This is the most general interface for building a weak pointer.
97 --
98 mkWeak :: k -- ^ key
99 -> v -- ^ value
100 -> Maybe (IO ()) -- ^ finalizer
101 -> IO (Weak v) -- ^ returns: a weak pointer object
102
103 mkWeak key val (Just (IO finalizer)) = IO $ \s ->
104 case mkWeak# key val finalizer' s of { (# s1, w #) -> (# s1, Weak w #) }
105 where
106 finalizer' :: State# RealWorld -> State# RealWorld
107 finalizer' s' = case finalizer s' of (# s'', () #) -> s''
108 mkWeak key val Nothing = IO $ \s ->
109 case mkWeakNoFinalizer# key val s of { (# s1, w #) -> (# s1, Weak w #) }
110
111 {-|
112 Dereferences a weak pointer. If the key is still alive, then
113 @'Just' v@ is returned (where @v@ is the /value/ in the weak pointer), otherwise
114 'Nothing' is returned.
115
116 The return value of 'deRefWeak' depends on when the garbage collector
117 runs, hence it is in the 'IO' monad.
118 -}
119 deRefWeak :: Weak v -> IO (Maybe v)
120 deRefWeak (Weak w) = IO $ \s ->
121 case deRefWeak# w s of
122 (# s1, flag, p #) -> case flag of
123 0# -> (# s1, Nothing #)
124 _ -> (# s1, Just p #)
125
126 -- | Causes a the finalizer associated with a weak pointer to be run
127 -- immediately.
128 finalize :: Weak v -> IO ()
129 finalize (Weak w) = IO $ \s ->
130 case finalizeWeak# w s of
131 (# s1, 0#, _ #) -> (# s1, () #) -- already dead, or no finalizer
132 (# s1, _, f #) -> case f s1 of s2 -> (# s2, () #)
133
134 {-
135 Instance Eq (Weak v) where
136 (Weak w1) == (Weak w2) = w1 `sameWeak#` w2
137 -}
138
139
140 -- run a batch of finalizers from the garbage collector. We're given
141 -- an array of finalizers and the length of the array, and we just
142 -- call each one in turn.
143 --
144 -- the IO primitives are inlined by hand here to get the optimal
145 -- code (sigh) --SDM.
146
147 runFinalizerBatch :: Int -> Array# (State# RealWorld -> State# RealWorld)
148 -> IO ()
149 runFinalizerBatch (I# n) arr =
150 let go m = IO $ \s ->
151 case m of
152 0# -> (# s, () #)
153 _ -> let !m' = m -# 1# in
154 case indexArray# arr m' of { (# io #) ->
155 case io s of { s' ->
156 unIO (go m') s'
157 }}
158 in
159 go n