1 -----------------------------------------------------------------------------
3 -- Module : System.Mem.Weak
4 -- Copyright : (c) The University of Glasgow 2001
5 -- License : BSD-style (see the file libraries/base/LICENSE)
7 -- Maintainer : libraries@haskell.org
8 -- Stability : experimental
9 -- Portability : non-portable
11 -- Weak references, weak pairs, weak pointers, and finalizers.
13 -----------------------------------------------------------------------------
15 module System.Mem.Weak (
17 -- instance Eq (Weak v)
19 mkWeak, -- :: k -> v -> Maybe (IO ()) -> IO (Weak v)
20 deRefWeak, -- :: Weak v -> IO (Maybe v)
21 finalize, -- :: Weak v -> IO ()
22 -- replaceFinaliser -- :: Weak v -> IO () -> IO ()
24 mkWeakPtr, -- :: k -> Maybe (IO ()) -> IO (Weak k)
25 mkWeakPair, -- :: k -> v -> Maybe (IO ()) -> IO (Weak (k,v))
26 addFinalizer -- :: key -> IO () -> IO ()
33 #ifdef __GLASGOW_HASKELL__
39 INSTANCE_TYPEABLE1(Weak,weakTc,"Weak")
41 deRefWeak :: Weak v -> IO (Maybe v)
42 deRefWeak (Weak w) = IO $ \s ->
43 case deRefWeak# w s of
44 (# s1, flag, p #) -> case flag of
45 0# -> (# s1, Nothing #)
48 mkWeakPair :: k -> v -> Maybe (IO ()) -> IO (Weak (k,v))
49 mkWeakPair key val finalizer = mkWeak key (key,val) finalizer
51 finalize :: Weak v -> IO ()
52 finalize (Weak w) = IO $ \s ->
53 case finalizeWeak# w s of
54 (# s1, 0#, _ #) -> (# s1, () #) -- already dead, or no finaliser
55 (# s1, _, f #) -> f s1