Bug 661927 - Add Cu.schedulePreciseGC to allow for a GC to run with no JS code running. r=mrbkap

This commit is contained in:
Josh Matthews 2011-07-01 02:08:48 -04:00
Родитель 0b762cda15
Коммит 5163e29da2
4 изменённых файлов: 90 добавлений и 1 удалений

Просмотреть файл

@ -120,10 +120,19 @@ interface nsIXPCComponents_utils_Sandbox : nsISupports
{
};
/**
* interface for callback to be passed to Cu.schedulePreciseGC
*/
[scriptable, function, uuid(71000535-b0fd-44d1-8ce0-909760e3953c)]
interface ScheduledGCCallback : nsISupports
{
void callback();
};
/**
* interface of Components.utils
*/
[scriptable, uuid(5f0acf45-135a-48d1-976c-082ce3b24ead)]
[scriptable, uuid(fed2d752-6cb3-4135-97b0-2c290e541e2d)]
interface nsIXPCComponents_Utils : nsISupports
{
@ -228,6 +237,13 @@ interface nsIXPCComponents_Utils : nsISupports
*/
void forceGC();
/*
* Schedule a garbage collection cycle for a point in the future when no JS
* is running. Call the provided function once this has occurred.
*/
[implicit_jscontext]
void schedulePreciseGC(in ScheduledGCCallback callback);
/*
* To be called from JS only.
*

Просмотреть файл

@ -55,6 +55,7 @@
#include "nsNullPrincipal.h"
#include "nsJSUtils.h"
#include "mozJSComponentLoader.h"
#include "nsContentUtils.h"
/***************************************************************************/
// stuff used by all
@ -3775,6 +3776,48 @@ nsXPCComponents_Utils::ForceGC()
return NS_OK;
}
class PreciseGCRunnable : public nsRunnable
{
public:
PreciseGCRunnable(JSContext *aCx, ScheduledGCCallback* aCallback)
: mCallback(aCallback), mCx(aCx) {}
NS_IMETHOD Run()
{
nsCOMPtr<nsIJSRuntimeService> runtimeSvc = do_GetService("@mozilla.org/js/xpc/RuntimeService;1");
NS_ENSURE_STATE(runtimeSvc);
JSRuntime* rt = nsnull;
runtimeSvc->GetRuntime(&rt);
NS_ENSURE_STATE(rt);
JSContext *cx;
JSContext *iter = nsnull;
while ((cx = JS_ContextIterator(rt, &iter)) != NULL) {
if (JS_IsRunning(cx)) {
return NS_DispatchToMainThread(this);
}
}
JS_GC(mCx);
mCallback->Callback();
return NS_OK;
}
private:
nsRefPtr<ScheduledGCCallback> mCallback;
JSContext *mCx;
};
/* [inline_jscontext] void schedulePreciseGC(in ScheduledGCCallback callback); */
NS_IMETHODIMP
nsXPCComponents_Utils::SchedulePreciseGC(ScheduledGCCallback* aCallback, JSContext* aCx)
{
nsRefPtr<PreciseGCRunnable> event = new PreciseGCRunnable(aCx, aCallback);
return NS_DispatchToMainThread(event);
}
/* void getGlobalForObject(); */
NS_IMETHODIMP
nsXPCComponents_Utils::GetGlobalForObject()

Просмотреть файл

@ -66,6 +66,7 @@ _CHROME_FILES = \
test_bug596580.xul \
test_bug654370.xul \
test_bug658560.xul \
test_precisegc.xul \
$(NULL)
# Disabled until this test gets updated to test the new proxy based

Просмотреть файл

@ -0,0 +1,29 @@
<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet href="chrome://mochikit/content/tests/SimpleTest/test.css"
type="text/css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=533596
-->
<window title="Mozilla Bug 661927"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://mochikit/content/MochiKit/packed.js"></script>
<script type="application/javascript"
src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<!-- test results are displayed in the html:body -->
<body xmlns="http://www.w3.org/1999/xhtml">
</body>
<!-- test code goes here -->
<script type="application/javascript"><![CDATA[
Components.utils.schedulePreciseGC(
function() {
ok(true, "callback executed");
SimpleTest.finish();
});
SimpleTest.waitForExplicitFinish();
]]></script>
</window>