Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "WorkerRunnable.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIEventTarget.h" |
michael@0 | 9 | #include "nsIRunnable.h" |
michael@0 | 10 | #include "nsThreadUtils.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "mozilla/DebugOnly.h" |
michael@0 | 13 | |
michael@0 | 14 | #include "js/RootingAPI.h" |
michael@0 | 15 | #include "js/Value.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "WorkerPrivate.h" |
michael@0 | 18 | |
michael@0 | 19 | USING_WORKERS_NAMESPACE |
michael@0 | 20 | |
michael@0 | 21 | namespace { |
michael@0 | 22 | |
michael@0 | 23 | const nsIID kWorkerRunnableIID = { |
michael@0 | 24 | 0x320cc0b5, 0xef12, 0x4084, { 0x88, 0x6e, 0xca, 0x6a, 0x81, 0xe4, 0x1d, 0x68 } |
michael@0 | 25 | }; |
michael@0 | 26 | |
michael@0 | 27 | void |
michael@0 | 28 | MaybeReportMainThreadException(JSContext* aCx, bool aResult) |
michael@0 | 29 | { |
michael@0 | 30 | AssertIsOnMainThread(); |
michael@0 | 31 | |
michael@0 | 32 | if (aCx && !aResult) { |
michael@0 | 33 | JS_ReportPendingException(aCx); |
michael@0 | 34 | } |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | } // anonymous namespace |
michael@0 | 38 | |
michael@0 | 39 | #ifdef DEBUG |
michael@0 | 40 | WorkerRunnable::WorkerRunnable(WorkerPrivate* aWorkerPrivate, |
michael@0 | 41 | TargetAndBusyBehavior aBehavior) |
michael@0 | 42 | : mWorkerPrivate(aWorkerPrivate), mBehavior(aBehavior), mCanceled(0), |
michael@0 | 43 | mCallingCancelWithinRun(false) |
michael@0 | 44 | { |
michael@0 | 45 | MOZ_ASSERT(aWorkerPrivate); |
michael@0 | 46 | } |
michael@0 | 47 | #endif |
michael@0 | 48 | |
michael@0 | 49 | bool |
michael@0 | 50 | WorkerRunnable::PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate) |
michael@0 | 51 | { |
michael@0 | 52 | #ifdef DEBUG |
michael@0 | 53 | MOZ_ASSERT(aWorkerPrivate); |
michael@0 | 54 | |
michael@0 | 55 | switch (mBehavior) { |
michael@0 | 56 | case ParentThreadUnchangedBusyCount: |
michael@0 | 57 | aWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 58 | break; |
michael@0 | 59 | |
michael@0 | 60 | case WorkerThreadModifyBusyCount: |
michael@0 | 61 | aWorkerPrivate->AssertIsOnParentThread(); |
michael@0 | 62 | MOZ_ASSERT(aCx); |
michael@0 | 63 | break; |
michael@0 | 64 | |
michael@0 | 65 | case WorkerThreadUnchangedBusyCount: |
michael@0 | 66 | aWorkerPrivate->AssertIsOnParentThread(); |
michael@0 | 67 | break; |
michael@0 | 68 | |
michael@0 | 69 | default: |
michael@0 | 70 | MOZ_ASSUME_UNREACHABLE("Unknown behavior!"); |
michael@0 | 71 | } |
michael@0 | 72 | #endif |
michael@0 | 73 | |
michael@0 | 74 | if (mBehavior == WorkerThreadModifyBusyCount) { |
michael@0 | 75 | return aWorkerPrivate->ModifyBusyCount(aCx, true); |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | return true; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | bool |
michael@0 | 82 | WorkerRunnable::Dispatch(JSContext* aCx) |
michael@0 | 83 | { |
michael@0 | 84 | bool ok; |
michael@0 | 85 | |
michael@0 | 86 | if (!aCx) { |
michael@0 | 87 | ok = PreDispatch(nullptr, mWorkerPrivate); |
michael@0 | 88 | if (ok) { |
michael@0 | 89 | ok = DispatchInternal(); |
michael@0 | 90 | } |
michael@0 | 91 | PostDispatch(nullptr, mWorkerPrivate, ok); |
michael@0 | 92 | return ok; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | JSAutoRequest ar(aCx); |
michael@0 | 96 | |
michael@0 | 97 | JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx)); |
michael@0 | 98 | |
michael@0 | 99 | Maybe<JSAutoCompartment> ac; |
michael@0 | 100 | if (global) { |
michael@0 | 101 | ac.construct(aCx, global); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | ok = PreDispatch(aCx, mWorkerPrivate); |
michael@0 | 105 | |
michael@0 | 106 | if (ok && !DispatchInternal()) { |
michael@0 | 107 | ok = false; |
michael@0 | 108 | } |
michael@0 | 109 | |
michael@0 | 110 | PostDispatch(aCx, mWorkerPrivate, ok); |
michael@0 | 111 | |
michael@0 | 112 | return ok; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | bool |
michael@0 | 116 | WorkerRunnable::DispatchInternal() |
michael@0 | 117 | { |
michael@0 | 118 | if (mBehavior == WorkerThreadModifyBusyCount || |
michael@0 | 119 | mBehavior == WorkerThreadUnchangedBusyCount) { |
michael@0 | 120 | return NS_SUCCEEDED(mWorkerPrivate->Dispatch(this)); |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | MOZ_ASSERT(mBehavior == ParentThreadUnchangedBusyCount); |
michael@0 | 124 | |
michael@0 | 125 | if (WorkerPrivate* parent = mWorkerPrivate->GetParent()) { |
michael@0 | 126 | return NS_SUCCEEDED(parent->Dispatch(this)); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); |
michael@0 | 130 | MOZ_ASSERT(mainThread); |
michael@0 | 131 | |
michael@0 | 132 | return NS_SUCCEEDED(mainThread->Dispatch(this, NS_DISPATCH_NORMAL)); |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | void |
michael@0 | 136 | WorkerRunnable::PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate, |
michael@0 | 137 | bool aDispatchResult) |
michael@0 | 138 | { |
michael@0 | 139 | MOZ_ASSERT(aWorkerPrivate); |
michael@0 | 140 | |
michael@0 | 141 | #ifdef DEBUG |
michael@0 | 142 | switch (mBehavior) { |
michael@0 | 143 | case ParentThreadUnchangedBusyCount: |
michael@0 | 144 | aWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 145 | break; |
michael@0 | 146 | |
michael@0 | 147 | case WorkerThreadModifyBusyCount: |
michael@0 | 148 | aWorkerPrivate->AssertIsOnParentThread(); |
michael@0 | 149 | MOZ_ASSERT(aCx); |
michael@0 | 150 | break; |
michael@0 | 151 | |
michael@0 | 152 | case WorkerThreadUnchangedBusyCount: |
michael@0 | 153 | aWorkerPrivate->AssertIsOnParentThread(); |
michael@0 | 154 | break; |
michael@0 | 155 | |
michael@0 | 156 | default: |
michael@0 | 157 | MOZ_ASSUME_UNREACHABLE("Unknown behavior!"); |
michael@0 | 158 | } |
michael@0 | 159 | #endif |
michael@0 | 160 | |
michael@0 | 161 | if (!aDispatchResult) { |
michael@0 | 162 | if (mBehavior == WorkerThreadModifyBusyCount) { |
michael@0 | 163 | aWorkerPrivate->ModifyBusyCount(aCx, false); |
michael@0 | 164 | } |
michael@0 | 165 | if (aCx) { |
michael@0 | 166 | JS_ReportPendingException(aCx); |
michael@0 | 167 | } |
michael@0 | 168 | } |
michael@0 | 169 | } |
michael@0 | 170 | |
michael@0 | 171 | void |
michael@0 | 172 | WorkerRunnable::PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, |
michael@0 | 173 | bool aRunResult) |
michael@0 | 174 | { |
michael@0 | 175 | MOZ_ASSERT(aCx); |
michael@0 | 176 | MOZ_ASSERT(aWorkerPrivate); |
michael@0 | 177 | |
michael@0 | 178 | #ifdef DEBUG |
michael@0 | 179 | switch (mBehavior) { |
michael@0 | 180 | case ParentThreadUnchangedBusyCount: |
michael@0 | 181 | aWorkerPrivate->AssertIsOnParentThread(); |
michael@0 | 182 | break; |
michael@0 | 183 | |
michael@0 | 184 | case WorkerThreadModifyBusyCount: |
michael@0 | 185 | aWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 186 | break; |
michael@0 | 187 | |
michael@0 | 188 | case WorkerThreadUnchangedBusyCount: |
michael@0 | 189 | aWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 190 | break; |
michael@0 | 191 | |
michael@0 | 192 | default: |
michael@0 | 193 | MOZ_ASSUME_UNREACHABLE("Unknown behavior!"); |
michael@0 | 194 | } |
michael@0 | 195 | #endif |
michael@0 | 196 | |
michael@0 | 197 | if (mBehavior == WorkerThreadModifyBusyCount) { |
michael@0 | 198 | if (!aWorkerPrivate->ModifyBusyCountFromWorker(aCx, false)) { |
michael@0 | 199 | aRunResult = false; |
michael@0 | 200 | } |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | if (!aRunResult) { |
michael@0 | 204 | JS_ReportPendingException(aCx); |
michael@0 | 205 | } |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | // static |
michael@0 | 209 | WorkerRunnable* |
michael@0 | 210 | WorkerRunnable::FromRunnable(nsIRunnable* aRunnable) |
michael@0 | 211 | { |
michael@0 | 212 | MOZ_ASSERT(aRunnable); |
michael@0 | 213 | |
michael@0 | 214 | WorkerRunnable* runnable; |
michael@0 | 215 | nsresult rv = aRunnable->QueryInterface(kWorkerRunnableIID, |
michael@0 | 216 | reinterpret_cast<void**>(&runnable)); |
michael@0 | 217 | if (NS_FAILED(rv)) { |
michael@0 | 218 | return nullptr; |
michael@0 | 219 | } |
michael@0 | 220 | |
michael@0 | 221 | MOZ_ASSERT(runnable); |
michael@0 | 222 | return runnable; |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | NS_IMPL_ADDREF(WorkerRunnable) |
michael@0 | 226 | NS_IMPL_RELEASE(WorkerRunnable) |
michael@0 | 227 | |
michael@0 | 228 | NS_INTERFACE_MAP_BEGIN(WorkerRunnable) |
michael@0 | 229 | NS_INTERFACE_MAP_ENTRY(nsIRunnable) |
michael@0 | 230 | NS_INTERFACE_MAP_ENTRY(nsICancelableRunnable) |
michael@0 | 231 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 232 | // kWorkerRunnableIID is special in that it does not AddRef its result. |
michael@0 | 233 | if (aIID.Equals(kWorkerRunnableIID)) { |
michael@0 | 234 | *aInstancePtr = this; |
michael@0 | 235 | return NS_OK; |
michael@0 | 236 | } |
michael@0 | 237 | else |
michael@0 | 238 | NS_INTERFACE_MAP_END |
michael@0 | 239 | |
michael@0 | 240 | NS_IMETHODIMP |
michael@0 | 241 | WorkerRunnable::Run() |
michael@0 | 242 | { |
michael@0 | 243 | bool targetIsWorkerThread = mBehavior == WorkerThreadModifyBusyCount || |
michael@0 | 244 | mBehavior == WorkerThreadUnchangedBusyCount; |
michael@0 | 245 | |
michael@0 | 246 | #ifdef DEBUG |
michael@0 | 247 | MOZ_ASSERT_IF(mCallingCancelWithinRun, targetIsWorkerThread); |
michael@0 | 248 | if (targetIsWorkerThread) { |
michael@0 | 249 | mWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 250 | } |
michael@0 | 251 | else { |
michael@0 | 252 | MOZ_ASSERT(mBehavior == ParentThreadUnchangedBusyCount); |
michael@0 | 253 | mWorkerPrivate->AssertIsOnParentThread(); |
michael@0 | 254 | } |
michael@0 | 255 | #endif |
michael@0 | 256 | |
michael@0 | 257 | if (IsCanceled() && !mCallingCancelWithinRun) { |
michael@0 | 258 | return NS_OK; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | JSContext* cx; |
michael@0 | 262 | nsRefPtr<WorkerPrivate> kungFuDeathGrip; |
michael@0 | 263 | nsCxPusher pusher; |
michael@0 | 264 | |
michael@0 | 265 | if (targetIsWorkerThread) { |
michael@0 | 266 | if (mWorkerPrivate->AllPendingRunnablesShouldBeCanceled() && |
michael@0 | 267 | !IsCanceled() && |
michael@0 | 268 | !mCallingCancelWithinRun) { |
michael@0 | 269 | |
michael@0 | 270 | // Prevent recursion. |
michael@0 | 271 | mCallingCancelWithinRun = true; |
michael@0 | 272 | |
michael@0 | 273 | Cancel(); |
michael@0 | 274 | |
michael@0 | 275 | MOZ_ASSERT(mCallingCancelWithinRun); |
michael@0 | 276 | mCallingCancelWithinRun = false; |
michael@0 | 277 | |
michael@0 | 278 | MOZ_ASSERT(IsCanceled(), "Subclass Cancel() didn't set IsCanceled()!"); |
michael@0 | 279 | |
michael@0 | 280 | return NS_OK; |
michael@0 | 281 | } |
michael@0 | 282 | |
michael@0 | 283 | cx = mWorkerPrivate->GetJSContext(); |
michael@0 | 284 | MOZ_ASSERT(cx); |
michael@0 | 285 | } |
michael@0 | 286 | else { |
michael@0 | 287 | cx = mWorkerPrivate->ParentJSContext(); |
michael@0 | 288 | MOZ_ASSERT(cx); |
michael@0 | 289 | |
michael@0 | 290 | kungFuDeathGrip = mWorkerPrivate; |
michael@0 | 291 | |
michael@0 | 292 | if (!mWorkerPrivate->GetParent()) { |
michael@0 | 293 | AssertIsOnMainThread(); |
michael@0 | 294 | pusher.Push(cx); |
michael@0 | 295 | } |
michael@0 | 296 | } |
michael@0 | 297 | |
michael@0 | 298 | JSAutoRequest ar(cx); |
michael@0 | 299 | |
michael@0 | 300 | JS::Rooted<JSObject*> targetCompartmentObject(cx); |
michael@0 | 301 | if (targetIsWorkerThread) { |
michael@0 | 302 | targetCompartmentObject = JS::CurrentGlobalOrNull(cx); |
michael@0 | 303 | } else { |
michael@0 | 304 | targetCompartmentObject = mWorkerPrivate->GetWrapper(); |
michael@0 | 305 | } |
michael@0 | 306 | |
michael@0 | 307 | Maybe<JSAutoCompartment> ac; |
michael@0 | 308 | if (targetCompartmentObject) { |
michael@0 | 309 | ac.construct(cx, targetCompartmentObject); |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | bool result = WorkerRun(cx, mWorkerPrivate); |
michael@0 | 313 | |
michael@0 | 314 | // In the case of CompileScriptRunnnable, WorkerRun above can cause us to |
michael@0 | 315 | // lazily create a global, in which case we need to be in its compartment |
michael@0 | 316 | // when calling PostRun() below. Maybe<> this time... |
michael@0 | 317 | if (targetIsWorkerThread && |
michael@0 | 318 | ac.empty() && |
michael@0 | 319 | js::DefaultObjectForContextOrNull(cx)) { |
michael@0 | 320 | ac.construct(cx, js::DefaultObjectForContextOrNull(cx)); |
michael@0 | 321 | } |
michael@0 | 322 | |
michael@0 | 323 | PostRun(cx, mWorkerPrivate, result); |
michael@0 | 324 | |
michael@0 | 325 | return result ? NS_OK : NS_ERROR_FAILURE; |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | NS_IMETHODIMP |
michael@0 | 329 | WorkerRunnable::Cancel() |
michael@0 | 330 | { |
michael@0 | 331 | uint32_t canceledCount = ++mCanceled; |
michael@0 | 332 | |
michael@0 | 333 | MOZ_ASSERT(canceledCount, "Cancel() overflow!"); |
michael@0 | 334 | |
michael@0 | 335 | // The docs say that Cancel() should not be called more than once and that we |
michael@0 | 336 | // should throw NS_ERROR_UNEXPECTED if it is. |
michael@0 | 337 | return (canceledCount == 1) ? NS_OK : NS_ERROR_UNEXPECTED; |
michael@0 | 338 | } |
michael@0 | 339 | |
michael@0 | 340 | WorkerSyncRunnable::WorkerSyncRunnable(WorkerPrivate* aWorkerPrivate, |
michael@0 | 341 | nsIEventTarget* aSyncLoopTarget) |
michael@0 | 342 | : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount), |
michael@0 | 343 | mSyncLoopTarget(aSyncLoopTarget) |
michael@0 | 344 | { |
michael@0 | 345 | #ifdef DEBUG |
michael@0 | 346 | if (mSyncLoopTarget) { |
michael@0 | 347 | mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget); |
michael@0 | 348 | } |
michael@0 | 349 | #endif |
michael@0 | 350 | } |
michael@0 | 351 | |
michael@0 | 352 | WorkerSyncRunnable::WorkerSyncRunnable( |
michael@0 | 353 | WorkerPrivate* aWorkerPrivate, |
michael@0 | 354 | already_AddRefed<nsIEventTarget>&& aSyncLoopTarget) |
michael@0 | 355 | : WorkerRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount), |
michael@0 | 356 | mSyncLoopTarget(aSyncLoopTarget) |
michael@0 | 357 | { |
michael@0 | 358 | #ifdef DEBUG |
michael@0 | 359 | if (mSyncLoopTarget) { |
michael@0 | 360 | mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget); |
michael@0 | 361 | } |
michael@0 | 362 | #endif |
michael@0 | 363 | } |
michael@0 | 364 | |
michael@0 | 365 | WorkerSyncRunnable::~WorkerSyncRunnable() |
michael@0 | 366 | { |
michael@0 | 367 | } |
michael@0 | 368 | |
michael@0 | 369 | bool |
michael@0 | 370 | WorkerSyncRunnable::DispatchInternal() |
michael@0 | 371 | { |
michael@0 | 372 | if (mSyncLoopTarget) { |
michael@0 | 373 | return NS_SUCCEEDED(mSyncLoopTarget->Dispatch(this, NS_DISPATCH_NORMAL)); |
michael@0 | 374 | } |
michael@0 | 375 | |
michael@0 | 376 | return WorkerRunnable::DispatchInternal(); |
michael@0 | 377 | } |
michael@0 | 378 | |
michael@0 | 379 | void |
michael@0 | 380 | MainThreadWorkerSyncRunnable::PostDispatch(JSContext* aCx, |
michael@0 | 381 | WorkerPrivate* aWorkerPrivate, |
michael@0 | 382 | bool aDispatchResult) |
michael@0 | 383 | { |
michael@0 | 384 | MaybeReportMainThreadException(aCx, aDispatchResult); |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | StopSyncLoopRunnable::StopSyncLoopRunnable( |
michael@0 | 388 | WorkerPrivate* aWorkerPrivate, |
michael@0 | 389 | already_AddRefed<nsIEventTarget>&& aSyncLoopTarget, |
michael@0 | 390 | bool aResult) |
michael@0 | 391 | : WorkerSyncRunnable(aWorkerPrivate, Move(aSyncLoopTarget)), mResult(aResult) |
michael@0 | 392 | { |
michael@0 | 393 | #ifdef DEBUG |
michael@0 | 394 | mWorkerPrivate->AssertValidSyncLoop(mSyncLoopTarget); |
michael@0 | 395 | #endif |
michael@0 | 396 | } |
michael@0 | 397 | |
michael@0 | 398 | NS_IMETHODIMP |
michael@0 | 399 | StopSyncLoopRunnable::Cancel() |
michael@0 | 400 | { |
michael@0 | 401 | nsresult rv = Run(); |
michael@0 | 402 | NS_WARN_IF_FALSE(NS_SUCCEEDED(rv), "Run() failed"); |
michael@0 | 403 | |
michael@0 | 404 | nsresult rv2 = WorkerSyncRunnable::Cancel(); |
michael@0 | 405 | NS_WARN_IF_FALSE(NS_SUCCEEDED(rv2), "Cancel() failed"); |
michael@0 | 406 | |
michael@0 | 407 | return NS_FAILED(rv) ? rv : rv2; |
michael@0 | 408 | } |
michael@0 | 409 | |
michael@0 | 410 | bool |
michael@0 | 411 | StopSyncLoopRunnable::WorkerRun(JSContext* aCx, |
michael@0 | 412 | WorkerPrivate* aWorkerPrivate) |
michael@0 | 413 | { |
michael@0 | 414 | aWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 415 | MOZ_ASSERT(mSyncLoopTarget); |
michael@0 | 416 | |
michael@0 | 417 | nsCOMPtr<nsIEventTarget> syncLoopTarget; |
michael@0 | 418 | mSyncLoopTarget.swap(syncLoopTarget); |
michael@0 | 419 | |
michael@0 | 420 | if (!mResult) { |
michael@0 | 421 | MaybeSetException(aCx); |
michael@0 | 422 | } |
michael@0 | 423 | |
michael@0 | 424 | aWorkerPrivate->StopSyncLoop(syncLoopTarget, mResult); |
michael@0 | 425 | return true; |
michael@0 | 426 | } |
michael@0 | 427 | |
michael@0 | 428 | bool |
michael@0 | 429 | StopSyncLoopRunnable::DispatchInternal() |
michael@0 | 430 | { |
michael@0 | 431 | MOZ_ASSERT(mSyncLoopTarget); |
michael@0 | 432 | |
michael@0 | 433 | return NS_SUCCEEDED(mSyncLoopTarget->Dispatch(this, NS_DISPATCH_NORMAL)); |
michael@0 | 434 | } |
michael@0 | 435 | |
michael@0 | 436 | void |
michael@0 | 437 | MainThreadStopSyncLoopRunnable::PostDispatch(JSContext* aCx, |
michael@0 | 438 | WorkerPrivate* aWorkerPrivate, |
michael@0 | 439 | bool aDispatchResult) |
michael@0 | 440 | { |
michael@0 | 441 | MaybeReportMainThreadException(aCx, aDispatchResult); |
michael@0 | 442 | } |
michael@0 | 443 | |
michael@0 | 444 | #ifdef DEBUG |
michael@0 | 445 | WorkerControlRunnable::WorkerControlRunnable(WorkerPrivate* aWorkerPrivate, |
michael@0 | 446 | TargetAndBusyBehavior aBehavior) |
michael@0 | 447 | : WorkerRunnable(aWorkerPrivate, aBehavior) |
michael@0 | 448 | { |
michael@0 | 449 | MOZ_ASSERT(aWorkerPrivate); |
michael@0 | 450 | MOZ_ASSERT(aBehavior == ParentThreadUnchangedBusyCount || |
michael@0 | 451 | aBehavior == WorkerThreadUnchangedBusyCount, |
michael@0 | 452 | "WorkerControlRunnables should not modify the busy count"); |
michael@0 | 453 | } |
michael@0 | 454 | #endif |
michael@0 | 455 | |
michael@0 | 456 | bool |
michael@0 | 457 | WorkerControlRunnable::DispatchInternal() |
michael@0 | 458 | { |
michael@0 | 459 | if (mBehavior == WorkerThreadUnchangedBusyCount) { |
michael@0 | 460 | return NS_SUCCEEDED(mWorkerPrivate->DispatchControlRunnable(this)); |
michael@0 | 461 | } |
michael@0 | 462 | |
michael@0 | 463 | if (WorkerPrivate* parent = mWorkerPrivate->GetParent()) { |
michael@0 | 464 | return NS_SUCCEEDED(parent->DispatchControlRunnable(this)); |
michael@0 | 465 | } |
michael@0 | 466 | |
michael@0 | 467 | nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); |
michael@0 | 468 | MOZ_ASSERT(mainThread); |
michael@0 | 469 | |
michael@0 | 470 | return NS_SUCCEEDED(mainThread->Dispatch(this, NS_DISPATCH_NORMAL)); |
michael@0 | 471 | } |
michael@0 | 472 | |
michael@0 | 473 | void |
michael@0 | 474 | MainThreadWorkerControlRunnable::PostDispatch(JSContext* aCx, |
michael@0 | 475 | WorkerPrivate* aWorkerPrivate, |
michael@0 | 476 | bool aDispatchResult) |
michael@0 | 477 | { |
michael@0 | 478 | AssertIsOnMainThread(); |
michael@0 | 479 | |
michael@0 | 480 | if (aCx && !aDispatchResult) { |
michael@0 | 481 | JS_ReportPendingException(aCx); |
michael@0 | 482 | } |
michael@0 | 483 | } |
michael@0 | 484 | |
michael@0 | 485 | NS_IMPL_ISUPPORTS_INHERITED0(WorkerControlRunnable, WorkerRunnable) |
michael@0 | 486 | |
michael@0 | 487 | bool |
michael@0 | 488 | WorkerSameThreadRunnable::PreDispatch(JSContext* aCx, |
michael@0 | 489 | WorkerPrivate* aWorkerPrivate) |
michael@0 | 490 | { |
michael@0 | 491 | aWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 492 | return true; |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | void |
michael@0 | 496 | WorkerSameThreadRunnable::PostDispatch(JSContext* aCx, |
michael@0 | 497 | WorkerPrivate* aWorkerPrivate, |
michael@0 | 498 | bool aDispatchResult) |
michael@0 | 499 | { |
michael@0 | 500 | aWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 501 | if (aDispatchResult) { |
michael@0 | 502 | DebugOnly<bool> willIncrement = aWorkerPrivate->ModifyBusyCountFromWorker(aCx, true); |
michael@0 | 503 | // Should never fail since if this thread is still running, so should the |
michael@0 | 504 | // parent and it should be able to process a control runnable. |
michael@0 | 505 | MOZ_ASSERT(willIncrement); |
michael@0 | 506 | } |
michael@0 | 507 | } |
michael@0 | 508 | |
michael@0 | 509 | void |
michael@0 | 510 | WorkerSameThreadRunnable::PostRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate, |
michael@0 | 511 | bool aRunResult) |
michael@0 | 512 | { |
michael@0 | 513 | MOZ_ASSERT(aCx); |
michael@0 | 514 | MOZ_ASSERT(aWorkerPrivate); |
michael@0 | 515 | |
michael@0 | 516 | aWorkerPrivate->AssertIsOnWorkerThread(); |
michael@0 | 517 | |
michael@0 | 518 | DebugOnly<bool> willDecrement = aWorkerPrivate->ModifyBusyCountFromWorker(aCx, false); |
michael@0 | 519 | MOZ_ASSERT(willDecrement); |
michael@0 | 520 | |
michael@0 | 521 | if (!aRunResult) { |
michael@0 | 522 | JS_ReportPendingException(aCx); |
michael@0 | 523 | } |
michael@0 | 524 | } |
michael@0 | 525 |