Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // ConditionVariable wraps pthreads condition variable synchronization or, on | 5 // ConditionVariable wraps pthreads condition variable synchronization or, on |
| 6 // Windows, simulates it. This functionality is very helpful for having | 6 // Windows, simulates it. This functionality is very helpful for having |
| 7 // several threads wait for an event, as is common with a thread pool managed | 7 // several threads wait for an event, as is common with a thread pool managed |
| 8 // by a master. The meaning of such an event in the (worker) thread pool | 8 // by a master. The meaning of such an event in the (worker) thread pool |
| 9 // scenario is that additional tasks are now available for processing. It is | 9 // scenario is that additional tasks are now available for processing. It is |
| 10 // used in Chrome in the DNS prefetching system to notify worker threads that | 10 // used in Chrome in the DNS prefetching system to notify worker threads that |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 68 #include "base/base_export.h" | 68 #include "base/base_export.h" |
| 69 #include "base/logging.h" | 69 #include "base/logging.h" |
| 70 #include "base/macros.h" | 70 #include "base/macros.h" |
| 71 #include "base/synchronization/lock.h" | 71 #include "base/synchronization/lock.h" |
| 72 #include "build/build_config.h" | 72 #include "build/build_config.h" |
| 73 | 73 |
| 74 #if defined(OS_POSIX) | 74 #if defined(OS_POSIX) |
| 75 #include <pthread.h> | 75 #include <pthread.h> |
| 76 #endif | 76 #endif |
| 77 | 77 |
| 78 #if defined(OS_WIN) | |
| 79 #include <windows.h> | |
| 80 #endif | |
| 81 | |
| 78 namespace base { | 82 namespace base { |
| 79 | 83 |
| 80 class ConditionVarImpl; | |
| 81 class TimeDelta; | 84 class TimeDelta; |
| 82 | 85 |
| 83 class BASE_EXPORT ConditionVariable { | 86 class BASE_EXPORT ConditionVariable { |
| 84 public: | 87 public: |
| 85 // Construct a cv for use with ONLY one user lock. | 88 // Construct a cv for use with ONLY one user lock. |
| 86 explicit ConditionVariable(Lock* user_lock); | 89 explicit ConditionVariable(Lock* user_lock); |
| 87 | 90 |
| 88 ~ConditionVariable(); | 91 ~ConditionVariable(); |
| 89 | 92 |
| 90 // Wait() releases the caller's critical section atomically as it starts to | 93 // Wait() releases the caller's critical section atomically as it starts to |
| 91 // sleep, and the reacquires it when it is signaled. | 94 // sleep, and the reacquires it when it is signaled. |
| 92 void Wait(); | 95 void Wait(); |
| 93 void TimedWait(const TimeDelta& max_time); | 96 void TimedWait(const TimeDelta& max_time); |
| 94 | 97 |
| 95 // Broadcast() revives all waiting threads. | 98 // Broadcast() revives all waiting threads. |
| 96 void Broadcast(); | 99 void Broadcast(); |
| 97 // Signal() revives one waiting thread. | 100 // Signal() revives one waiting thread. |
| 98 void Signal(); | 101 void Signal(); |
| 99 | 102 |
| 100 private: | 103 private: |
| 101 | 104 |
| 102 #if defined(OS_WIN) | 105 #if defined(OS_WIN) |
| 103 ConditionVarImpl* impl_; | 106 CRITICAL_SECTION* const crit_sec_; |
|
scottmg
2016/04/06 18:34:04
nit; swap order to make win & posix match.
robliao
2016/04/06 19:22:18
I would have done that, but it diverges from the i
robliao
2016/04/06 19:24:53
I guess we already do that with user_lock_. Hands
| |
| 107 CONDITION_VARIABLE cv_; | |
| 104 #elif defined(OS_POSIX) | 108 #elif defined(OS_POSIX) |
| 105 pthread_cond_t condition_; | 109 pthread_cond_t condition_; |
| 106 pthread_mutex_t* user_mutex_; | 110 pthread_mutex_t* user_mutex_; |
| 107 #if DCHECK_IS_ON() | |
| 108 base::Lock* user_lock_; // Needed to adjust shadow lock state on wait. | |
| 109 #endif | 111 #endif |
| 110 | 112 |
| 113 #if DCHECK_IS_ON() && (defined(OS_WIN) || defined(OS_POSIX)) | |
| 114 base::Lock* const user_lock_; // Needed to adjust shadow lock state on wait. | |
| 111 #endif | 115 #endif |
| 112 | 116 |
| 113 DISALLOW_COPY_AND_ASSIGN(ConditionVariable); | 117 DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 114 }; | 118 }; |
| 115 | 119 |
| 116 } // namespace base | 120 } // namespace base |
| 117 | 121 |
| 118 #endif // BASE_SYNCHRONIZATION_CONDITION_VARIABLE_H_ | 122 #endif // BASE_SYNCHRONIZATION_CONDITION_VARIABLE_H_ |
| OLD | NEW |