Bug 956899 - Add comments to ConditionVariable and handle some edge cases gracefully; r=froydnj
authorTerrence Cole <terrence@mozilla.com>
Sat, 28 May 2016 16:10:15 +0200
changeset 340448 cbce330b4dd328a0d7ccc87a1f0d9a1f8ee07a01
parent 340447 10a2a0ed4d329e7509883acb17d8817319e28017
child 340449 df7f3f8416c5b24f1ea6bfad1015211d6bace955
push id1183
push userraliiev@mozilla.com
push dateMon, 05 Sep 2016 20:01:49 +0000
treeherdermozilla-release@3148731bed45 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs956899
milestone49.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 956899 - Add comments to ConditionVariable and handle some edge cases gracefully; r=froydnj
js/src/threading/ConditionVariable.h
js/src/threading/posix/ConditionVariable.cpp
js/src/threading/windows/ConditionVariable.cpp
--- a/js/src/threading/ConditionVariable.h
+++ b/js/src/threading/ConditionVariable.h
@@ -32,45 +32,71 @@ enum class CVStatus {
 class ConditionVariable
 {
 public:
   struct PlatformData;
 
   ConditionVariable();
   ~ConditionVariable();
 
+  // Wake one thread that is waiting on this condition.
   void notify_one();
+
+  // Wake all threads that are waiting on this condition.
   void notify_all();
 
+  // Block the current thread of execution until this condition variable is
+  // woken from another thread via notify_one or notify_all.
   void wait(UniqueLock<Mutex>& lock);
 
+  // As with |wait|, block the current thread of execution until woken from
+  // another thread. This method will resume waiting once woken until the given
+  // Predicate |pred| evaluates to true.
   template <typename Predicate>
   void wait(UniqueLock<Mutex>& lock, Predicate pred) {
     while (!pred()) {
       wait(lock);
     }
   }
 
+  // Block the current thread of execution until woken from another thread, or
+  // the given absolute time is reached. The given absolute time is evaluated
+  // when this method is called, so will wake up after (abs_time - now),
+  // independent of system clock changes. While insulated from clock changes,
+  // this API is succeptible to the issues discussed above wait_for.
   CVStatus wait_until(UniqueLock<Mutex>& lock,
                       const mozilla::TimeStamp& abs_time);
 
+  // As with |wait_until|, block the current thread of execution until woken
+  // from another thread, or the given absolute time is reached. This method
+  // will resume waiting once woken until the given Predicate |pred| evaluates
+  // to true.
   template <typename Predicate>
   bool wait_until(UniqueLock<Mutex>& lock, const mozilla::TimeStamp& abs_time,
                   Predicate pred) {
     while (!pred()) {
       if (wait_until(lock, abs_time) == CVStatus::Timeout) {
         return pred();
       }
     }
     return true;
   }
 
+  // Block the current thread of execution until woken from another thread, or
+  // the given time duration has elapsed. Given that the system may be
+  // interrupted between the callee and the actual wait beginning, this call
+  // has a minimum granularity of the system's scheduling interval, and may
+  // encounter substantially longer delays, depending on system load.
   CVStatus wait_for(UniqueLock<Mutex>& lock,
                     const mozilla::TimeDuration& rel_time);
 
+  // As with |wait_for|, block the current thread of execution until woken from
+  // another thread or the given time duration has elapsed. This method will
+  // resume waiting once woken until the given Predicate |pred| evaluates to
+  // true.
   template <typename Predicate>
   bool wait_for(UniqueLock<Mutex>& lock, const mozilla::TimeDuration& rel_time,
                 Predicate pred) {
     return wait_until(lock, mozilla::TimeStamp::Now() + rel_time,
                       mozilla::Move(pred));
   }
 
 
--- a/js/src/threading/posix/ConditionVariable.cpp
+++ b/js/src/threading/posix/ConditionVariable.cpp
@@ -124,16 +124,21 @@ js::ConditionVariable::wait_until(Unique
 {
   return wait_for(lock, abs_time - TimeStamp::Now());
 }
 
 js::CVStatus
 js::ConditionVariable::wait_for(UniqueLock<Mutex>& lock,
                                 const TimeDuration& a_rel_time)
 {
+  if (a_rel_time == TimeDuration::Forever()) {
+    wait(lock);
+    return CVStatus::NoTimeout;
+  }
+
   pthread_cond_t* ptCond = &platformData()->ptCond;
   pthread_mutex_t* ptMutex = &lock.lock.platformData()->ptMutex;
   int r;
 
   // Clamp to 0, as time_t is unsigned.
   TimeDuration rel_time = a_rel_time < TimeDuration::FromSeconds(0)
                           ? TimeDuration::FromSeconds(0)
                           : a_rel_time;
--- a/js/src/threading/windows/ConditionVariable.cpp
+++ b/js/src/threading/windows/ConditionVariable.cpp
@@ -376,20 +376,22 @@ js::ConditionVariable::wait_until(Unique
 
 js::CVStatus
 js::ConditionVariable::wait_for(UniqueLock<Mutex>& lock,
                                 const mozilla::TimeDuration& rel_time)
 {
   CRITICAL_SECTION* cs = &lock.lock.platformData()->criticalSection;
 
   // Note that DWORD is unsigned, so we have to be careful to clamp at 0.
+  // If rel_time is Forever, then ToMilliseconds is +inf, which evaluates as
+  // greater than UINT32_MAX, resulting in the correct INFINITE wait.
   double msecd = rel_time.ToMilliseconds();
   DWORD msec = msecd < 0.0
                ? 0
-               : msecd > DBL_MAX
+               : msecd > UINT32_MAX
                  ? INFINITE
                  : static_cast<DWORD>(msecd);
 
   BOOL r;
   if (sNativeImports.supported())
     r = platformData()->native.wait(cs, msec);
   else
     r = platformData()->fallback.wait(cs, msec);