X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=rts%2FWeak.c;h=a50a72e59cd4bf56152ea6e766b6e6c7a7d201e1;hb=5a336f14d30f9b46ac74ca319ed1af25430cd67a;hp=a83cef995f64ece71523fa61e3593c622532445d;hpb=5a2769f0273dd389977e8283375e7920d183bdd4;p=ghc-hetmet.git diff --git a/rts/Weak.c b/rts/Weak.c index a83cef9..a50a72e 100644 --- a/rts/Weak.c +++ b/rts/Weak.c @@ -7,20 +7,48 @@ * ---------------------------------------------------------------------------*/ #include "PosixSource.h" -#define COMPILING_RTS_MAIN #include "Rts.h" #include "RtsUtils.h" #include "SchedAPI.h" #include "RtsFlags.h" #include "Weak.h" -#include "Storage.h" #include "Schedule.h" #include "Prelude.h" #include "RtsAPI.h" #include "Trace.h" +// ForeignPtrs with C finalizers rely on weak pointers inside weak_ptr_list +// to always be in the same order. + StgWeak *weak_ptr_list; +void +runCFinalizer(StgVoid *fn, StgVoid *ptr, StgVoid *env, StgWord flag) +{ + if (flag) + ((void (*)(void *, void *))fn)(env, ptr); + else + ((void (*)(void *))fn)(ptr); +} + +void +runAllCFinalizers(StgWeak *list) +{ + StgWeak *w; + + for (w = list; w; w = w->link) { + StgArrWords *farr; + + farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer); + + if ((StgClosure *)farr != &stg_NO_FINALIZER_closure) + runCFinalizer((StgVoid *)farr->payload[0], + (StgVoid *)farr->payload[1], + (StgVoid *)farr->payload[2], + farr->payload[3]); + } +} + /* * scheduleFinalizers() is called on the list of weak pointers found * to be dead after a garbage collection. It overwrites each object @@ -47,6 +75,7 @@ scheduleFinalizers(Capability *cap, StgWeak *list) // count number of finalizers, and kill all the weak pointers first... n = 0; for (w = list; w; w = w->link) { + StgArrWords *farr; // Better not be a DEAD_WEAK at this stage; the garbage // collector removes DEAD_WEAKs from the weak pointer list. @@ -56,6 +85,14 @@ scheduleFinalizers(Capability *cap, StgWeak *list) n++; } + farr = (StgArrWords *)UNTAG_CLOSURE(w->cfinalizer); + + if ((StgClosure *)farr != &stg_NO_FINALIZER_closure) + runCFinalizer((StgVoid *)farr->payload[0], + (StgVoid *)farr->payload[1], + (StgVoid *)farr->payload[2], + farr->payload[3]); + #ifdef PROFILING // A weak pointer is inherently used, so we do not need to call // LDV_recordDead().