Bug 1580091 - BaseProfilerMaybeMutex and BaseProfilerMaybeAutoLock - r=gregtatum
authorGerald Squelart <gsquelart@mozilla.com>
Wed, 18 Sep 2019 01:19:06 +0000
changeset 493673 3ba4d78b186abe432250d4a97b8518c32693fdf6
parent 493672 d3f4115964cc3920bc29f1287ce22a5da298e8ae
child 493674 5f0d86d44a8a4f3b210327e82c93a4e0f97f4cf3
push id95644
push usergsquelart@mozilla.com
push dateWed, 18 Sep 2019 01:35:09 +0000
treeherderautoland@4001594f15dd [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgregtatum
bugs1580091
milestone71.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1580091 - BaseProfilerMaybeMutex and BaseProfilerMaybeAutoLock - r=gregtatum `BaseProfilerMaybeMutex` wraps a `BaseProfilerMutex` inside a `Maybe`. The decision to use a mutex or not is set at construction time. If there is no mutex, all operations do nothing (at the small cost of checking if the mutex is present.) `BaseProfilerMaybeAutoLock` is the recommented RAII object to lock and automatically unlock a `BaseProfilerMaybeMutex` until the end of a block scope. Differential Revision: https://phabricator.services.mozilla.com/D45304
mozglue/baseprofiler/public/BaseProfilerDetail.h
--- a/mozglue/baseprofiler/public/BaseProfilerDetail.h
+++ b/mozglue/baseprofiler/public/BaseProfilerDetail.h
@@ -4,16 +4,17 @@
  * 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/. */
 
 // Internal Base Profiler utilities.
 
 #ifndef BaseProfilerDetail_h
 #define BaseProfilerDetail_h
 
+#include "mozilla/Maybe.h"
 #include "mozilla/PlatformMutex.h"
 
 #ifdef DEBUG
 #  include "BaseProfiler.h"
 #  ifdef MOZ_BASE_PROFILER
 #    include "mozilla/Atomics.h"
 // When #defined, safety-checking code is added. By default: DEBUG builds,
 // and we need `MOZ_BASE_PROFILER` to use `profiler_current_thread_id()`.
@@ -95,13 +96,77 @@ class MOZ_RAII BaseProfilerAutoLock {
   BaseProfilerAutoLock& operator=(BaseProfilerAutoLock&&) = delete;
 
   ~BaseProfilerAutoLock() { mMutex.Unlock(); }
 
  private:
   BaseProfilerMutex& mMutex;
 };
 
+// Thin shell around mozglue PlatformMutex, for Base Profiler internal use.
+// Actual mutex may be disabled at construction time.
+// Does not preserve behavior in JS record/replay.
+class BaseProfilerMaybeMutex : private ::mozilla::detail::MutexImpl {
+ public:
+  explicit BaseProfilerMaybeMutex(bool aActivate) {
+    if (aActivate) {
+      mMaybeMutex.emplace();
+    }
+  }
+
+  BaseProfilerMaybeMutex(const BaseProfilerMaybeMutex&) = delete;
+  BaseProfilerMaybeMutex& operator=(const BaseProfilerMaybeMutex&) = delete;
+  BaseProfilerMaybeMutex(BaseProfilerMaybeMutex&&) = delete;
+  BaseProfilerMaybeMutex& operator=(BaseProfilerMaybeMutex&&) = delete;
+
+  ~BaseProfilerMaybeMutex() = default;
+
+  bool IsActivated() const { return mMaybeMutex.isSome(); }
+
+  void Lock() {
+    if (IsActivated()) {
+      mMaybeMutex->Lock();
+    }
+  }
+
+  void Unlock() {
+    if (IsActivated()) {
+      mMaybeMutex->Unlock();
+    }
+  }
+
+  void AssertCurrentThreadOwns() const {
+#ifdef MOZ_BASE_PROFILER_DEBUG
+    if (IsActivated()) {
+      mMaybeMutex->AssertCurrentThreadOwns();
+    }
+#endif  // MOZ_BASE_PROFILER_DEBUG
+  }
+
+ private:
+  Maybe<BaseProfilerMutex> mMaybeMutex;
+};
+
+// RAII class to lock a mutex.
+class MOZ_RAII BaseProfilerMaybeAutoLock {
+ public:
+  explicit BaseProfilerMaybeAutoLock(BaseProfilerMaybeMutex& aMaybeMutex)
+      : mMaybeMutex(aMaybeMutex) {
+    mMaybeMutex.Lock();
+  }
+
+  BaseProfilerMaybeAutoLock(const BaseProfilerMaybeAutoLock&) = delete;
+  BaseProfilerMaybeAutoLock& operator=(const BaseProfilerMaybeAutoLock&) =
+      delete;
+  BaseProfilerMaybeAutoLock(BaseProfilerMaybeAutoLock&&) = delete;
+  BaseProfilerMaybeAutoLock& operator=(BaseProfilerMaybeAutoLock&&) = delete;
+
+  ~BaseProfilerMaybeAutoLock() { mMaybeMutex.Unlock(); }
+
+ private:
+  BaseProfilerMaybeMutex& mMaybeMutex;
+};
+
 }  // namespace detail
 }  // namespace baseprofiler
 }  // namespace mozilla
 
 #endif  // BaseProfilerDetail_h