Backed out changeset 8aa6b27297b6 (bug 956899)
authorSebastian Hengst <archaeopteryx@coole-files.de>
Sat, 28 May 2016 01:38:52 +0200
changeset 372398 a3a3aa23f4ef145bdc30278e092b77676e00babf
parent 372397 4224b7714822e13b16c0c6508f3d1b6090bb38a7
child 372399 3c86f1e4bbc95042068ec7b1e2246ab54239e1ce
push id19519
push userbmo:rail@mozilla.com
push dateSat, 28 May 2016 13:01:27 +0000
bugs956899
milestone49.0a1
backs out8aa6b27297b6aa94abb63b2430f7879f14dfa86a
Backed out changeset 8aa6b27297b6 (bug 956899)
js/src/shell/js.cpp
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -77,17 +77,16 @@
 #include "js/TrackedOptimizationInfo.h"
 #include "perf/jsperf.h"
 #include "shell/jsoptparse.h"
 #include "shell/jsshell.h"
 #include "shell/OSObject.h"
 #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"
 #include "vm/HelperThreads.h"
 #include "vm/Monitor.h"
 #include "vm/Shape.h"
 #include "vm/SharedArrayObject.h"
 #include "vm/StringBuffer.h"
@@ -168,17 +167,17 @@ struct ShellRuntime
     JS::PersistentRooted<JobQueue> jobQueue;
 #endif // SPIDERMONKEY_PROMISE
 
     /*
      * Watchdog thread state.
      */
     Mutex watchdogLock;
     ConditionVariable watchdogWakeup;
-    Maybe<Thread> watchdogThread;
+    PRThread* watchdogThread;
     Maybe<TimeStamp> watchdogTimeout;
 
     ConditionVariable sleepWakeup;
 
     int exitCode;
     bool quitting;
     bool gotError;
 };
@@ -311,16 +310,17 @@ ShellRuntime::ShellRuntime(JSRuntime* rt
     serviceInterrupt(false),
     haveInterruptFunc(false),
     interruptFunc(rt, NullValue()),
     lastWarningEnabled(false),
     lastWarning(rt, NullValue()),
 #ifdef SPIDERMONKEY_PROMISE
     promiseRejectionTrackerCallback(rt, NullValue()),
 #endif // SPIDERMONKEY_PROMISE
+    watchdogThread(nullptr),
     exitCode(0),
     quitting(false),
     gotError(false)
 {}
 
 static ShellRuntime*
 GetShellRuntime(JSRuntime *rt)
 {
@@ -3108,65 +3108,68 @@ Sleep_fn(JSContext* cx, unsigned argc, V
     args.rval().setUndefined();
     return !sr->serviceInterrupt;
 }
 
 static void
 KillWatchdog(JSRuntime* rt)
 {
     ShellRuntime* sr = GetShellRuntime(rt);
-    Maybe<Thread> thread;
+    PRThread* thread;
 
     {
         LockGuard<Mutex> guard(sr->watchdogLock);
-        Swap(sr->watchdogThread, thread);
+        thread = sr->watchdogThread;
         if (thread) {
-            // The watchdog thread becoming Nothing is its signal to exit.
+            /*
+             * The watchdog thread is running, tell it to terminate waking it up
+             * if necessary.
+             */
+            sr->watchdogThread = nullptr;
             sr->watchdogWakeup.notify_one();
         }
     }
     if (thread)
-        thread->join();
-
-    MOZ_ASSERT(!sr->watchdogThread);
+        PR_JoinThread(thread);
 }
 
 static void
-WatchdogMain(JSRuntime* rt)
-{
-    ThisThread::SetName("JS Watchdog");
-
+WatchdogMain(void* arg)
+{
+    PR_SetCurrentThreadName("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 && now >= sr->watchdogTimeout.value()) {
+        if (sr->watchdogTimeout.isSome() && now >= sr->watchdogTimeout.value()) {
             /*
              * The timeout has just expired. Request an interrupt callback
              * outside the lock.
              */
             sr->watchdogTimeout = Nothing();
             {
                 UnlockGuard<Mutex> unlock(guard);
                 CancelExecution(rt);
             }
 
             /* Wake up any threads doing sleep. */
             sr->sleepWakeup.notify_all();
         } else {
-            if (sr->watchdogTimeout) {
+            if (sr->watchdogTimeout.isSome()) {
                 /*
                  * Time hasn't expired yet. Simulate an interrupt callback
                  * which doesn't abort execution.
                  */
                 JS_RequestInterruptCallback(rt);
             }
 
-            TimeDuration sleepDuration = sr->watchdogTimeout
+            TimeDuration sleepDuration = sr->watchdogTimeout.isSome()
                                          ? TimeDuration::FromSeconds(0.1)
                                          : TimeDuration::Forever();
             sr->watchdogWakeup.wait_for(guard, sleepDuration);
         }
     }
 }
 
 static bool
@@ -3179,19 +3182,27 @@ ScheduleWatchdog(JSRuntime* rt, double t
         sr->watchdogTimeout = Nothing();
         return true;
     }
 
     auto interval = TimeDuration::FromSeconds(t);
     auto timeout = TimeStamp::Now() + interval;
     LockGuard<Mutex> guard(sr->watchdogLock);
     if (!sr->watchdogThread) {
-        MOZ_ASSERT(!sr->watchdogTimeout);
-        sr->watchdogThread.emplace(WatchdogMain, rt);
-    } else if (!sr->watchdogTimeout || timeout < sr->watchdogTimeout.value()) {
+        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()) {
          sr->watchdogWakeup.notify_one();
     }
     sr->watchdogTimeout = Some(timeout);
     return true;
 }
 
 static void
 KillWorkerThreads()