//static void XPCJSRuntime::WatchdogMain(void *arg) { XPCJSRuntime* self = static_cast<XPCJSRuntime*>(arg); // Lock lasts until we return AutoLockJSGC lock(self->mJSRuntime); while (self->mWatchdogThread) { #ifdef DEBUG PRStatus status = #endif PR_WaitCondVar(self->mWatchdogWakeup, PR_TicksPerSecond()); JS_ASSERT(status == PR_SUCCESS); JSContext* cx = nsnull; while((cx = js_NextActiveContext(self->mJSRuntime, cx))) { JS_TriggerOperationCallback(cx); } } /* Wake up the main thread waiting for the watchdog to terminate. */ PR_NotifyCondVar(self->mWatchdogWakeup); }
//static void XPCJSRuntime::WatchdogMain(void *arg) { XPCJSRuntime* self = static_cast<XPCJSRuntime*>(arg); // Lock lasts until we return AutoLockJSGC lock(self->mJSRuntime); PRIntervalTime sleepInterval; while (self->mWatchdogThread) { // Sleep only 1 second if recently (or currently) active; otherwise, hibernate if (self->mLastActiveTime == -1 || PR_Now() - self->mLastActiveTime <= 2*PR_USEC_PER_SEC) sleepInterval = PR_TicksPerSecond(); else { sleepInterval = PR_INTERVAL_NO_TIMEOUT; self->mWatchdogHibernating = PR_TRUE; } #ifdef DEBUG PRStatus status = #endif PR_WaitCondVar(self->mWatchdogWakeup, sleepInterval); JS_ASSERT(status == PR_SUCCESS); JSContext* cx = nsnull; while((cx = js_NextActiveContext(self->mJSRuntime, cx))) { JS_TriggerOperationCallback(cx); } } /* Wake up the main thread waiting for the watchdog to terminate. */ PR_NotifyCondVar(self->mWatchdogWakeup); }
/** Thread for triggering closures registered with gpsee_addAsyncCallback() */ static void gpsee_asyncCallbackTriggerThreadFunc(void *grt_vp) { gpsee_runtime_t *grt = (gpsee_runtime_t *) grt_vp; GPSEEAsyncCallback *cb; /* Run this loop as long as there are async callbacks registered */ do { JSContext *cx; /* Acquire mutex protecting grt->asyncCallbacks */ PR_Lock(grt->asyncCallbacks_lock); /* Grab the head of the list */ cb = grt->asyncCallbacks; if (cb) { /* Grab the JSContext from the head */ cx = cb->cx; /* Trigger operation callbacks on the first context */ JS_TriggerOperationCallback(cx); /* Iterate over each operation callback */ while ((cb = cb->next)) { /* Trigger operation callback on each new context found */ if (cx != cb->cx) { cx = cb->cx; JS_TriggerOperationCallback(cx); } } } /* Relinquish mutex */ PR_Unlock(grt->asyncCallbacks_lock); /* Sleep for a bit; interrupted by PR_Interrupt() */ PR_Sleep(PR_INTERVAL_MIN); // TODO this should be configurable!! } while (grt->asyncCallbacks); }
void *js_watchdog(void *ptr) { struct watchdog *wd = (struct watchdog *) ptr; struct pollfd poller; poller.fd = wd->pipe[1]; poller.events = POLLIN; int retval = poll(&poller, 1, wd->timeout * 1000); if (retval <= 0) { JS_TriggerOperationCallback(wd->context); } return NULL; }