diff options
author | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
---|---|---|
committer | Matt A. Tobin <mattatobin@localhost.localdomain> | 2018-02-02 04:16:08 -0500 |
commit | 5f8de423f190bbb79a62f804151bc24824fa32d8 (patch) | |
tree | 10027f336435511475e392454359edea8e25895d /js/src/threading/Mutex.h | |
parent | 49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff) | |
download | UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip |
Add m-esr52 at 52.6.0
Diffstat (limited to 'js/src/threading/Mutex.h')
-rw-r--r-- | js/src/threading/Mutex.h | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/js/src/threading/Mutex.h b/js/src/threading/Mutex.h new file mode 100644 index 000000000..1ea019dde --- /dev/null +++ b/js/src/threading/Mutex.h @@ -0,0 +1,128 @@ +/* -*- 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 threading_Mutex_h +#define threading_Mutex_h + +#include "mozilla/Assertions.h" +#include "mozilla/Attributes.h" +#include "mozilla/Move.h" +#include "mozilla/ThreadLocal.h" +#include "mozilla/Vector.h" + +#include <new> +#include <string.h> + +namespace js { + +class ConditionVariable; + +namespace detail { + +class MutexImpl +{ +public: + struct PlatformData; + + MutexImpl(); + ~MutexImpl(); + + MutexImpl(MutexImpl&& rhs) + : platformData_(rhs.platformData_) + { + MOZ_ASSERT(this != &rhs, "self move disallowed!"); + rhs.platformData_ = nullptr; + } + + MutexImpl& operator=(MutexImpl&& rhs) { + this->~MutexImpl(); + new (this) MutexImpl(mozilla::Move(rhs)); + return *this; + } + + bool operator==(const MutexImpl& rhs) { + return platformData_ == rhs.platformData_; + } + +protected: + void lock(); + void unlock(); + +private: + MutexImpl(const MutexImpl&) = delete; + void operator=(const MutexImpl&) = delete; + + friend class js::ConditionVariable; + PlatformData* platformData() { + MOZ_ASSERT(platformData_); + return platformData_; + }; + + PlatformData* platformData_; +}; + +} // namespace detail + +// A MutexId secifies the name and mutex order for a mutex. +// +// The mutex order defines the allowed order of mutex acqusition on a single +// thread. Mutexes must be acquired in strictly increasing order. Mutexes with +// the same order may not be held at the same time by that thread. +struct MutexId +{ + const char* name; + uint32_t order; +}; + +#ifndef DEBUG + +class Mutex : public detail::MutexImpl +{ +public: + static bool Init() { return true; } + static void ShutDown() {} + + explicit Mutex(const MutexId& id) {} + + using MutexImpl::lock; + using MutexImpl::unlock; +}; + +#else + +// In debug builds, js::Mutex is a wrapper over MutexImpl that checks correct +// locking order is observed. +// +// The class maintains a per-thread stack of currently-held mutexes to enable it +// to check this. +class Mutex : public detail::MutexImpl +{ +public: + static bool Init(); + static void ShutDown(); + + explicit Mutex(const MutexId& id) + : id_(id) + { + MOZ_ASSERT(id_.order != 0); + } + + void lock(); + void unlock(); + +private: + const MutexId id_; + + using MutexVector = mozilla::Vector<const Mutex*>; + static MOZ_THREAD_LOCAL(MutexVector*) HeldMutexStack; + static MutexVector& heldMutexStack(); +}; + +#endif + +} // namespace js + +#endif // threading_Mutex_h |