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 <stddef.h> | 5 #include <stddef.h> |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/debug/activity_tracker.h" | |
11 #include "base/logging.h" | 10 #include "base/logging.h" |
12 #include "base/synchronization/condition_variable.h" | 11 #include "base/synchronization/condition_variable.h" |
13 #include "base/synchronization/lock.h" | 12 #include "base/synchronization/lock.h" |
14 #include "base/synchronization/waitable_event.h" | 13 #include "base/synchronization/waitable_event.h" |
15 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
16 | 15 |
17 // ----------------------------------------------------------------------------- | 16 // ----------------------------------------------------------------------------- |
18 // A WaitableEvent on POSIX is implemented as a wait-list. Currently we don't | 17 // A WaitableEvent on POSIX is implemented as a wait-list. Currently we don't |
19 // support cross-process events (where one process can signal an event which | 18 // support cross-process events (where one process can signal an event which |
20 // others are waiting on). Because of this, we can avoid having one thread per | 19 // others are waiting on). Because of this, we can avoid having one thread per |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
151 base::Lock lock_; | 150 base::Lock lock_; |
152 base::ConditionVariable cv_; | 151 base::ConditionVariable cv_; |
153 }; | 152 }; |
154 | 153 |
155 void WaitableEvent::Wait() { | 154 void WaitableEvent::Wait() { |
156 bool result = TimedWait(TimeDelta::FromSeconds(-1)); | 155 bool result = TimedWait(TimeDelta::FromSeconds(-1)); |
157 DCHECK(result) << "TimedWait() should never fail with infinite timeout"; | 156 DCHECK(result) << "TimedWait() should never fail with infinite timeout"; |
158 } | 157 } |
159 | 158 |
160 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 159 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
161 // Record the event that this thread is blocking upon (for hang diagnosis). | |
162 base::debug::ScopedEventWaitActivity event_activity(this); | |
163 | |
164 base::ThreadRestrictions::AssertWaitAllowed(); | 160 base::ThreadRestrictions::AssertWaitAllowed(); |
165 const TimeTicks end_time(TimeTicks::Now() + max_time); | 161 const TimeTicks end_time(TimeTicks::Now() + max_time); |
166 const bool finite_time = max_time.ToInternalValue() >= 0; | 162 const bool finite_time = max_time.ToInternalValue() >= 0; |
167 | 163 |
168 kernel_->lock_.Acquire(); | 164 kernel_->lock_.Acquire(); |
169 if (kernel_->signaled_) { | 165 if (kernel_->signaled_) { |
170 if (!kernel_->manual_reset_) { | 166 if (!kernel_->manual_reset_) { |
171 // In this case we were signaled when we had no waiters. Now that | 167 // In this case we were signaled when we had no waiters. Now that |
172 // someone has waited upon us, we can automatically reset. | 168 // someone has waited upon us, we can automatically reset. |
173 kernel_->signaled_ = false; | 169 kernel_->signaled_ = false; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 const std::pair<WaitableEvent*, unsigned> &b) { | 225 const std::pair<WaitableEvent*, unsigned> &b) { |
230 return a.first < b.first; | 226 return a.first < b.first; |
231 } | 227 } |
232 | 228 |
233 // static | 229 // static |
234 size_t WaitableEvent::WaitMany(WaitableEvent** raw_waitables, | 230 size_t WaitableEvent::WaitMany(WaitableEvent** raw_waitables, |
235 size_t count) { | 231 size_t count) { |
236 base::ThreadRestrictions::AssertWaitAllowed(); | 232 base::ThreadRestrictions::AssertWaitAllowed(); |
237 DCHECK(count) << "Cannot wait on no events"; | 233 DCHECK(count) << "Cannot wait on no events"; |
238 | 234 |
239 // Record an event (the first) that this thread is blocking upon. | |
240 base::debug::ScopedEventWaitActivity event_activity(raw_waitables[0]); | |
241 | |
242 // We need to acquire the locks in a globally consistent order. Thus we sort | 235 // We need to acquire the locks in a globally consistent order. Thus we sort |
243 // the array of waitables by address. We actually sort a pairs so that we can | 236 // the array of waitables by address. We actually sort a pairs so that we can |
244 // map back to the original index values later. | 237 // map back to the original index values later. |
245 std::vector<std::pair<WaitableEvent*, size_t> > waitables; | 238 std::vector<std::pair<WaitableEvent*, size_t> > waitables; |
246 waitables.reserve(count); | 239 waitables.reserve(count); |
247 for (size_t i = 0; i < count; ++i) | 240 for (size_t i = 0; i < count; ++i) |
248 waitables.push_back(std::make_pair(raw_waitables[i], i)); | 241 waitables.push_back(std::make_pair(raw_waitables[i], i)); |
249 | 242 |
250 DCHECK_EQ(count, waitables.size()); | 243 DCHECK_EQ(count, waitables.size()); |
251 | 244 |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 return true; | 406 return true; |
414 } | 407 } |
415 } | 408 } |
416 | 409 |
417 return false; | 410 return false; |
418 } | 411 } |
419 | 412 |
420 // ----------------------------------------------------------------------------- | 413 // ----------------------------------------------------------------------------- |
421 | 414 |
422 } // namespace base | 415 } // namespace base |
OLD | NEW |