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