From a29935ee62a9695f62c4d9e410cd6d15231ebd72 Mon Sep 17 00:00:00 2001 From: "dbaron@dbaron.org" Date: Fri, 29 Jun 2007 20:03:30 -0700 Subject: [PATCH] Suspect all native wrappers during cycle collection (last part, rest already done by Graydon). b=368869 r=graydon sr=jst --- js/src/xpconnect/src/nsXPConnect.cpp | 2 + js/src/xpconnect/src/xpcprivate.h | 3 ++ .../xpconnect/src/xpcwrappednativescope.cpp | 40 ++++++++++++++----- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/js/src/xpconnect/src/nsXPConnect.cpp b/js/src/xpconnect/src/nsXPConnect.cpp index 3375b097715..6031d0bb3f2 100644 --- a/js/src/xpconnect/src/nsXPConnect.cpp +++ b/js/src/xpconnect/src/nsXPConnect.cpp @@ -573,6 +573,8 @@ nsXPConnect::BeginCycleCollection() JS_SetGCCallback(cx, gOldJSGCCallback); gOldJSGCCallback = nsnull; + XPCWrappedNativeScope::SuspectAllWrappers(mRuntime); + #ifndef XPCONNECT_STANDALONE NS_ASSERTION(mObjRefcounts->mScopes.Count() == 0, "Didn't clear mScopes?"); XPCWrappedNativeScope::TraverseScopes(*mCycleCollectionContext); diff --git a/js/src/xpconnect/src/xpcprivate.h b/js/src/xpconnect/src/xpcprivate.h index 13e9df7fae0..33dea13f842 100644 --- a/js/src/xpconnect/src/xpcprivate.h +++ b/js/src/xpconnect/src/xpcprivate.h @@ -1135,6 +1135,9 @@ public: static void TraceJS(JSTracer* trc, XPCJSRuntime* rt); + static void + SuspectAllWrappers(XPCJSRuntime* rt); + static void FinishedMarkPhaseOfGC(JSContext* cx, XPCJSRuntime* rt); diff --git a/js/src/xpconnect/src/xpcwrappednativescope.cpp b/js/src/xpconnect/src/xpcwrappednativescope.cpp index 22b2b8cdf6b..25e018a3a06 100644 --- a/js/src/xpconnect/src/xpcwrappednativescope.cpp +++ b/js/src/xpconnect/src/xpcwrappednativescope.cpp @@ -271,15 +271,8 @@ WrappedNativeJSGCThingTracer(JSDHashTable *table, JSDHashEntryHdr *hdr, JSTracer* trc = (JSTracer *)arg; JS_CALL_OBJECT_TRACER(trc, wrapper->GetFlatJSObject(), "XPCWrappedNative::mFlatJSObject"); - - // FIXME: this call appears to do more harm than good, but - // there is reason to imagine it might clean up some cycles - // formed by a poor order between C++ and JS garbage cycle - // formations. See Bug 368869. - // - // if (JS_IsGCMarkingTracer(trc)) - // nsCycleCollector_suspectCurrent(wrapper); } + return JS_DHASH_NEXT; } @@ -291,13 +284,42 @@ XPCWrappedNativeScope::TraceJS(JSTracer* trc, XPCJSRuntime* rt) // access to JS runtime. See bug 380139. XPCAutoLock lock(rt->GetMapLock()); - // Do JS_CallTracer for all wrapperednatives with external references. + // Do JS_CallTracer for all wrapped natives with external references. for(XPCWrappedNativeScope* cur = gScopes; cur; cur = cur->mNext) { cur->mWrappedNativeMap->Enumerate(WrappedNativeJSGCThingTracer, trc); } } +JS_STATIC_DLL_CALLBACK(JSDHashOperator) +WrappedNativeSuspecter(JSDHashTable *table, JSDHashEntryHdr *hdr, + uint32 number, void *arg) +{ + XPCWrappedNative* wrapper = ((Native2WrappedNativeMap::Entry*)hdr)->value; + XPCWrappedNativeProto* proto = wrapper->GetProto(); + if(proto && proto->ClassIsMainThreadOnly()) + { + NS_ASSERTION(NS_IsMainThread(), + "Suspecting wrapped natives from non-main thread"); + nsCycleCollector_suspectCurrent(wrapper); + } + + return JS_DHASH_NEXT; +} + +// static +void +XPCWrappedNativeScope::SuspectAllWrappers(XPCJSRuntime* rt) +{ + XPCAutoLock lock(rt->GetMapLock()); + + // Do nsCycleCollector_suspectCurrent for all wrapped natives. + for(XPCWrappedNativeScope* cur = gScopes; cur; cur = cur->mNext) + { + cur->mWrappedNativeMap->Enumerate(WrappedNativeSuspecter, nsnull); + } +} + // static void XPCWrappedNativeScope::FinishedMarkPhaseOfGC(JSContext* cx, XPCJSRuntime* rt)