Bug 1062533 - part 5 - add {Mutex,Monitor}::TryLock methods; r=mccr8
authorNathan Froyd <froydnj@mozilla.com>
Wed, 09 Jan 2019 11:09:24 -0400
changeset 510192 5feee7eb8703303e75770050e358739b8fd63e29
parent 510191 c275c8909c57f4e008756e24de1bc5b3f1333a4f
child 510193 785c68522094596c62e08d0f465633ef2b8f3892
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmccr8
bugs1062533
milestone66.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 1062533 - part 5 - add {Mutex,Monitor}::TryLock methods; r=mccr8 This plumbs the code from PlatformMutex up to the xpcom level.
xpcom/threads/BlockingResourceBase.cpp
xpcom/threads/Monitor.h
xpcom/threads/Mutex.h
--- a/xpcom/threads/BlockingResourceBase.cpp
+++ b/xpcom/threads/BlockingResourceBase.cpp
@@ -344,16 +344,26 @@ void BlockingResourceBase::Release() {
 // Debug implementation of (OffTheBooks)Mutex
 void OffTheBooksMutex::Lock() {
   CheckAcquire();
   this->lock();
   mOwningThread = PR_GetCurrentThread();
   Acquire();
 }
 
+bool OffTheBooksMutex::TryLock() {
+  CheckAcquire();
+  bool locked = this->tryLock();
+  if (locked) {
+    mOwningThread = PR_GetCurrentThread();
+    Acquire();
+  }
+  return locked;
+}
+
 void OffTheBooksMutex::Unlock() {
   Release();
   mOwningThread = nullptr;
   this->unlock();
 }
 
 void OffTheBooksMutex::AssertCurrentThreadOwns() const {
   MOZ_ASSERT(IsAcquired() && mOwningThread == PR_GetCurrentThread());
--- a/xpcom/threads/Monitor.h
+++ b/xpcom/threads/Monitor.h
@@ -25,16 +25,17 @@ class Monitor {
  public:
   explicit Monitor(const char* aName, recordreplay::Behavior aRecorded =
                                           recordreplay::Behavior::Preserve)
       : mMutex(aName, aRecorded), mCondVar(mMutex, "[Monitor.mCondVar]") {}
 
   ~Monitor() {}
 
   void Lock() { mMutex.Lock(); }
+  bool TryLock() { return mMutex.TryLock(); }
   void Unlock() { mMutex.Unlock(); }
 
   void Wait() { mCondVar.Wait(); }
   CVStatus Wait(TimeDuration aDuration) { return mCondVar.Wait(aDuration); }
 
   nsresult Notify() { return mCondVar.Notify(); }
   nsresult NotifyAll() { return mCondVar.NotifyAll(); }
 
--- a/xpcom/threads/Mutex.h
+++ b/xpcom/threads/Mutex.h
@@ -61,16 +61,21 @@ class OffTheBooksMutex : public detail::
 
 #ifndef DEBUG
   /**
    * Lock this mutex.
    **/
   void Lock() { this->lock(); }
 
   /**
+   * Try to lock this mutex, returning true if we were successful.
+   **/
+  bool TryLock() { return this->tryLock(); }
+
+  /**
    * Unlock this mutex.
    **/
   void Unlock() { this->unlock(); }
 
   /**
    * Assert that the current thread owns this mutex in debug builds.
    *
    * Does nothing in non-debug builds.
@@ -84,16 +89,17 @@ class OffTheBooksMutex : public detail::
    * non-debug builds due to difficulties in dealing with memory ordering.
    *
    * It is therefore mostly useful as documentation.
    **/
   void AssertNotCurrentThreadOwns() const {}
 
 #else
   void Lock();
+  bool TryLock();
   void Unlock();
 
   void AssertCurrentThreadOwns() const;
 
   void AssertNotCurrentThreadOwns() const {
     // FIXME bug 476536
   }