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