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