Movatterモバイル変換
[0]ホーム
{-# LANGUAGE Unsafe #-}{-# LANGUAGE NoImplicitPrelude , BangPatterns , MagicHash , UnboxedTuples , StandaloneDeriving #-}{-# OPTIONS_HADDOCK hide #-}------------------------------------------------------------------------------- |-- Module : GHC.Weak-- Copyright : (c) The University of Glasgow, 1998-2002-- License : see libraries/base/LICENSE---- Maintainer : cvs-ghc@haskell.org-- Stability : internal-- Portability : non-portable (GHC Extensions)---- Weak pointers.-------------------------------------------------------------------------------moduleGHC.Weak(Weak(..),mkWeak,deRefWeak,finalize,runFinalizerBatch)whereimportGHC.Base{-|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 thegarbage collector, then the value is also alive. A reference fromthe 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 at most once, at a time after the keyhas become unreachable by the program (\"dead\"). The storage managerattempts to run the finalizer(s) for an object soon after the objectdies, but promptness is not guaranteed.It is not guaranteed that a finalizer will eventually run, and noattempt is made to run outstanding finalizers when the program exits.Therefore finalizers should not be relied on to clean up resources -other methods (eg. exception handlers) should be employed, possibly inaddition to finalizers.References from the finalizer to the key are treated in the same wayas references from the value to the key: they do not keep the keyalive. A finalizer may therefore ressurrect the key, perhaps bystoring 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 thiscase, the finalizers for each of these weak pointers will all berun in some arbitrary order, or perhaps concurrently, when the keydies. If the programmer specifies a finalizer that assumes it hasthe only reference to an object (for example, a file that it wishesto close), then the programmer must ensure that there is only onesuch finalizer.If there are no other threads to run, the runtime system will checkfor runnable finalizers before declaring the system to be deadlocked.WARNING: weak pointers to ordinary non-primitive Haskell types areparticularly fragile, because the compiler is free to optimise away orduplicate the underlying data structure. Therefore attempting toplace a finalizer on an ordinary Haskell type may well result in thefinalizer running earlier than you expected. This is not a problemfor caches and memo tables where early finalization is benign.Finalizers /can/ be used reliably for types that are created explicitlyand have identity, such as @IORef@ and @MVar@. However, to place afinalizer on one of these types, you should use the specific operationprovided for that type, e.g. @mkWeakIORef@ and @addMVarFinalizer@respectively (the non-uniformity is accidental). These operationsattach the finalizer to the primitive object inside the box(e.g. @MutVar#@ in the case of @IORef@), because attaching thefinalizer to the box itself fails when the outer box is optimised awayby the compiler.-}dataWeakv=Weak(Weak#v)-- | Establishes a weak pointer to @k@, with value @v@ and a finalizer.---- This is the most general interface for building a weak pointer.--mkWeak::k-- ^ key->v-- ^ value->Maybe(IO())-- ^ finalizer->IO(Weakv)-- ^ returns: a weak pointer objectmkWeakkeyval(Just(IOfinalizer))=IO$\s->casemkWeak#keyvalfinalizersof{(#s1,w#)->(#s1,Weakw#)}mkWeakkeyvalNothing=IO$\s->casemkWeakNoFinalizer#keyvalsof{(#s1,w#)->(#s1,Weakw#)}{-|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 collectorruns, hence it is in the 'IO' monad.-}deRefWeak::Weakv->IO(Maybev)deRefWeak(Weakw)=IO$\s->casedeRefWeak#wsof(#s1,flag,p#)->caseflagof0#->(#s1,Nothing#)_->(#s1,Justp#)-- | Causes a the finalizer associated with a weak pointer to be run-- immediately.finalize::Weakv->IO()finalize(Weakw)=IO$\s->casefinalizeWeak#wsof(#s1,0#,_#)->(#s1,()#)-- already dead, or no finalizer(#s1,_,f#)->fs1{-Instance Eq (Weak v) where (Weak w1) == (Weak w2) = w1 `sameWeak#` w2-}-- run a batch of finalizers from the garbage collector. We're given-- an array of finalizers and the length of the array, and we just-- call each one in turn.---- the IO primitives are inlined by hand here to get the optimal-- code (sigh) --SDM.runFinalizerBatch::Int->Array#(State#RealWorld->State#RealWorld)->IO()runFinalizerBatch(I#n)arr=letgom=IO$\s->casemof0#->(#s,()#)_->let!m'=m-#1#incaseindexArray#arrm'of{(#io#)->casecatch#(\p->(#iop,()#))(\_s''->(#s'',()#))sof{(#s',_#)->unIO(gom')s'}}ingon
[8]ページ先頭