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
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef threading_ConditionVariable_h
8 #define threading_ConditionVariable_h
9 
10 #include "mozilla/Attributes.h"
11 #include "mozilla/Move.h"
12 #include "mozilla/TimeStamp.h"
13 
14 #include <stdint.h>
15 #ifndef XP_WIN
16 # include <pthread.h>
17 #endif
18 
19 #include "threading/LockGuard.h"
20 #include "threading/Mutex.h"
21 
22 namespace js {
23 
24 template <typename T> using UniqueLock = LockGuard<T>;
25 
26 enum class CVStatus {
27   NoTimeout,
28   Timeout
29 };
30 
31 // A poly-fill for std::condition_variable.
32 class ConditionVariable
33 {
34 public:
35   struct PlatformData;
36 
37   ConditionVariable();
38   ~ConditionVariable();
39 
40   // Wake one thread that is waiting on this condition.
41   void notify_one();
42 
43   // Wake all threads that are waiting on this condition.
44   void notify_all();
45 
46   // Block the current thread of execution until this condition variable is
47   // woken from another thread via notify_one or notify_all.
48   void wait(UniqueLock<Mutex>& lock);
49 
50   // As with |wait|, block the current thread of execution until woken from
51   // another thread. This method will resume waiting once woken until the given
52   // Predicate |pred| evaluates to true.
53   template <typename Predicate>
wait(UniqueLock<Mutex> & lock,Predicate pred)54   void wait(UniqueLock<Mutex>& lock, Predicate pred) {
55     while (!pred()) {
56       wait(lock);
57     }
58   }
59 
60   // Block the current thread of execution until woken from another thread, or
61   // the given absolute time is reached. The given absolute time is evaluated
62   // when this method is called, so will wake up after (abs_time - now),
63   // independent of system clock changes. While insulated from clock changes,
64   // this API is succeptible to the issues discussed above wait_for.
65   CVStatus wait_until(UniqueLock<Mutex>& lock,
66                       const mozilla::TimeStamp& abs_time);
67 
68   // As with |wait_until|, block the current thread of execution until woken
69   // from another thread, or the given absolute time is reached. This method
70   // will resume waiting once woken until the given Predicate |pred| evaluates
71   // to true.
72   template <typename Predicate>
wait_until(UniqueLock<Mutex> & lock,const mozilla::TimeStamp & abs_time,Predicate pred)73   bool wait_until(UniqueLock<Mutex>& lock, const mozilla::TimeStamp& abs_time,
74                   Predicate pred) {
75     while (!pred()) {
76       if (wait_until(lock, abs_time) == CVStatus::Timeout) {
77         return pred();
78       }
79     }
80     return true;
81   }
82 
83   // Block the current thread of execution until woken from another thread, or
84   // the given time duration has elapsed. Given that the system may be
85   // interrupted between the callee and the actual wait beginning, this call
86   // has a minimum granularity of the system's scheduling interval, and may
87   // encounter substantially longer delays, depending on system load.
88   CVStatus wait_for(UniqueLock<Mutex>& lock,
89                     const mozilla::TimeDuration& rel_time);
90 
91   // As with |wait_for|, block the current thread of execution until woken from
92   // another thread or the given time duration has elapsed. This method will
93   // resume waiting once woken until the given Predicate |pred| evaluates to
94   // true.
95   template <typename Predicate>
wait_for(UniqueLock<Mutex> & lock,const mozilla::TimeDuration & rel_time,Predicate pred)96   bool wait_for(UniqueLock<Mutex>& lock, const mozilla::TimeDuration& rel_time,
97                 Predicate pred) {
98     return wait_until(lock, mozilla::TimeStamp::Now() + rel_time,
99                       mozilla::Move(pred));
100   }
101 
102 
103 private:
104   ConditionVariable(const ConditionVariable&) = delete;
105   ConditionVariable& operator=(const ConditionVariable&) = delete;
106 
107   PlatformData* platformData();
108 
109 #ifndef XP_WIN
110   void* platformData_[sizeof(pthread_cond_t) / sizeof(void*)];
111   static_assert(sizeof(pthread_cond_t) / sizeof(void*) != 0 &&
112                 sizeof(pthread_cond_t) % sizeof(void*) == 0,
113                 "pthread_cond_t must have pointer alignment");
114 #else
115   void* platformData_[4];
116 #endif
117 };
118 
119 } // namespace js
120 
121 #endif // threading_ConditionVariable_h
122