2017-12-20 06:15:09 +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/. */
|
|
|
|
|
|
|
|
#ifndef DataMutex_h__
|
|
|
|
#define DataMutex_h__
|
|
|
|
|
2019-01-18 04:32:40 +03:00
|
|
|
#include <utility>
|
2017-12-20 06:15:09 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2019-09-12 22:58:01 +03:00
|
|
|
#include "mozilla/StaticMutex.h"
|
2017-12-20 06:15:09 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// A template to wrap a type with a mutex so that accesses to the type's
|
|
|
|
// data are required to take the lock before accessing it. This ensures
|
|
|
|
// that a mutex is explicitly associated with the data that it protects,
|
|
|
|
// and makes it impossible to access the data without first taking the
|
|
|
|
// associated mutex.
|
|
|
|
//
|
|
|
|
// This is based on Rust's std::sync::Mutex, which operates under the
|
|
|
|
// strategy of locking data, rather than code.
|
|
|
|
//
|
|
|
|
// Examples:
|
|
|
|
//
|
|
|
|
// DataMutex<uint32_t> u32DataMutex(1, "u32DataMutex");
|
|
|
|
// auto x = u32DataMutex.Lock();
|
|
|
|
// *x = 4;
|
|
|
|
// assert(*x, 4u);
|
|
|
|
//
|
|
|
|
// DataMutex<nsTArray<uint32_t>> arrayDataMutex("arrayDataMutex");
|
|
|
|
// auto a = arrayDataMutex.Lock();
|
|
|
|
// auto& x = a.ref();
|
|
|
|
// x.AppendElement(1u);
|
|
|
|
// assert(x[0], 1u);
|
|
|
|
//
|
2019-09-12 22:58:01 +03:00
|
|
|
template <typename T, typename MutexType>
|
|
|
|
class DataMutexBase {
|
2020-11-13 13:04:22 +03:00
|
|
|
public:
|
2017-12-20 06:15:09 +03:00
|
|
|
class MOZ_STACK_CLASS AutoLock {
|
|
|
|
public:
|
2019-01-18 19:48:22 +03:00
|
|
|
T* operator->() const& { return &ref(); }
|
|
|
|
T* operator->() const&& = delete;
|
2017-12-20 06:15:09 +03:00
|
|
|
|
2019-01-18 19:48:22 +03:00
|
|
|
T& operator*() const& { return ref(); }
|
|
|
|
T& operator*() const&& = delete;
|
2017-12-20 06:15:09 +03:00
|
|
|
|
|
|
|
// Like RefPtr, make this act like its underlying raw pointer type
|
|
|
|
// whenever it is used in a context where a raw pointer is expected.
|
|
|
|
operator T*() const& { return &ref(); }
|
|
|
|
|
|
|
|
// Like RefPtr, don't allow implicit conversion of temporary to raw pointer.
|
|
|
|
operator T*() const&& = delete;
|
|
|
|
|
2019-01-18 19:48:22 +03:00
|
|
|
T& ref() const& {
|
2017-12-20 06:15:09 +03:00
|
|
|
MOZ_ASSERT(mOwner);
|
|
|
|
return mOwner->mValue;
|
|
|
|
}
|
2019-01-18 19:48:22 +03:00
|
|
|
T& ref() const&& = delete;
|
2017-12-20 06:15:09 +03:00
|
|
|
|
|
|
|
AutoLock(AutoLock&& aOther) : mOwner(aOther.mOwner) {
|
|
|
|
aOther.mOwner = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~AutoLock() {
|
|
|
|
if (mOwner) {
|
|
|
|
mOwner->mMutex.Unlock();
|
|
|
|
mOwner = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-09-12 22:58:01 +03:00
|
|
|
friend class DataMutexBase;
|
2017-12-20 06:15:09 +03:00
|
|
|
|
|
|
|
AutoLock(const AutoLock& aOther) = delete;
|
|
|
|
|
2019-09-12 22:58:01 +03:00
|
|
|
explicit AutoLock(DataMutexBase<T, MutexType>* aDataMutex)
|
|
|
|
: mOwner(aDataMutex) {
|
2017-12-20 06:15:09 +03:00
|
|
|
MOZ_ASSERT(!!mOwner);
|
|
|
|
mOwner->mMutex.Lock();
|
|
|
|
}
|
|
|
|
|
2019-09-12 22:58:01 +03:00
|
|
|
DataMutexBase<T, MutexType>* mOwner;
|
2017-12-20 06:15:09 +03:00
|
|
|
};
|
|
|
|
|
2019-09-12 22:58:01 +03:00
|
|
|
explicit DataMutexBase(const char* aName) : mMutex(aName) {}
|
2017-12-20 06:15:09 +03:00
|
|
|
|
2019-09-12 22:58:01 +03:00
|
|
|
DataMutexBase(T&& aValue, const char* aName)
|
2019-01-18 04:32:40 +03:00
|
|
|
: mMutex(aName), mValue(std::move(aValue)) {}
|
2017-12-20 06:15:09 +03:00
|
|
|
|
|
|
|
AutoLock Lock() { return AutoLock(this); }
|
|
|
|
|
2020-11-19 14:47:08 +03:00
|
|
|
const MutexType& Mutex() const { return mMutex; }
|
|
|
|
|
2017-12-20 06:15:09 +03:00
|
|
|
private:
|
2019-09-12 22:58:01 +03:00
|
|
|
MutexType mMutex;
|
2017-12-20 06:15:09 +03:00
|
|
|
T mValue;
|
|
|
|
};
|
|
|
|
|
2019-09-12 22:58:01 +03:00
|
|
|
// Craft a version of StaticMutex that takes a const char* in its ctor.
|
|
|
|
// We need this so it works interchangeably with Mutex which requires a const
|
|
|
|
// char* aName in its ctor.
|
2020-02-27 20:39:15 +03:00
|
|
|
class StaticMutexNameless : public StaticMutex {
|
2019-09-12 22:58:01 +03:00
|
|
|
public:
|
2020-02-27 20:39:15 +03:00
|
|
|
explicit StaticMutexNameless(const char* aName) : StaticMutex() {}
|
2019-10-06 21:29:55 +03:00
|
|
|
|
2019-09-12 22:58:01 +03:00
|
|
|
private:
|
|
|
|
// Disallow copy construction, `=`, `new`, and `delete` like BaseStaticMutex.
|
|
|
|
#ifdef DEBUG
|
|
|
|
StaticMutexNameless(StaticMutexNameless& aOther);
|
|
|
|
#endif // DEBUG
|
|
|
|
StaticMutexNameless& operator=(StaticMutexNameless* aRhs);
|
|
|
|
static void* operator new(size_t) noexcept(true);
|
|
|
|
static void operator delete(void*);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
using DataMutex = DataMutexBase<T, Mutex>;
|
|
|
|
template <typename T>
|
|
|
|
using StaticDataMutex = DataMutexBase<T, StaticMutexNameless>;
|
|
|
|
|
2017-12-20 06:15:09 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // DataMutex_h__
|