Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(40)

Side by Side Diff: base/synchronization/waitable_event_posix.cc

Issue 1980743002: Track thread activities in order to diagnose hangs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@readwrite-mmf
Patch Set: more clean-up and addressed review comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 diagonsis).
manzagop (departed) 2016/06/16 15:19:41 typo: disgonsis, here and below.
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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
222 226
223 static bool // StrictWeakOrdering 227 static bool // StrictWeakOrdering
224 cmp_fst_addr(const std::pair<WaitableEvent*, unsigned> &a, 228 cmp_fst_addr(const std::pair<WaitableEvent*, unsigned> &a,
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) {
236 // Record the event that this thread is blocking upon (for hang diagonsis).
manzagop (departed) 2016/06/16 15:19:41 Can |count| be 0?
manzagop (departed) 2016/06/16 15:19:41 Make note only considering first event?
bcwhite 2016/06/16 17:04:43 There's a DCHECK(count) below, so no. But I shoul
237 // Extra parenthesis needed so C++ doesn't think this is a function prototype.
manzagop (departed) 2016/06/16 15:19:40 Dead comment?
bcwhite 2016/06/16 17:04:43 Done.
238 base::debug::ScopedEventWaitActivity event_activity(raw_waitables[0]);
239
232 base::ThreadRestrictions::AssertWaitAllowed(); 240 base::ThreadRestrictions::AssertWaitAllowed();
233 DCHECK(count) << "Cannot wait on no events"; 241 DCHECK(count) << "Cannot wait on no events";
234 242
235 // We need to acquire the locks in a globally consistent order. Thus we sort 243 // 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 244 // the array of waitables by address. We actually sort a pairs so that we can
237 // map back to the original index values later. 245 // map back to the original index values later.
238 std::vector<std::pair<WaitableEvent*, size_t> > waitables; 246 std::vector<std::pair<WaitableEvent*, size_t> > waitables;
239 waitables.reserve(count); 247 waitables.reserve(count);
240 for (size_t i = 0; i < count; ++i) 248 for (size_t i = 0; i < count; ++i)
241 waitables.push_back(std::make_pair(raw_waitables[i], i)); 249 waitables.push_back(std::make_pair(raw_waitables[i], i));
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
406 return true; 414 return true;
407 } 415 }
408 } 416 }
409 417
410 return false; 418 return false;
411 } 419 }
412 420
413 // ----------------------------------------------------------------------------- 421 // -----------------------------------------------------------------------------
414 422
415 } // namespace base 423 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698