Haskell Core Libraries (base package)ParentContentsIndex
GHC.Weak
Portability non-portable (GHC Extensions)
Stability internal
Maintainer cvs-ghc@haskell.org
Description
Weak pointers.
Synopsis
data Weak v = Weak (Weak# v)
mkWeak :: k -> v -> Maybe (IO ()) -> IO (Weak v)
deRefWeak :: Weak v -> IO (Maybe v)
finalize :: Weak v -> IO ()
runFinalizerBatch :: Int -> Array# (IO ()) -> IO ()
Documentation
data Weak v

A weak pointer object with a key and a value. The value has type v.

A weak pointer expresses a relationship between two objects, the key and the value: if the key is considered to be alive by the garbage collector, then the value is also alive. A reference from the value to the key does not keep the key alive.

A weak pointer may also have a finalizer of type IO (); if it does, then the finalizer will be run once, and once only, at a time after the key has become unreachable by the program ("dead"). The storage manager attempts to run the finalizer(s) for an object soon after the object dies, but promptness is not guaranteed.

References from the finalizer to the key are treated in the same way as references from the value to the key: they do not keep the key alive. A finalizer may therefore ressurrect the key, perhaps by storing it in the same data structure.

The finalizer, and the relationship between the key and the value, exist regardless of whether the program keeps a reference to the Weak object or not.

There may be multiple weak pointers with the same key. In this case, the finalizers for each of these weak pointers will all be run in some arbitrary order, or perhaps concurrently, when the key dies. If the programmer specifies a finalizer that assumes it has the only reference to an object (for example, a file that it wishes to close), then the programmer must ensure that there is only one such finalizer.

If there are no other threads to run, the runtime system will check for runnable finalizers before declaring the system to be deadlocked.

Constructors
Weak (Weak# v)
Instances
(Typeable a) => Typeable (Weak a)
mkWeak
:: k key
-> v value
-> Maybe (IO ()) finalizer
-> IO (Weak v) returns: a weak pointer object

Establishes a weak pointer to k, with value v and a finalizer.

This is the most general interface for building a weak pointer.

deRefWeak :: Weak v -> IO (Maybe v)

Dereferences a weak pointer. If the key is still alive, then Just v is returned (where v is the value in the weak pointer), otherwise Nothing is returned.

The return value of deRefWeak depends on when the garbage collector runs, hence it is in the IO monad.

finalize :: Weak v -> IO ()
Causes a the finalizer associated with a weak pointer to be run immediately.
runFinalizerBatch :: Int -> Array# (IO ()) -> IO ()
Produced by Haddock version 0.4