2018-03-13 23:16:48 +03:00
|
|
|
/* -*- 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 "WorkerRef.h"
|
|
|
|
|
|
|
|
#include "mozilla/Unused.h"
|
|
|
|
#include "WorkerRunnable.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// This runnable is used to release the StrongWorkerRef on the worker thread
|
|
|
|
// when a ThreadSafeWorkerRef is released.
|
|
|
|
class ReleaseRefControlRunnable final : public WorkerControlRunnable {
|
|
|
|
public:
|
|
|
|
ReleaseRefControlRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
already_AddRefed<StrongWorkerRef> aRef)
|
2018-03-14 17:24:38 +03:00
|
|
|
: WorkerControlRunnable(aWorkerPrivate, WorkerThreadUnchangedBusyCount),
|
2018-05-30 22:15:35 +03:00
|
|
|
mRef(std::move(aRef)) {
|
2018-03-13 23:16:48 +03:00
|
|
|
MOZ_ASSERT(mRef);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PreDispatch(WorkerPrivate* aWorkerPrivate) override { return true; }
|
|
|
|
|
|
|
|
void PostDispatch(WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult) override {}
|
|
|
|
|
|
|
|
bool WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override {
|
|
|
|
mRef = nullptr;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<StrongWorkerRef> mRef;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2019-07-12 14:18:17 +03:00
|
|
|
// WorkerRef
|
2018-03-13 23:16:48 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
WorkerRef::WorkerRef(WorkerPrivate* aWorkerPrivate, const char* aName,
|
|
|
|
bool aIsPreventingShutdown)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate),
|
|
|
|
mName(aName),
|
|
|
|
mIsPreventingShutdown(aIsPreventingShutdown),
|
|
|
|
mHolding(false) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aName);
|
2018-03-13 23:16:48 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
2018-03-14 17:27:13 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
WorkerRef::~WorkerRef() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(WorkerRef);
|
|
|
|
ReleaseWorker();
|
|
|
|
}
|
2018-04-17 21:51:02 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
void WorkerRef::ReleaseWorker() {
|
|
|
|
if (mHolding) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
2018-03-14 17:27:13 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
mWorkerPrivate->RemoveWorkerRef(this);
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
|
|
|
|
mHolding = false;
|
2018-03-13 23:16:48 +03:00
|
|
|
}
|
2019-07-12 14:18:17 +03:00
|
|
|
}
|
2018-03-13 23:16:48 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
bool WorkerRef::HoldWorker(WorkerStatus aStatus) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(!mHolding);
|
2018-03-13 23:16:48 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
if (NS_WARN_IF(!mWorkerPrivate->AddWorkerRef(this, aStatus))) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-13 23:16:48 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
mHolding = true;
|
|
|
|
return true;
|
2018-03-13 23:16:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void WorkerRef::Notify() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(WorkerRef);
|
|
|
|
|
|
|
|
if (!mCallback) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-18 21:52:26 +03:00
|
|
|
std::function<void()> callback = std::move(mCallback);
|
|
|
|
MOZ_ASSERT(!mCallback);
|
2018-03-13 23:16:48 +03:00
|
|
|
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// WeakWorkerRef
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<WeakWorkerRef> WeakWorkerRef::Create(
|
2018-12-18 21:52:26 +03:00
|
|
|
WorkerPrivate* aWorkerPrivate, std::function<void()>&& aCallback) {
|
2018-03-13 23:16:48 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
RefPtr<WeakWorkerRef> ref = new WeakWorkerRef(aWorkerPrivate);
|
2019-07-12 14:18:17 +03:00
|
|
|
if (!ref->HoldWorker(Canceling)) {
|
2018-03-13 23:16:48 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-12-18 21:52:26 +03:00
|
|
|
ref->mCallback = std::move(aCallback);
|
2018-03-13 23:16:48 +03:00
|
|
|
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakWorkerRef::WeakWorkerRef(WorkerPrivate* aWorkerPrivate)
|
2019-07-12 14:18:17 +03:00
|
|
|
: WorkerRef(aWorkerPrivate, "WeakWorkerRef", false) {}
|
2018-03-13 23:16:48 +03:00
|
|
|
|
|
|
|
WeakWorkerRef::~WeakWorkerRef() = default;
|
|
|
|
|
|
|
|
void WeakWorkerRef::Notify() {
|
2019-07-12 14:18:17 +03:00
|
|
|
MOZ_ASSERT(mHolding);
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
|
|
|
// Notify could drop the last reference to this object. We must keep it alive
|
|
|
|
// in order to call ReleaseWorker() immediately after.
|
|
|
|
RefPtr<WeakWorkerRef> kungFuGrip = this;
|
2018-03-27 19:30:38 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
WorkerRef::Notify();
|
|
|
|
ReleaseWorker();
|
2018-03-13 23:16:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* WeakWorkerRef::GetPrivate() const {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(WeakWorkerRef);
|
|
|
|
return mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:27:13 +03:00
|
|
|
WorkerPrivate* WeakWorkerRef::GetUnsafePrivate() const {
|
|
|
|
return mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:16:48 +03:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// StrongWorkerRef
|
|
|
|
|
2019-02-26 01:05:29 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<StrongWorkerRef> StrongWorkerRef::Create(
|
2019-03-05 02:05:34 +03:00
|
|
|
WorkerPrivate* const aWorkerPrivate, const char* const aName,
|
2018-12-18 21:52:26 +03:00
|
|
|
std::function<void()>&& aCallback) {
|
2019-03-05 02:05:34 +03:00
|
|
|
if (RefPtr<StrongWorkerRef> ref =
|
|
|
|
CreateImpl(aWorkerPrivate, aName, Canceling)) {
|
|
|
|
ref->mCallback = std::move(aCallback);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<StrongWorkerRef> StrongWorkerRef::CreateForcibly(
|
|
|
|
WorkerPrivate* const aWorkerPrivate, const char* const aName) {
|
|
|
|
return CreateImpl(aWorkerPrivate, aName, Killing);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<StrongWorkerRef> StrongWorkerRef::CreateImpl(
|
|
|
|
WorkerPrivate* const aWorkerPrivate, const char* const aName,
|
|
|
|
WorkerStatus const aFailStatus) {
|
2018-03-13 23:16:48 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
MOZ_ASSERT(aName);
|
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
RefPtr<StrongWorkerRef> ref = new StrongWorkerRef(aWorkerPrivate, aName);
|
|
|
|
if (!ref->HoldWorker(aFailStatus)) {
|
2018-03-13 23:16:48 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
StrongWorkerRef::StrongWorkerRef(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const char* aName)
|
|
|
|
: WorkerRef(aWorkerPrivate, aName, true) {}
|
2018-03-13 23:16:48 +03:00
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
StrongWorkerRef::~StrongWorkerRef() = default;
|
2018-03-13 23:16:48 +03:00
|
|
|
|
|
|
|
WorkerPrivate* StrongWorkerRef::Private() const {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(StrongWorkerRef);
|
|
|
|
return mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
2018-03-27 19:30:38 +03:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// ThreadSafeWorkerRef
|
|
|
|
|
2018-03-13 23:16:48 +03:00
|
|
|
ThreadSafeWorkerRef::ThreadSafeWorkerRef(StrongWorkerRef* aRef) : mRef(aRef) {
|
|
|
|
MOZ_ASSERT(aRef);
|
|
|
|
aRef->Private()->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadSafeWorkerRef::~ThreadSafeWorkerRef() {
|
|
|
|
// Let's release the StrongWorkerRef on the correct thread.
|
|
|
|
if (!mRef->mWorkerPrivate->IsOnWorkerThread()) {
|
2018-03-14 17:24:38 +03:00
|
|
|
WorkerPrivate* workerPrivate = mRef->mWorkerPrivate;
|
2018-03-13 23:16:48 +03:00
|
|
|
RefPtr<ReleaseRefControlRunnable> r =
|
2018-03-14 17:24:38 +03:00
|
|
|
new ReleaseRefControlRunnable(workerPrivate, mRef.forget());
|
2018-03-13 23:16:48 +03:00
|
|
|
r->Dispatch();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* ThreadSafeWorkerRef::Private() const {
|
|
|
|
return mRef->mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:16:03 +03:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// IPCWorkerRef
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<IPCWorkerRef> IPCWorkerRef::Create(
|
|
|
|
WorkerPrivate* aWorkerPrivate, const char* aName,
|
|
|
|
std::function<void()>&& aCallback) {
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
RefPtr<IPCWorkerRef> ref = new IPCWorkerRef(aWorkerPrivate, aName);
|
|
|
|
if (!ref->HoldWorker(Canceling)) {
|
2019-07-12 14:16:03 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ref->mCallback = std::move(aCallback);
|
|
|
|
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2019-07-12 14:18:17 +03:00
|
|
|
IPCWorkerRef::IPCWorkerRef(WorkerPrivate* aWorkerPrivate, const char* aName)
|
|
|
|
: WorkerRef(aWorkerPrivate, aName, false) {}
|
2019-07-12 14:16:03 +03:00
|
|
|
|
|
|
|
IPCWorkerRef::~IPCWorkerRef() = default;
|
|
|
|
|
|
|
|
WorkerPrivate* IPCWorkerRef::Private() const {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(IPCWorkerRef);
|
|
|
|
return mWorkerPrivate;
|
|
|
|
}
|
|
|
|
|
2018-03-13 23:16:48 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|