c3839b1002b3efe55d6757811fbbf134afaeb170
[ghc-base.git] / GHC / Weak.lhs
1 \begin{code}
2 {-# OPTIONS -fno-implicit-prelude #-}
3 -----------------------------------------------------------------------------
4 -- |
5 -- Module      :  GHC.Weak
6 -- Copyright   :  (c) The University of Glasgow, 1998-2002
7 -- License     :  see libraries/base/LICENSE
8 -- 
9 -- Maintainer  :  cvs-ghc@haskell.org
10 -- Stability   :  internal
11 -- Portability :  non-portable (GHC Extensions)
12 --
13 -- Weak pointers.
14 --
15 -----------------------------------------------------------------------------
16
17 module GHC.Weak where
18
19 import GHC.Base
20 import Data.Maybe
21 import GHC.IOBase       ( IO(..), unIO )
22
23 {-|
24 A weak pointer object with a key and a value.  The value has type @v@.
25
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.
30
31 A weak pointer may also have a finalizer of type @IO ()@; if it does,
32 then the finalizer will be run at most once, at a time after the key
33 has become unreachable by the program (\"dead\").  The storage manager
34 attempts to run the finalizer(s) for an object soon after the object
35 dies, but promptness is not guaranteed.  
36
37 It is not guaranteed that a finalizer will eventually run, and no
38 attempt is made to run outstanding finalizers when the program exits.
39 Therefore finalizers should not be relied on to clean up resources -
40 other methods (eg. exception handlers) should be employed, possibly in
41 addition to finalisers.
42
43 References from the finalizer to the key are treated in the same way
44 as references from the value to the key: they do not keep the key
45 alive.  A finalizer may therefore ressurrect the key, perhaps by
46 storing it in the same data structure.
47
48 The finalizer, and the relationship between the key and the value,
49 exist regardless of whether the program keeps a reference to the
50 'Weak' object or not.
51
52 There may be multiple weak pointers with the same key.  In this
53 case, the finalizers for each of these weak pointers will all be
54 run in some arbitrary order, or perhaps concurrently, when the key
55 dies.  If the programmer specifies a finalizer that assumes it has
56 the only reference to an object (for example, a file that it wishes
57 to close), then the programmer must ensure that there is only one
58 such finalizer.
59
60 If there are no other threads to run, the runtime system will check
61 for runnable finalizers before declaring the system to be deadlocked.
62 -}
63 data Weak v = Weak (Weak# v)
64
65 -- | Establishes a weak pointer to @k@, with value @v@ and a finalizer.
66 --
67 -- This is the most general interface for building a weak pointer.
68 --
69 mkWeak  :: k                            -- ^ key
70         -> v                            -- ^ value
71         -> Maybe (IO ())                -- ^ finalizer
72         -> IO (Weak v)                  -- ^ returns: a weak pointer object
73
74 mkWeak key val (Just finalizer) = IO $ \s ->
75    case mkWeak# key val finalizer s of { (# s1, w #) -> (# s1, Weak w #) }
76 mkWeak key val Nothing = IO $ \s ->
77    case mkWeak# key val (unsafeCoerce# 0#) s of { (# s1, w #) -> (# s1, Weak w #) }
78
79 {-|
80 Dereferences a weak pointer.  If the key is still alive, then
81 @'Just' v@ is returned (where @v@ is the /value/ in the weak pointer), otherwise
82 'Nothing' is returned.
83
84 The return value of 'deRefWeak' depends on when the garbage collector
85 runs, hence it is in the 'IO' monad.
86 -}
87 deRefWeak :: Weak v -> IO (Maybe v)
88 deRefWeak (Weak w) = IO $ \s ->
89    case deRefWeak# w s of
90         (# s1, flag, p #) -> case flag of
91                                 0# -> (# s1, Nothing #)
92                                 _  -> (# s1, Just p #)
93
94 -- | Causes a the finalizer associated with a weak pointer to be run
95 -- immediately.
96 finalize :: Weak v -> IO ()
97 finalize (Weak w) = IO $ \s ->
98    case finalizeWeak# w s of
99         (# s1, 0#, _ #) -> (# s1, () #) -- already dead, or no finaliser
100         (# s1, _,  f #) -> f s1
101
102 {-
103 Instance Eq (Weak v) where
104   (Weak w1) == (Weak w2) = w1 `sameWeak#` w2
105 -}
106
107
108 -- run a batch of finalizers from the garbage collector.  We're given 
109 -- an array of finalizers and the length of the array, and we just
110 -- call each one in turn.
111 --
112 -- the IO primitives are inlined by hand here to get the optimal
113 -- code (sigh) --SDM.
114
115 runFinalizerBatch :: Int -> Array# (IO ()) -> IO ()
116 runFinalizerBatch (I# n) arr = 
117    let  go m  = IO $ \s ->
118                   case m of 
119                   0# -> (# s, () #)
120                   _  -> let m' = m -# 1# in
121                         case indexArray# arr m' of { (# io #) -> 
122                         case unIO io s of          { (# s, _ #) -> 
123                         unIO (go m') s
124                         }}
125    in
126         go n
127
128 \end{code}