OLD | NEW |
(Empty) | |
| 1 // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are |
| 4 // met: |
| 5 // |
| 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. |
| 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
| 28 #ifndef V8_PLATFORM_CONDITION_VARIABLE_H_ |
| 29 #define V8_PLATFORM_CONDITION_VARIABLE_H_ |
| 30 |
| 31 #include "platform/mutex.h" |
| 32 |
| 33 namespace v8 { |
| 34 namespace internal { |
| 35 |
| 36 // Forward declarations. |
| 37 class ConditionVariableEvent; |
| 38 class TimeDelta; |
| 39 |
| 40 // ----------------------------------------------------------------------------- |
| 41 // ConditionVariable |
| 42 // |
| 43 // This class is a synchronization primitive that can be used to block a thread, |
| 44 // or multiple threads at the same time, until: |
| 45 // - a notification is received from another thread, |
| 46 // - a timeout expires, or |
| 47 // - a spurious wakeup occurs |
| 48 // Any thread that intends to wait on a ConditionVariable has to acquire a lock |
| 49 // on a Mutex first. The |Wait()| and |WaitFor()| operations atomically release |
| 50 // the mutex and suspend the execution of the calling thread. When the condition |
| 51 // variable is notified, the thread is awakened, and the mutex is reacquired. |
| 52 |
| 53 class ConditionVariable V8_FINAL { |
| 54 public: |
| 55 ConditionVariable(); |
| 56 ~ConditionVariable(); |
| 57 |
| 58 // If any threads are waiting on this condition variable, calling |
| 59 // |NotifyOne()| unblocks one of the waiting threads. |
| 60 void NotifyOne(); |
| 61 |
| 62 // Unblocks all threads currently waiting for this condition variable. |
| 63 void NotifyAll(); |
| 64 |
| 65 // |Wait()| causes the calling thread to block until the condition variable is |
| 66 // notified or a spurious wakeup occurs. Atomically releases the mutex, blocks |
| 67 // the current executing thread, and adds it to the list of threads waiting on |
| 68 // this condition variable. The thread will be unblocked when |NotifyAll()| or |
| 69 // |NotifyOne()| is executed. It may also be unblocked spuriously. When |
| 70 // unblocked, regardless of the reason, the lock on the mutex is reacquired |
| 71 // and |Wait()| exits. |
| 72 void Wait(Mutex* mutex); |
| 73 |
| 74 // Atomically releases the mutex, blocks the current executing thread, and |
| 75 // adds it to the list of threads waiting on this condition variable. The |
| 76 // thread will be unblocked when |NotifyAll()| or |NotifyOne()| is executed, |
| 77 // or when the relative timeout |rel_time| expires. It may also be unblocked |
| 78 // spuriously. When unblocked, regardless of the reason, the lock on the mutex |
| 79 // is reacquired and |WaitFor()| exits. Returns true if the condition variable |
| 80 // was notified prior to the timeout. |
| 81 bool WaitFor(Mutex* mutex, const TimeDelta& rel_time) V8_WARN_UNUSED_RESULT; |
| 82 |
| 83 // The implementation-defined native handle type. |
| 84 #if V8_OS_POSIX |
| 85 typedef pthread_cond_t NativeHandle; |
| 86 #elif V8_OS_WIN |
| 87 struct Event; |
| 88 class NativeHandle V8_FINAL { |
| 89 public: |
| 90 NativeHandle() : waitlist_(NULL), freelist_(NULL) {} |
| 91 ~NativeHandle(); |
| 92 |
| 93 Event* Pre() V8_WARN_UNUSED_RESULT; |
| 94 void Post(Event* event, bool result); |
| 95 |
| 96 Mutex* mutex() { return &mutex_; } |
| 97 Event* waitlist() { return waitlist_; } |
| 98 |
| 99 private: |
| 100 Event* waitlist_; |
| 101 Event* freelist_; |
| 102 Mutex mutex_; |
| 103 |
| 104 DISALLOW_COPY_AND_ASSIGN(NativeHandle); |
| 105 }; |
| 106 #endif |
| 107 |
| 108 NativeHandle& native_handle() V8_WARN_UNUSED_RESULT { |
| 109 return native_handle_; |
| 110 } |
| 111 const NativeHandle& native_handle() const V8_WARN_UNUSED_RESULT { |
| 112 return native_handle_; |
| 113 } |
| 114 |
| 115 private: |
| 116 NativeHandle native_handle_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 119 }; |
| 120 |
| 121 |
| 122 // POD ConditionVariable initialized lazily (i.e. the first time Pointer() is |
| 123 // called). |
| 124 // Usage: |
| 125 // static LazyConditionVariable my_condvar = |
| 126 // LAZY_CONDITION_VARIABLE_INITIALIZER; |
| 127 // |
| 128 // void my_function() { |
| 129 // LockGuard<Mutex> lock_guard(&my_mutex); |
| 130 // my_condvar.Pointer()->Wait(&my_mutex); |
| 131 // } |
| 132 typedef LazyStaticInstance<ConditionVariable, |
| 133 DefaultConstructTrait<ConditionVariable>, |
| 134 ThreadSafeInitOnceTrait>::type LazyConditionVariable; |
| 135 |
| 136 #define LAZY_CONDITION_VARIABLE_INITIALIZER LAZY_STATIC_INSTANCE_INITIALIZER |
| 137 |
| 138 } } // namespace v8::internal |
| 139 |
| 140 #endif // V8_PLATFORM_CONDITION_VARIABLE_H_ |
OLD | NEW |