| 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 | 68 |
| 69 #include "build/build_config.h" | 69 #include "build/build_config.h" |
| 70 | 70 |
| 71 #if defined(OS_WIN) | 71 #if defined(OS_WIN) |
| 72 #include <windows.h> | 72 #include <windows.h> |
| 73 #elif defined(OS_POSIX) | 73 #elif defined(OS_POSIX) |
| 74 #include <pthread.h> | 74 #include <pthread.h> |
| 75 #endif | 75 #endif |
| 76 | 76 |
| 77 #include "base/basictypes.h" | 77 #include "base/basictypes.h" |
| 78 #include "base/lock.h" | 78 #include "base/synchronization/lock.h" |
| 79 | 79 |
| 80 namespace base { | 80 namespace base { |
| 81 | 81 |
| 82 class TimeDelta; | 82 class TimeDelta; |
| 83 | 83 |
| 84 class ConditionVariable { | 84 class ConditionVariable { |
| 85 public: | 85 public: |
| 86 // Construct a cv for use with ONLY one user lock. | 86 // Construct a cv for use with ONLY one user lock. |
| 87 explicit ConditionVariable(Lock* user_lock); | 87 explicit ConditionVariable(Lock* user_lock); |
| 88 | 88 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 // This helps with defensive destructor coding in the face of user error. | 149 // This helps with defensive destructor coding in the face of user error. |
| 150 enum RunState { SHUTDOWN = 0, RUNNING = 64213 }; | 150 enum RunState { SHUTDOWN = 0, RUNNING = 64213 }; |
| 151 | 151 |
| 152 // Internal implementation methods supporting Wait(). | 152 // Internal implementation methods supporting Wait(). |
| 153 Event* GetEventForWaiting(); | 153 Event* GetEventForWaiting(); |
| 154 void RecycleEvent(Event* used_event); | 154 void RecycleEvent(Event* used_event); |
| 155 | 155 |
| 156 RunState run_state_; | 156 RunState run_state_; |
| 157 | 157 |
| 158 // Private critical section for access to member data. | 158 // Private critical section for access to member data. |
| 159 Lock internal_lock_; | 159 base::Lock internal_lock_; |
| 160 | 160 |
| 161 // Lock that is acquired before calling Wait(). | 161 // Lock that is acquired before calling Wait(). |
| 162 Lock& user_lock_; | 162 base::Lock& user_lock_; |
| 163 | 163 |
| 164 // Events that threads are blocked on. | 164 // Events that threads are blocked on. |
| 165 Event waiting_list_; | 165 Event waiting_list_; |
| 166 | 166 |
| 167 // Free list for old events. | 167 // Free list for old events. |
| 168 Event recycling_list_; | 168 Event recycling_list_; |
| 169 int recycling_list_size_; | 169 int recycling_list_size_; |
| 170 | 170 |
| 171 // The number of allocated, but not yet deleted events. | 171 // The number of allocated, but not yet deleted events. |
| 172 int allocation_counter_; | 172 int allocation_counter_; |
| 173 | 173 |
| 174 #elif defined(OS_POSIX) | 174 #elif defined(OS_POSIX) |
| 175 | 175 |
| 176 pthread_cond_t condition_; | 176 pthread_cond_t condition_; |
| 177 pthread_mutex_t* user_mutex_; | 177 pthread_mutex_t* user_mutex_; |
| 178 #if !defined(NDEBUG) | 178 #if !defined(NDEBUG) |
| 179 Lock* user_lock_; // Needed to adjust shadow lock state on wait. | 179 base::Lock* user_lock_; // Needed to adjust shadow lock state on wait. |
| 180 #endif | 180 #endif |
| 181 | 181 |
| 182 #endif | 182 #endif |
| 183 | 183 |
| 184 DISALLOW_COPY_AND_ASSIGN(ConditionVariable); | 184 DISALLOW_COPY_AND_ASSIGN(ConditionVariable); |
| 185 }; | 185 }; |
| 186 | 186 |
| 187 } // namespace base | 187 } // namespace base |
| 188 | 188 |
| 189 #endif // BASE_SYNCHRONIZATION_CONDITION_VARIABLE_H_ | 189 #endif // BASE_SYNCHRONIZATION_CONDITION_VARIABLE_H_ |
| OLD | NEW |