2017-05-23 04:26:26 +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/. */
|
|
|
|
|
|
|
|
// An interface for read-write locks.
|
|
|
|
|
|
|
|
#ifndef mozilla_RWLock_h
|
|
|
|
#define mozilla_RWLock_h
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/Atomics.h"
|
2022-10-14 17:20:35 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-05-23 04:26:26 +03:00
|
|
|
#include "mozilla/BlockingResourceBase.h"
|
2022-02-28 02:32:55 +03:00
|
|
|
#include "mozilla/PlatformRWLock.h"
|
2022-03-17 02:01:41 +03:00
|
|
|
#include "mozilla/ThreadSafety.h"
|
2017-05-23 04:26:26 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// A RWLock is similar to a Mutex, but whereas a Mutex permits only a single
|
|
|
|
// reader thread or a single writer thread to access a piece of data, a
|
|
|
|
// RWLock distinguishes between readers and writers: you may have multiple
|
|
|
|
// reader threads concurrently accessing a piece of data or a single writer
|
|
|
|
// thread. This difference should guide your usage of RWLock: if you are not
|
|
|
|
// reading the data from multiple threads simultaneously or you are writing
|
|
|
|
// to the data roughly as often as read from it, then Mutex will suit your
|
|
|
|
// purposes just fine.
|
|
|
|
//
|
|
|
|
// You should be using the AutoReadLock and AutoWriteLock classes, below,
|
|
|
|
// for RAII read locking and write locking, respectively. If you really must
|
|
|
|
// take a read lock manually, call the ReadLock method; to relinquish that
|
|
|
|
// read lock, call the ReadUnlock method. Similarly, WriteLock and WriteUnlock
|
|
|
|
// perform the same operations, but for write locks.
|
|
|
|
//
|
|
|
|
// It is unspecified what happens when a given thread attempts to acquire the
|
|
|
|
// same lock in multiple ways; some underlying implementations of RWLock do
|
|
|
|
// support acquiring a read lock multiple times on a given thread, but you
|
|
|
|
// should not rely on this behavior.
|
|
|
|
//
|
|
|
|
// It is unspecified whether RWLock gives priority to waiting readers or
|
|
|
|
// a waiting writer when unlocking.
|
2022-10-28 22:55:30 +03:00
|
|
|
class MOZ_CAPABILITY("rwlock") RWLock : public detail::RWLockImpl,
|
|
|
|
public BlockingResourceBase {
|
2017-05-23 04:26:26 +03:00
|
|
|
public:
|
|
|
|
explicit RWLock(const char* aName);
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2017-05-26 01:04:13 +03:00
|
|
|
bool LockedForWritingByCurrentThread();
|
2022-08-03 19:39:41 +03:00
|
|
|
[[nodiscard]] bool TryReadLock() MOZ_SHARED_TRYLOCK_FUNCTION(true);
|
|
|
|
void ReadLock() MOZ_ACQUIRE_SHARED();
|
|
|
|
void ReadUnlock() MOZ_RELEASE_SHARED();
|
|
|
|
[[nodiscard]] bool TryWriteLock() MOZ_TRY_ACQUIRE(true);
|
|
|
|
void WriteLock() MOZ_CAPABILITY_ACQUIRE();
|
|
|
|
void WriteUnlock() MOZ_EXCLUSIVE_RELEASE();
|
2017-05-23 04:26:26 +03:00
|
|
|
#else
|
2022-08-03 19:39:41 +03:00
|
|
|
[[nodiscard]] bool TryReadLock() MOZ_SHARED_TRYLOCK_FUNCTION(true) {
|
2022-03-17 02:01:41 +03:00
|
|
|
return detail::RWLockImpl::tryReadLock();
|
|
|
|
}
|
2022-08-03 19:39:41 +03:00
|
|
|
void ReadLock() MOZ_ACQUIRE_SHARED() { detail::RWLockImpl::readLock(); }
|
|
|
|
void ReadUnlock() MOZ_RELEASE_SHARED() { detail::RWLockImpl::readUnlock(); }
|
|
|
|
[[nodiscard]] bool TryWriteLock() MOZ_TRY_ACQUIRE(true) {
|
2022-02-28 02:32:56 +03:00
|
|
|
return detail::RWLockImpl::tryWriteLock();
|
|
|
|
}
|
2022-08-03 19:39:41 +03:00
|
|
|
void WriteLock() MOZ_CAPABILITY_ACQUIRE() { detail::RWLockImpl::writeLock(); }
|
|
|
|
void WriteUnlock() MOZ_EXCLUSIVE_RELEASE() {
|
|
|
|
detail::RWLockImpl::writeUnlock();
|
|
|
|
}
|
2017-05-23 04:26:26 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
|
|
|
RWLock() = delete;
|
|
|
|
RWLock(const RWLock&) = delete;
|
|
|
|
RWLock& operator=(const RWLock&) = delete;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// We record the owning thread for write locks only.
|
|
|
|
PRThread* mOwningThread;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2022-03-17 02:01:41 +03:00
|
|
|
// We only use this once; not sure we can add thread safety attributions here
|
2021-09-08 13:40:07 +03:00
|
|
|
template <typename T>
|
|
|
|
class MOZ_RAII BaseAutoTryReadLock {
|
|
|
|
public:
|
|
|
|
explicit BaseAutoTryReadLock(T& aLock)
|
|
|
|
: mLock(aLock.TryReadLock() ? &aLock : nullptr) {}
|
|
|
|
|
|
|
|
~BaseAutoTryReadLock() {
|
|
|
|
if (mLock) {
|
|
|
|
mLock->ReadUnlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const { return mLock; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
BaseAutoTryReadLock() = delete;
|
|
|
|
BaseAutoTryReadLock(const BaseAutoTryReadLock&) = delete;
|
|
|
|
BaseAutoTryReadLock& operator=(const BaseAutoTryReadLock&) = delete;
|
|
|
|
|
|
|
|
T* mLock;
|
|
|
|
};
|
|
|
|
|
2020-02-15 00:44:05 +03:00
|
|
|
template <typename T>
|
2022-08-03 19:39:41 +03:00
|
|
|
class MOZ_SCOPED_CAPABILITY MOZ_RAII BaseAutoReadLock {
|
2017-05-23 04:26:26 +03:00
|
|
|
public:
|
2022-08-03 19:39:41 +03:00
|
|
|
explicit BaseAutoReadLock(T& aLock) MOZ_ACQUIRE_SHARED(aLock)
|
|
|
|
: mLock(&aLock) {
|
2017-05-23 04:26:26 +03:00
|
|
|
MOZ_ASSERT(mLock, "null lock");
|
|
|
|
mLock->ReadLock();
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:39:41 +03:00
|
|
|
// Not MOZ_RELEASE_SHARED(), which would make sense - apparently this trips
|
2022-03-17 02:01:41 +03:00
|
|
|
// over a bug in clang's static analyzer and it says it expected an
|
|
|
|
// exclusive unlock.
|
2022-08-03 19:39:41 +03:00
|
|
|
~BaseAutoReadLock() MOZ_RELEASE_GENERIC() { mLock->ReadUnlock(); }
|
2017-05-23 04:26:26 +03:00
|
|
|
|
|
|
|
private:
|
2020-02-15 00:44:05 +03:00
|
|
|
BaseAutoReadLock() = delete;
|
|
|
|
BaseAutoReadLock(const BaseAutoReadLock&) = delete;
|
|
|
|
BaseAutoReadLock& operator=(const BaseAutoReadLock&) = delete;
|
2017-05-23 04:26:26 +03:00
|
|
|
|
2020-02-15 00:44:05 +03:00
|
|
|
T* mLock;
|
2017-05-23 04:26:26 +03:00
|
|
|
};
|
|
|
|
|
2022-03-17 02:01:41 +03:00
|
|
|
// XXX Mutex attributions?
|
2021-09-08 13:40:07 +03:00
|
|
|
template <typename T>
|
|
|
|
class MOZ_RAII BaseAutoTryWriteLock {
|
|
|
|
public:
|
|
|
|
explicit BaseAutoTryWriteLock(T& aLock)
|
|
|
|
: mLock(aLock.TryWriteLock() ? &aLock : nullptr) {}
|
|
|
|
|
|
|
|
~BaseAutoTryWriteLock() {
|
|
|
|
if (mLock) {
|
|
|
|
mLock->WriteUnlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
explicit operator bool() const { return mLock; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
BaseAutoTryWriteLock() = delete;
|
|
|
|
BaseAutoTryWriteLock(const BaseAutoTryWriteLock&) = delete;
|
|
|
|
BaseAutoTryWriteLock& operator=(const BaseAutoTryWriteLock&) = delete;
|
|
|
|
|
|
|
|
T* mLock;
|
|
|
|
};
|
|
|
|
|
2020-02-15 00:44:05 +03:00
|
|
|
template <typename T>
|
2022-08-03 19:39:41 +03:00
|
|
|
class MOZ_SCOPED_CAPABILITY MOZ_RAII BaseAutoWriteLock final {
|
2017-05-23 04:26:26 +03:00
|
|
|
public:
|
2022-08-03 19:39:41 +03:00
|
|
|
explicit BaseAutoWriteLock(T& aLock) MOZ_CAPABILITY_ACQUIRE(aLock)
|
2022-06-07 15:32:55 +03:00
|
|
|
: mLock(&aLock) {
|
2017-05-23 04:26:26 +03:00
|
|
|
MOZ_ASSERT(mLock, "null lock");
|
|
|
|
mLock->WriteLock();
|
|
|
|
}
|
|
|
|
|
2022-08-03 19:39:41 +03:00
|
|
|
~BaseAutoWriteLock() MOZ_CAPABILITY_RELEASE() { mLock->WriteUnlock(); }
|
2017-05-23 04:26:26 +03:00
|
|
|
|
|
|
|
private:
|
2020-02-15 00:44:05 +03:00
|
|
|
BaseAutoWriteLock() = delete;
|
|
|
|
BaseAutoWriteLock(const BaseAutoWriteLock&) = delete;
|
|
|
|
BaseAutoWriteLock& operator=(const BaseAutoWriteLock&) = delete;
|
2017-05-23 04:26:26 +03:00
|
|
|
|
2020-02-15 00:44:05 +03:00
|
|
|
T* mLock;
|
2017-05-23 04:26:26 +03:00
|
|
|
};
|
|
|
|
|
2021-09-08 13:40:07 +03:00
|
|
|
// Read try-lock and unlock a RWLock with RAII semantics. Much preferred to
|
|
|
|
// bare calls to TryReadLock() and ReadUnlock().
|
|
|
|
typedef BaseAutoTryReadLock<RWLock> AutoTryReadLock;
|
|
|
|
|
2020-02-15 00:44:05 +03:00
|
|
|
// Read lock and unlock a RWLock with RAII semantics. Much preferred to bare
|
|
|
|
// calls to ReadLock() and ReadUnlock().
|
|
|
|
typedef BaseAutoReadLock<RWLock> AutoReadLock;
|
|
|
|
|
2021-09-08 13:40:07 +03:00
|
|
|
// Write try-lock and unlock a RWLock with RAII semantics. Much preferred to
|
|
|
|
// bare calls to TryWriteLock() and WriteUnlock().
|
|
|
|
typedef BaseAutoTryWriteLock<RWLock> AutoTryWriteLock;
|
|
|
|
|
2020-02-15 00:44:05 +03:00
|
|
|
// Write lock and unlock a RWLock with RAII semantics. Much preferred to bare
|
|
|
|
// calls to WriteLock() and WriteUnlock().
|
|
|
|
typedef BaseAutoWriteLock<RWLock> AutoWriteLock;
|
|
|
|
|
2022-10-28 22:55:30 +03:00
|
|
|
class MOZ_ONLY_USED_TO_AVOID_STATIC_CONSTRUCTORS MOZ_CAPABILITY("rwlock")
|
|
|
|
StaticRWLock {
|
2020-02-15 00:44:05 +03:00
|
|
|
public:
|
|
|
|
// In debug builds, check that mLock is initialized for us as we expect by
|
|
|
|
// the compiler. In non-debug builds, don't declare a constructor so that
|
|
|
|
// the compiler can see that the constructor is trivial.
|
|
|
|
#ifdef DEBUG
|
|
|
|
StaticRWLock() { MOZ_ASSERT(!mLock); }
|
|
|
|
#endif
|
|
|
|
|
2022-08-03 19:39:41 +03:00
|
|
|
[[nodiscard]] bool TryReadLock() MOZ_SHARED_TRYLOCK_FUNCTION(true) {
|
2022-03-17 02:01:41 +03:00
|
|
|
return Lock()->TryReadLock();
|
|
|
|
}
|
2022-08-03 19:39:41 +03:00
|
|
|
void ReadLock() MOZ_ACQUIRE_SHARED() { Lock()->ReadLock(); }
|
|
|
|
void ReadUnlock() MOZ_RELEASE_SHARED() { Lock()->ReadUnlock(); }
|
|
|
|
[[nodiscard]] bool TryWriteLock() MOZ_TRY_ACQUIRE(true) {
|
2022-03-17 02:01:41 +03:00
|
|
|
return Lock()->TryWriteLock();
|
|
|
|
}
|
2022-08-03 19:39:41 +03:00
|
|
|
void WriteLock() MOZ_CAPABILITY_ACQUIRE() { Lock()->WriteLock(); }
|
|
|
|
void WriteUnlock() MOZ_EXCLUSIVE_RELEASE() { Lock()->WriteUnlock(); }
|
2020-02-15 00:44:05 +03:00
|
|
|
|
|
|
|
private:
|
2022-08-03 19:39:41 +03:00
|
|
|
[[nodiscard]] RWLock* Lock() MOZ_RETURN_CAPABILITY(*mLock) {
|
2020-02-15 00:44:05 +03:00
|
|
|
if (mLock) {
|
|
|
|
return mLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
RWLock* lock = new RWLock("StaticRWLock");
|
|
|
|
if (!mLock.compareExchange(nullptr, lock)) {
|
|
|
|
delete lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mLock;
|
|
|
|
}
|
|
|
|
|
|
|
|
Atomic<RWLock*> mLock;
|
|
|
|
|
|
|
|
// Disallow copy constructor, but only in debug mode. We only define
|
|
|
|
// a default constructor in debug mode (see above); if we declared
|
|
|
|
// this constructor always, the compiler wouldn't generate a trivial
|
|
|
|
// default constructor for us in non-debug mode.
|
|
|
|
#ifdef DEBUG
|
|
|
|
StaticRWLock(const StaticRWLock& aOther);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Disallow these operators.
|
2022-02-28 02:32:56 +03:00
|
|
|
StaticRWLock& operator=(StaticRWLock* aRhs) = delete;
|
|
|
|
static void* operator new(size_t) noexcept(true) = delete;
|
|
|
|
static void operator delete(void*) = delete;
|
2020-02-15 00:44:05 +03:00
|
|
|
};
|
|
|
|
|
2021-09-08 13:40:07 +03:00
|
|
|
typedef BaseAutoTryReadLock<StaticRWLock> StaticAutoTryReadLock;
|
2020-02-15 00:44:05 +03:00
|
|
|
typedef BaseAutoReadLock<StaticRWLock> StaticAutoReadLock;
|
2021-09-08 13:40:07 +03:00
|
|
|
typedef BaseAutoTryWriteLock<StaticRWLock> StaticAutoTryWriteLock;
|
2020-02-15 00:44:05 +03:00
|
|
|
typedef BaseAutoWriteLock<StaticRWLock> StaticAutoWriteLock;
|
|
|
|
|
2017-05-23 04:26:26 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_RWLock_h
|