1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
11 #include "mozilla/Mutex.h"
12 #include "mozilla/StaticMutex.h"
16 // A template to wrap a type with a mutex so that accesses to the type's
17 // data are required to take the lock before accessing it. This ensures
18 // that a mutex is explicitly associated with the data that it protects,
19 // and makes it impossible to access the data without first taking the
22 // This is based on Rust's std::sync::Mutex, which operates under the
23 // strategy of locking data, rather than code.
27 // DataMutex<uint32_t> u32DataMutex(1, "u32DataMutex");
28 // auto x = u32DataMutex.Lock();
32 // DataMutex<nsTArray<uint32_t>> arrayDataMutex("arrayDataMutex");
33 // auto a = arrayDataMutex.Lock();
35 // x.AppendElement(1u);
38 template <typename T
, typename MutexType
>
42 class MOZ_STACK_CLASS AutoLockBase
{
44 V
* operator->() const& { return &ref(); }
45 V
* operator->() const&& = delete;
47 V
& operator*() const& { return ref(); }
48 V
& operator*() const&& = delete;
50 // Like RefPtr, make this act like its underlying raw pointer type
51 // whenever it is used in a context where a raw pointer is expected.
52 operator V
*() const& { return &ref(); }
54 // Like RefPtr, don't allow implicit conversion of temporary to raw pointer.
55 operator V
*() const&& = delete;
59 return mOwner
->mValue
;
61 V
& ref() const&& = delete;
63 AutoLockBase(AutoLockBase
&& aOther
) : mOwner(aOther
.mOwner
) {
64 aOther
.mOwner
= nullptr;
69 mOwner
->mMutex
.Unlock();
75 friend class DataMutexBase
;
77 AutoLockBase(const AutoLockBase
& aOther
) = delete;
79 explicit AutoLockBase(DataMutexBase
<T
, MutexType
>* aDataMutex
)
80 : mOwner(aDataMutex
) {
82 mOwner
->mMutex
.Lock();
85 DataMutexBase
<T
, MutexType
>* mOwner
;
88 using AutoLock
= AutoLockBase
<T
>;
89 using ConstAutoLock
= AutoLockBase
<const T
>;
91 explicit DataMutexBase(const char* aName
) : mMutex(aName
) {}
93 DataMutexBase(T
&& aValue
, const char* aName
)
94 : mMutex(aName
), mValue(std::move(aValue
)) {}
96 AutoLock
Lock() { return AutoLock(this); }
97 ConstAutoLock
ConstLock() { return ConstAutoLock(this); }
99 const MutexType
& Mutex() const { return mMutex
; }
106 // Craft a version of StaticMutex that takes a const char* in its ctor.
107 // We need this so it works interchangeably with Mutex which requires a const
108 // char* aName in its ctor.
109 class StaticMutexNameless
: public StaticMutex
{
111 explicit StaticMutexNameless(const char* aName
) : StaticMutex() {}
114 // Disallow copy construction, `=`, `new`, and `delete` like BaseStaticMutex.
116 StaticMutexNameless(StaticMutexNameless
& aOther
);
118 StaticMutexNameless
& operator=(StaticMutexNameless
* aRhs
);
119 static void* operator new(size_t) noexcept(true);
120 static void operator delete(void*);
123 template <typename T
>
124 using DataMutex
= DataMutexBase
<T
, Mutex
>;
125 template <typename T
>
126 using StaticDataMutex
= DataMutexBase
<T
, StaticMutexNameless
>;
128 } // namespace mozilla
130 #endif // DataMutex_h__