Bug 1618165 - Provide BaseAutoLock and BaseAutoUnlock deduction guides for Mutex references. r=froydnj

Differential Revision: https://phabricator.services.mozilla.com/D64359

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Simon Giesecke 2020-02-28 07:59:30 +00:00
parent 5f8fe948be
commit 69b996524d
6 changed files with 30 additions and 19 deletions

View file

@ -107,8 +107,8 @@ class CrossProcessMutex {
#endif
};
typedef BaseAutoLock<CrossProcessMutex&> CrossProcessMutexAutoLock;
typedef BaseAutoUnlock<CrossProcessMutex&> CrossProcessMutexAutoUnlock;
typedef detail::BaseAutoLock<CrossProcessMutex&> CrossProcessMutexAutoLock;
typedef detail::BaseAutoUnlock<CrossProcessMutex&> CrossProcessMutexAutoUnlock;
} // namespace mozilla

View file

@ -74,8 +74,8 @@ void SharedRef::Clear() {
} // namespace detail
typedef BaseAutoLock<detail::SharedRef&> SharedRefAutoLock;
typedef BaseAutoUnlock<detail::SharedRef&> SharedRefAutoUnlock;
typedef mozilla::detail::BaseAutoLock<detail::SharedRef&> SharedRefAutoLock;
typedef mozilla::detail::BaseAutoUnlock<detail::SharedRef&> SharedRefAutoUnlock;
WeakReferenceSupport::WeakReferenceSupport(Flags aFlags)
: mRefCnt(0), mFlags(aFlags) {

View file

@ -62,7 +62,8 @@ StaticMutex MacCrashReporterLock::sInnerMutex;
bool MacCrashReporterLock::sIsLocked;
// Use MacCrashReporterLock for locking
typedef mozilla::BaseAutoLock<MacCrashReporterLock&> CrashReporterAutoLock;
typedef mozilla::detail::BaseAutoLock<MacCrashReporterLock&>
CrashReporterAutoLock;
typedef MacCrashReporterLock CrashReporterLockType;
#else /* !XP_MACOSX */
// Use StaticMutex for locking

View file

@ -88,8 +88,8 @@ class MOZ_STACK_CLASS AnyStaticMutex {
StaticMutex* mStaticMutex;
};
typedef BaseAutoLock<AnyStaticMutex> StaticMutexAutoLock;
typedef BaseAutoUnlock<AnyStaticMutex> StaticMutexAutoUnlock;
typedef detail::BaseAutoLock<AnyStaticMutex> StaticMutexAutoLock;
typedef detail::BaseAutoUnlock<AnyStaticMutex> StaticMutexAutoUnlock;
} // namespace mozilla

View file

@ -111,8 +111,8 @@ class SafeMutex {
mozilla::Atomic<PRThread*, mozilla::Relaxed> mOwnerThread;
};
typedef mozilla::BaseAutoLock<SafeMutex&> SafeMutexAutoLock;
typedef mozilla::BaseAutoUnlock<SafeMutex&> SafeMutexAutoUnlock;
typedef mozilla::detail::BaseAutoLock<SafeMutex&> SafeMutexAutoLock;
typedef mozilla::detail::BaseAutoUnlock<SafeMutex&> SafeMutexAutoUnlock;
class nsComponentManagerImpl final : public nsIComponentManager,
public nsIServiceManager,

View file

@ -133,6 +133,7 @@ class Mutex : public OffTheBooksMutex {
Mutex& operator=(const Mutex&);
};
namespace detail {
template <typename T>
class MOZ_RAII BaseAutoUnlock;
@ -165,7 +166,7 @@ class MOZ_RAII BaseAutoLock {
// Assert that aLock is the mutex passed to the constructor and that the
// current thread owns the mutex. In coding patterns such as:
//
// void LockedMethod(const MutexAutoLock& aProofOfLock)
// void LockedMethod(const BaseAutoLock<T>& aProofOfLock)
// {
// aProofOfLock.AssertOwns(mMutex);
// ...
@ -174,9 +175,9 @@ class MOZ_RAII BaseAutoLock {
// Without this assertion, it could be that mMutex is not actually
// locked. It's possible to have code like:
//
// MutexAutoLock lock(someMutex);
// BaseAutoLock lock(someMutex);
// ...
// MutexAutoUnlock unlock(someMutex);
// BaseAutoUnlock unlock(someMutex);
// ...
// LockedMethod(lock);
//
@ -187,8 +188,8 @@ class MOZ_RAII BaseAutoLock {
// should use this method in preference to using AssertCurrentThreadOwns on
// the mutex you expected to be held, since this method provides stronger
// guarantees.
void AssertOwns(const T& aLock) const {
MOZ_ASSERT(&aLock == &mLock);
void AssertOwns(const T& aMutex) const {
MOZ_ASSERT(&aMutex == &aMutex);
mLock.AssertCurrentThreadOwns();
}
@ -204,11 +205,16 @@ class MOZ_RAII BaseAutoLock {
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
typedef BaseAutoLock<Mutex&> MutexAutoLock;
typedef BaseAutoLock<OffTheBooksMutex&> OffTheBooksMutexAutoLock;
template <typename MutexType>
BaseAutoLock(MutexType&)->BaseAutoLock<MutexType&>;
} // namespace detail
typedef detail::BaseAutoLock<Mutex&> MutexAutoLock;
typedef detail::BaseAutoLock<OffTheBooksMutex&> OffTheBooksMutexAutoLock;
namespace detail {
/**
* MutexAutoUnlock
* BaseAutoUnlock
* Releases the Mutex when it enters scope, and re-acquires it when it leaves
* scope.
*
@ -243,8 +249,12 @@ class MOZ_RAII BaseAutoUnlock {
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
};
typedef BaseAutoUnlock<Mutex&> MutexAutoUnlock;
typedef BaseAutoUnlock<OffTheBooksMutex&> OffTheBooksMutexAutoUnlock;
template <typename MutexType>
BaseAutoUnlock(MutexType&)->BaseAutoUnlock<MutexType&>;
} // namespace detail
typedef detail::BaseAutoUnlock<Mutex&> MutexAutoUnlock;
typedef detail::BaseAutoUnlock<OffTheBooksMutex&> OffTheBooksMutexAutoUnlock;
} // namespace mozilla