Bug 956899 - Use js::Thread for JS shell watchdog thread; r=jandem

--HG--
extra : rebase_source : 0a7482357cb036b9991deaa20c76e07e957cddff
This commit is contained in:
Terrence Cole 2016-05-25 09:41:57 -07:00
Родитель 7f5809cae8
Коммит eaec88e971
1 изменённых файлов: 16 добавлений и 27 удалений

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

@ -82,6 +82,7 @@
#include "threading/ConditionVariable.h"
#include "threading/LockGuard.h"
#include "threading/Mutex.h"
#include "threading/Thread.h"
#include "vm/ArgumentsObject.h"
#include "vm/Compression.h"
#include "vm/Debugger.h"
@ -172,7 +173,7 @@ struct ShellRuntime
*/
Mutex watchdogLock;
ConditionVariable watchdogWakeup;
PRThread* watchdogThread;
Maybe<Thread> watchdogThread;
Maybe<TimeStamp> watchdogTimeout;
ConditionVariable sleepWakeup;
@ -315,7 +316,6 @@ ShellRuntime::ShellRuntime(JSRuntime* rt)
#ifdef SPIDERMONKEY_PROMISE
promiseRejectionTrackerCallback(rt, NullValue()),
#endif // SPIDERMONKEY_PROMISE
watchdogThread(nullptr),
exitCode(0),
quitting(false),
gotError(false)
@ -3113,36 +3113,33 @@ static void
KillWatchdog(JSRuntime* rt)
{
ShellRuntime* sr = GetShellRuntime(rt);
PRThread* thread;
Maybe<Thread> thread;
{
LockGuard<Mutex> guard(sr->watchdogLock);
thread = sr->watchdogThread;
Swap(sr->watchdogThread, thread);
if (thread) {
/*
* The watchdog thread is running, tell it to terminate waking it up
* if necessary.
*/
sr->watchdogThread = nullptr;
// The watchdog thread becoming Nothing is its signal to exit.
sr->watchdogWakeup.notify_one();
}
}
if (thread)
PR_JoinThread(thread);
thread->join();
MOZ_ASSERT(!sr->watchdogThread);
}
static void
WatchdogMain(void* arg)
WatchdogMain(JSRuntime* rt)
{
PR_SetCurrentThreadName("JS Watchdog");
ThisThread::SetName("JS Watchdog");
JSRuntime* rt = (JSRuntime*) arg;
ShellRuntime* sr = GetShellRuntime(rt);
LockGuard<Mutex> guard(sr->watchdogLock);
while (sr->watchdogThread) {
auto now = TimeStamp::Now();
if (sr->watchdogTimeout.isSome() && now >= sr->watchdogTimeout.value()) {
if (sr->watchdogTimeout && now >= sr->watchdogTimeout.value()) {
/*
* The timeout has just expired. Request an interrupt callback
* outside the lock.
@ -3156,7 +3153,7 @@ WatchdogMain(void* arg)
/* Wake up any threads doing sleep. */
sr->sleepWakeup.notify_all();
} else {
if (sr->watchdogTimeout.isSome()) {
if (sr->watchdogTimeout) {
/*
* Time hasn't expired yet. Simulate an interrupt callback
* which doesn't abort execution.
@ -3164,7 +3161,7 @@ WatchdogMain(void* arg)
JS_RequestInterruptCallback(rt);
}
TimeDuration sleepDuration = sr->watchdogTimeout.isSome()
TimeDuration sleepDuration = sr->watchdogTimeout
? TimeDuration::FromSeconds(0.1)
: TimeDuration::Forever();
sr->watchdogWakeup.wait_for(guard, sleepDuration);
@ -3187,17 +3184,9 @@ ScheduleWatchdog(JSRuntime* rt, double t)
auto timeout = TimeStamp::Now() + interval;
LockGuard<Mutex> guard(sr->watchdogLock);
if (!sr->watchdogThread) {
MOZ_ASSERT(sr->watchdogTimeout.isNothing());
sr->watchdogThread = PR_CreateThread(PR_USER_THREAD,
WatchdogMain,
rt,
PR_PRIORITY_NORMAL,
PR_GLOBAL_THREAD,
PR_JOINABLE_THREAD,
0);
if (!sr->watchdogThread)
return false;
} else if (sr->watchdogTimeout.isNothing() || timeout < sr->watchdogTimeout.value()) {
MOZ_ASSERT(!sr->watchdogTimeout);
sr->watchdogThread.emplace(WatchdogMain, rt);
} else if (!sr->watchdogTimeout || timeout < sr->watchdogTimeout.value()) {
sr->watchdogWakeup.notify_one();
}
sr->watchdogTimeout = Some(timeout);