2 % (c) The AQUA Project, Glasgow University, 1998
5 \section[PrelWeak]{Module @PrelWeak@}
8 {-# OPTIONS -fno-implicit-prelude #-}
18 data Weak v = Weak (Weak# v)
23 -> IO (Weak v) -- weak pointer
25 mkWeak key val finaliser = IO $ \s ->
26 case mkWeak# key val finaliser s of { (# s1, w #) ->
29 deRefWeak :: Weak v -> IO (Maybe v)
30 deRefWeak (Weak w) = IO $ \s ->
31 case deRefWeak# w s of
32 (# s1, flag, p #) -> case flag of
33 0# -> (# s1, Nothing #)
36 mkWeakPtr :: k -> IO () -> IO (Weak k)
37 mkWeakPtr key finaliser = mkWeak key key finaliser
39 mkWeakPair :: k -> v -> IO () -> IO (Weak (k,v))
40 mkWeakPair key val finaliser = mkWeak key (key,val) finaliser
42 addFinaliser :: key -> IO () -> IO ()
43 addFinaliser key finaliser = do
44 mkWeakPtr key finaliser -- throw it away
47 addForeignFinaliser :: ForeignObj -> IO () -> IO ()
48 addForeignFinaliser (ForeignObj fo) finaliser = addFinaliser fo finaliser
51 finalise :: Weak v -> IO ()
52 finalise (Weak w) = finaliseWeak# w
54 instance Eq (Weak v) where
55 (Weak w1) == (Weak w2) = w1 `sameWeak#` w2