Skip to content

Commit

Permalink
Bug 1725942 - Part 3: Migrate LockManager implementation to PLockMana…
Browse files Browse the repository at this point in the history
…ger r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D125555
  • Loading branch information
saschanaz committed Oct 19, 2021
1 parent fa94c9c commit b81f2ed
Show file tree
Hide file tree
Showing 28 changed files with 823 additions and 300 deletions.
8 changes: 7 additions & 1 deletion dom/base/Navigator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,13 @@ void Navigator::Invalidate() {

mWebGpu = nullptr;

mLocks = nullptr;
if (mLocks) {
// Unloading a page does not immediately destruct the lock manager actor,
// but we want to abort the lock requests as soon as possible. Explicitly
// call Shutdown() to do that.
mLocks->Shutdown();
mLocks = nullptr;
}

mSharePromise = nullptr;
}
Expand Down
28 changes: 28 additions & 0 deletions dom/locks/IPCUtils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* -*- 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/. */

#ifndef DOM_LOCKS_IPCUTILS_H_
#define DOM_LOCKS_IPCUTILS_H_

#include "ipc/EnumSerializer.h"
#include "ipc/IPCMessageUtilsSpecializations.h"
#include "mozilla/dom/LockManagerBinding.h"

namespace IPC {
using LockMode = mozilla::dom::LockMode;
template <>
struct ParamTraits<LockMode>
: public ContiguousEnumSerializerInclusive<LockMode, LockMode::Shared,
LockMode::Exclusive> {};

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::LockInfo, mName, mMode,
mClientId);

DEFINE_IPC_SERIALIZER_WITH_FIELDS(mozilla::dom::LockManagerSnapshot, mHeld,
mPending);
} // namespace IPC

#endif // DOM_LOCKS_IPCUTILS_H_
26 changes: 15 additions & 11 deletions dom/locks/Lock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,30 @@
#include "mozilla/dom/LockBinding.h"
#include "mozilla/dom/LockManager.h"
#include "mozilla/dom/Promise.h"
#include "mozilla/dom/locks/LockRequestChild.h"

namespace mozilla::dom {

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Lock, mOwner, mLockManager,
mWaitingPromise, mReleasedPromise)
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Lock, mOwner, mWaitingPromise,
mReleasedPromise)
NS_IMPL_CYCLE_COLLECTING_ADDREF(Lock)
NS_IMPL_CYCLE_COLLECTING_RELEASE(Lock)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Lock)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

Lock::Lock(nsIGlobalObject* aGlobal, const RefPtr<LockManager>& aLockManager,
Lock::Lock(nsIGlobalObject* aGlobal,
const WeakPtr<locks::LockRequestChild>& aLockRequestChild,
const nsString& aName, LockMode aMode,
const RefPtr<Promise>& aReleasedPromise, ErrorResult& aRv)
: mOwner(aGlobal),
mLockManager(aLockManager),
mLockRequestChild(aLockRequestChild),
mName(aName),
mMode(aMode),
mWaitingPromise(Promise::Create(aGlobal, aRv)),
mReleasedPromise(aReleasedPromise) {
MOZ_ASSERT(aLockManager);
MOZ_ASSERT(mLockRequestChild);
MOZ_ASSERT(aReleasedPromise);
}

Expand All @@ -47,16 +49,18 @@ Promise& Lock::GetWaitingPromise() {
}

void Lock::ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) {
RefPtr<Lock> pin = this;
// decrements the refcount, may delete this
mLockManager->ReleaseHeldLock(this);
if (mLockRequestChild) {
locks::PLockRequestChild::Send__delete__(mLockRequestChild);
mLockRequestChild = nullptr;
}
mReleasedPromise->MaybeResolve(aValue);
}

void Lock::RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) {
RefPtr<Lock> pin = this;
// decrements the refcount, may delete this
mLockManager->ReleaseHeldLock(this);
if (mLockRequestChild) {
locks::PLockRequestChild::Send__delete__(mLockRequestChild);
mLockRequestChild = nullptr;
}
mReleasedPromise->MaybeReject(aValue);
}

Expand Down
17 changes: 7 additions & 10 deletions dom/locks/Lock.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "js/TypeDecls.h"
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/WeakPtr.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/LockManagerBinding.h"
#include "mozilla/dom/PromiseNativeHandler.h"
Expand All @@ -19,6 +20,9 @@
namespace mozilla::dom {

class LockManager;
namespace locks {
class LockRequestChild;
}

class Lock final : public PromiseNativeHandler, public nsWrapperCache {
friend class LockManager;
Expand All @@ -27,18 +31,11 @@ class Lock final : public PromiseNativeHandler, public nsWrapperCache {
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Lock)

Lock(nsIGlobalObject* aGlobal, const RefPtr<LockManager>& aLockManager,
Lock(nsIGlobalObject* aGlobal,
const WeakPtr<locks::LockRequestChild>& aLockRequestChild,
const nsString& aName, LockMode aMode,
const RefPtr<Promise>& aReleasedPromise, ErrorResult& aRv);

bool operator==(const Lock& aOther) const {
// This operator is needed to remove released locks from the queue.
// This assumes each lock has a unique promise
MOZ_ASSERT(mReleasedPromise && aOther.mReleasedPromise,
"Promises are null when locks are unreleased??");
return mReleasedPromise == aOther.mReleasedPromise;
}

protected:
~Lock() = default;

Expand All @@ -62,7 +59,7 @@ class Lock final : public PromiseNativeHandler, public nsWrapperCache {

private:
nsCOMPtr<nsIGlobalObject> mOwner;
RefPtr<LockManager> mLockManager;
WeakPtr<locks::LockRequestChild> mLockRequestChild;

nsString mName;
LockMode mMode;
Expand Down
Loading

0 comments on commit b81f2ed

Please sign in to comment.