OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include <algorithm> | 5 #include <algorithm> |
6 #include <vector> | 6 #include <vector> |
7 | 7 |
| 8 #include "base/logging.h" |
8 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
9 | |
10 #include "base/synchronization/condition_variable.h" | 10 #include "base/synchronization/condition_variable.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
12 #include "base/logging.h" | 12 #include "base/threading/thread_restrictions.h" |
13 | 13 |
14 // ----------------------------------------------------------------------------- | 14 // ----------------------------------------------------------------------------- |
15 // A WaitableEvent on POSIX is implemented as a wait-list. Currently we don't | 15 // A WaitableEvent on POSIX is implemented as a wait-list. Currently we don't |
16 // support cross-process events (where one process can signal an event which | 16 // support cross-process events (where one process can signal an event which |
17 // others are waiting on). Because of this, we can avoid having one thread per | 17 // others are waiting on). Because of this, we can avoid having one thread per |
18 // listener in several cases. | 18 // listener in several cases. |
19 // | 19 // |
20 // The WaitableEvent maintains a list of waiters, protected by a lock. Each | 20 // The WaitableEvent maintains a list of waiters, protected by a lock. Each |
21 // waiter is either an async wait, in which case we have a Task and the | 21 // waiter is either an async wait, in which case we have a Task and the |
22 // MessageLoop to run it on, or a blocking wait, in which case we have the | 22 // MessageLoop to run it on, or a blocking wait, in which case we have the |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 base::Lock lock_; | 151 base::Lock lock_; |
152 base::ConditionVariable cv_; | 152 base::ConditionVariable cv_; |
153 }; | 153 }; |
154 | 154 |
155 void WaitableEvent::Wait() { | 155 void WaitableEvent::Wait() { |
156 bool result = TimedWait(TimeDelta::FromSeconds(-1)); | 156 bool result = TimedWait(TimeDelta::FromSeconds(-1)); |
157 DCHECK(result) << "TimedWait() should never fail with infinite timeout"; | 157 DCHECK(result) << "TimedWait() should never fail with infinite timeout"; |
158 } | 158 } |
159 | 159 |
160 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 160 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
| 161 base::ThreadRestrictions::AssertWaitAllowed(); |
161 const Time end_time(Time::Now() + max_time); | 162 const Time end_time(Time::Now() + max_time); |
162 const bool finite_time = max_time.ToInternalValue() >= 0; | 163 const bool finite_time = max_time.ToInternalValue() >= 0; |
163 | 164 |
164 kernel_->lock_.Acquire(); | 165 kernel_->lock_.Acquire(); |
165 if (kernel_->signaled_) { | 166 if (kernel_->signaled_) { |
166 if (!kernel_->manual_reset_) { | 167 if (!kernel_->manual_reset_) { |
167 // In this case we were signaled when we had no waiters. Now that | 168 // In this case we were signaled when we had no waiters. Now that |
168 // someone has waited upon us, we can automatically reset. | 169 // someone has waited upon us, we can automatically reset. |
169 kernel_->signaled_ = false; | 170 kernel_->signaled_ = false; |
170 } | 171 } |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 218 |
218 static bool // StrictWeakOrdering | 219 static bool // StrictWeakOrdering |
219 cmp_fst_addr(const std::pair<WaitableEvent*, unsigned> &a, | 220 cmp_fst_addr(const std::pair<WaitableEvent*, unsigned> &a, |
220 const std::pair<WaitableEvent*, unsigned> &b) { | 221 const std::pair<WaitableEvent*, unsigned> &b) { |
221 return a.first < b.first; | 222 return a.first < b.first; |
222 } | 223 } |
223 | 224 |
224 // static | 225 // static |
225 size_t WaitableEvent::WaitMany(WaitableEvent** raw_waitables, | 226 size_t WaitableEvent::WaitMany(WaitableEvent** raw_waitables, |
226 size_t count) { | 227 size_t count) { |
| 228 base::ThreadRestrictions::AssertWaitAllowed(); |
227 DCHECK(count) << "Cannot wait on no events"; | 229 DCHECK(count) << "Cannot wait on no events"; |
228 | 230 |
229 // We need to acquire the locks in a globally consistent order. Thus we sort | 231 // We need to acquire the locks in a globally consistent order. Thus we sort |
230 // the array of waitables by address. We actually sort a pairs so that we can | 232 // the array of waitables by address. We actually sort a pairs so that we can |
231 // map back to the original index values later. | 233 // map back to the original index values later. |
232 std::vector<std::pair<WaitableEvent*, size_t> > waitables; | 234 std::vector<std::pair<WaitableEvent*, size_t> > waitables; |
233 waitables.reserve(count); | 235 waitables.reserve(count); |
234 for (size_t i = 0; i < count; ++i) | 236 for (size_t i = 0; i < count; ++i) |
235 waitables.push_back(std::make_pair(raw_waitables[i], i)); | 237 waitables.push_back(std::make_pair(raw_waitables[i], i)); |
236 | 238 |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
396 return true; | 398 return true; |
397 } | 399 } |
398 } | 400 } |
399 | 401 |
400 return false; | 402 return false; |
401 } | 403 } |
402 | 404 |
403 // ----------------------------------------------------------------------------- | 405 // ----------------------------------------------------------------------------- |
404 | 406 |
405 } // namespace base | 407 } // namespace base |
OLD | NEW |