1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/dom/JSExecutionManager.h"
#include "WorkerCommon.h"
#include "WorkerPrivate.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/dom/DocGroup.h"
namespace mozilla::dom {
JSExecutionManager* JSExecutionManager::mCurrentMTManager;
const uint32_t kTimeSliceExpirationMS = 50;
using RequestState = JSExecutionManager::RequestState;
static StaticRefPtr<JSExecutionManager> sSABSerializationManager;
void JSExecutionManager::Initialize() {
if (StaticPrefs::dom_workers_serialized_sab_access()) {
sSABSerializationManager = MakeRefPtr<JSExecutionManager>(1);
}
}
void JSExecutionManager::Shutdown() { sSABSerializationManager = nullptr; }
JSExecutionManager* JSExecutionManager::GetSABSerializationManager() {
return sSABSerializationManager.get();
}
RequestState JSExecutionManager::RequestJSThreadExecution() {
if (NS_IsMainThread()) {
return RequestJSThreadExecutionMainThread();
}
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
if (!workerPrivate || workerPrivate->GetExecutionGranted()) {
return RequestState::ExecutingAlready;
}
MutexAutoLock lock(mExecutionQueueMutex);
MOZ_ASSERT(mMaxRunning >= mRunning);
if ((mExecutionQueue.size() + (mMainThreadAwaitingExecution ? 1 : 0)) <
size_t(mMaxRunning - mRunning)) {
// There's slots ready for things to run, execute right away.
workerPrivate->SetExecutionGranted(true);
workerPrivate->ScheduleTimeSliceExpiration(kTimeSliceExpirationMS);
mRunning++;
return RequestState::Granted;
}
mExecutionQueue.push_back(workerPrivate);
TimeStamp waitStart = TimeStamp::Now();
while (mRunning >= mMaxRunning || (workerPrivate != mExecutionQueue.front() ||
mMainThreadAwaitingExecution)) {
// If there is no slots available, the main thread is awaiting permission
// or we are not first in line for execution, wait until notified.
mExecutionQueueCondVar.Wait(TimeDuration::FromMilliseconds(500));
if ((TimeStamp::Now() - waitStart) > TimeDuration::FromSeconds(20)) {
// Crash so that these types of situations are actually caught in the
// crash reporter.
MOZ_CRASH();
}
}
workerPrivate->SetExecutionGranted(true);
workerPrivate->ScheduleTimeSliceExpiration(kTimeSliceExpirationMS);
mExecutionQueue.pop_front();
mRunning++;
if (mRunning < mMaxRunning) {
// If a thread woke up before that wasn't first in line it will have gone
// back to sleep, if there's more slots available, wake it now.
mExecutionQueueCondVar.NotifyAll();
}
return RequestState::Granted;
}
void JSExecutionManager::YieldJSThreadExecution() {
if (NS_IsMainThread()) {
MOZ_ASSERT(mMainThreadIsExecuting);
mMainThreadIsExecuting = false;
MutexAutoLock lock(mExecutionQueueMutex);
mRunning--;
mExecutionQueueCondVar.NotifyAll();
return;
}
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
if (!workerPrivate) {
return;
}
MOZ_ASSERT(workerPrivate->GetExecutionGranted());
workerPrivate->CancelTimeSliceExpiration();
MutexAutoLock lock(mExecutionQueueMutex);
mRunning--;
mExecutionQueueCondVar.NotifyAll();
workerPrivate->SetExecutionGranted(false);
}
bool JSExecutionManager::YieldJSThreadExecutionIfGranted() {
if (NS_IsMainThread()) {
if (mMainThreadIsExecuting) {
YieldJSThreadExecution();
return true;
}
return false;
}
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
if (workerPrivate && workerPrivate->GetExecutionGranted()) {
YieldJSThreadExecution();
return true;
}
return false;
}
RequestState JSExecutionManager::RequestJSThreadExecutionMainThread() {
MOZ_ASSERT(NS_IsMainThread());
if (mMainThreadIsExecuting) {
return RequestState::ExecutingAlready;
}
MutexAutoLock lock(mExecutionQueueMutex);
MOZ_ASSERT(mMaxRunning >= mRunning);
if ((mMaxRunning - mRunning) > 0) {
// If there's any slots available run, the main thread always takes
// precedence over any worker threads.
mRunning++;
mMainThreadIsExecuting = true;
return RequestState::Granted;
}
mMainThreadAwaitingExecution = true;
TimeStamp waitStart = TimeStamp::Now();
while (mRunning >= mMaxRunning) {
if ((TimeStamp::Now() - waitStart) > TimeDuration::FromSeconds(20)) {
// Crash so that these types of situations are actually caught in the
// crash reporter.
MOZ_CRASH();
}
mExecutionQueueCondVar.Wait(TimeDuration::FromMilliseconds(500));
}
mMainThreadAwaitingExecution = false;
mMainThreadIsExecuting = true;
mRunning++;
if (mRunning < mMaxRunning) {
// If a thread woke up before that wasn't first in line it will have gone
// back to sleep, if there's more slots available, wake it now.
mExecutionQueueCondVar.NotifyAll();
}
return RequestState::Granted;
}
AutoRequestJSThreadExecution::AutoRequestJSThreadExecution(
nsIGlobalObject* aGlobalObject, bool aIsMainThread) {
JSExecutionManager* manager = nullptr;
mIsMainThread = aIsMainThread;
if (mIsMainThread) {
mOldGrantingManager = JSExecutionManager::mCurrentMTManager;
nsPIDOMWindowInner* innerWindow = nullptr;
if (aGlobalObject) {
innerWindow = aGlobalObject->GetAsInnerWindow();
}
DocGroup* docGroup = nullptr;
if (innerWindow) {
docGroup = innerWindow->GetDocGroup();
}
if (docGroup) {
manager = docGroup->GetExecutionManager();
}
if (JSExecutionManager::mCurrentMTManager == manager) {
return;
}
if (JSExecutionManager::mCurrentMTManager) {
JSExecutionManager::mCurrentMTManager->YieldJSThreadExecution();
JSExecutionManager::mCurrentMTManager = nullptr;
}
} else {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
if (workerPrivate) {
manager = workerPrivate->GetExecutionManager();
}
}
if (manager &&
(manager->RequestJSThreadExecution() == RequestState::Granted)) {
if (NS_IsMainThread()) {
// Make sure we restore permission on destruction if needed.
JSExecutionManager::mCurrentMTManager = manager;
}
mExecutionGrantingManager = std::move(manager);
}
}
AutoYieldJSThreadExecution::AutoYieldJSThreadExecution() {
JSExecutionManager* manager = nullptr;
if (NS_IsMainThread()) {
manager = JSExecutionManager::mCurrentMTManager;
} else {
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
if (workerPrivate) {
manager = workerPrivate->GetExecutionManager();
}
}
if (manager && manager->YieldJSThreadExecutionIfGranted()) {
mExecutionGrantingManager = std::move(manager);
if (NS_IsMainThread()) {
JSExecutionManager::mCurrentMTManager = nullptr;
}
}
}
} // namespace mozilla::dom
|