Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
| 5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
| 6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/base/platform_thread.h" | 11 #include "webrtc/base/platform_thread.h" |
| 12 | 12 |
| 13 #include <inttypes.h> | |
|
the sun
2017/03/01 23:31:36
stdint.h
tommi
2017/03/02 14:11:59
removed now
| |
| 14 | |
| 13 #include "webrtc/base/atomicops.h" | 15 #include "webrtc/base/atomicops.h" |
| 14 #include "webrtc/base/checks.h" | 16 #include "webrtc/base/checks.h" |
| 17 #include "webrtc/base/timeutils.h" | |
| 15 | 18 |
| 16 #if defined(WEBRTC_LINUX) | 19 #if defined(WEBRTC_LINUX) |
| 17 #include <sys/prctl.h> | 20 #include <sys/prctl.h> |
| 18 #include <sys/syscall.h> | 21 #include <sys/syscall.h> |
| 19 #endif | 22 #endif |
| 20 | 23 |
| 21 namespace rtc { | 24 namespace rtc { |
| 22 | 25 |
| 23 PlatformThreadId CurrentThreadId() { | 26 PlatformThreadId CurrentThreadId() { |
| 24 PlatformThreadId ret; | 27 PlatformThreadId ret; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 void PlatformThread::Run() { | 216 void PlatformThread::Run() { |
| 214 // Attach the worker thread checker to this thread. | 217 // Attach the worker thread checker to this thread. |
| 215 RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); | 218 RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); |
| 216 rtc::SetCurrentThreadName(name_.c_str()); | 219 rtc::SetCurrentThreadName(name_.c_str()); |
| 217 | 220 |
| 218 if (run_function_) { | 221 if (run_function_) { |
| 219 SetPriority(priority_); | 222 SetPriority(priority_); |
| 220 run_function_(obj_); | 223 run_function_(obj_); |
| 221 return; | 224 return; |
| 222 } | 225 } |
| 223 // TODO(tommi): Delete the below. | 226 |
| 224 #if !defined(WEBRTC_MAC) && !defined(WEBRTC_WIN) | 227 // TODO(tommi): Delete the rest of this function when looping isn't supported. |
| 225 const struct timespec ts_null = {0}; | 228 #if RTC_DCHECK_IS_ON |
| 229 static const int kMaxLoopCount = 1000; | |
| 230 static const int kPeriodToMeasureMs = 100; | |
| 231 int64_t loop_stamps[kMaxLoopCount] = {}; | |
| 232 int64_t sequence_nr = 0; | |
| 226 #endif | 233 #endif |
| 234 | |
| 227 do { | 235 do { |
| 228 // The interface contract of Start/Stop is that for a successful call to | 236 // The interface contract of Start/Stop is that for a successful call to |
| 229 // Start, there should be at least one call to the run function. So we | 237 // Start, there should be at least one call to the run function. So we |
| 230 // call the function before checking |stop_|. | 238 // call the function before checking |stop_|. |
| 231 if (!run_function_deprecated_(obj_)) | 239 if (!run_function_deprecated_(obj_)) |
| 232 break; | 240 break; |
| 241 #if RTC_DCHECK_IS_ON | |
|
the sun
2017/03/01 23:31:36
A comment wouldn't hurt, considering the above con
tommi
2017/03/02 14:11:59
Done.
| |
| 242 auto id = sequence_nr % kMaxLoopCount; | |
| 243 loop_stamps[id] = rtc::TimeMillis(); | |
| 244 if (sequence_nr > kMaxLoopCount) { | |
| 245 auto compare_id = (id + 1) % kMaxLoopCount; | |
| 246 auto diff = loop_stamps[id] - loop_stamps[compare_id]; | |
|
the sun
2017/03/01 23:31:36
Why do you store the whole sequence, when you're o
tommi
2017/03/02 14:11:59
Since this is a sliding window, the last time stam
the sun
2017/03/02 14:20:09
Ah, got the indices wrong - thanks for explaining.
| |
| 247 RTC_DCHECK_GE(diff, 0); | |
| 248 if (diff < kPeriodToMeasureMs) { | |
| 249 RTC_NOTREACHED() << "This thread is too busy: " << name_ << " " << diff | |
| 250 << "ms sequence=" << sequence_nr << " " << loop_stamps[id] << " vs " | |
| 251 << loop_stamps[compare_id] << ", " << id << " vs " << compare_id; | |
| 252 } | |
| 253 } | |
| 254 ++sequence_nr; | |
| 255 #endif | |
| 233 #if defined(WEBRTC_WIN) | 256 #if defined(WEBRTC_WIN) |
| 234 // Alertable sleep to permit RaiseFlag to run and update |stop_|. | 257 // Alertable sleep to permit RaiseFlag to run and update |stop_|. |
| 235 SleepEx(0, true); | 258 SleepEx(0, true); |
| 236 } while (!stop_); | 259 } while (!stop_); |
| 237 #else | 260 #else |
| 238 #if defined(WEBRTC_MAC) | |
| 239 sched_yield(); | 261 sched_yield(); |
| 240 #else | |
| 241 nanosleep(&ts_null, nullptr); | |
| 242 #endif | |
| 243 } while (!AtomicOps::AcquireLoad(&stop_flag_)); | 262 } while (!AtomicOps::AcquireLoad(&stop_flag_)); |
| 244 #endif // defined(WEBRTC_WIN) | 263 #endif // defined(WEBRTC_WIN) |
| 245 } | 264 } |
| 246 | 265 |
| 247 bool PlatformThread::SetPriority(ThreadPriority priority) { | 266 bool PlatformThread::SetPriority(ThreadPriority priority) { |
| 248 #if RTC_DCHECK_IS_ON | 267 #if RTC_DCHECK_IS_ON |
| 249 if (run_function_) { | 268 if (run_function_) { |
| 250 // The non-deprecated way of how this function gets called, is that it must | 269 // The non-deprecated way of how this function gets called, is that it must |
| 251 // be called on the worker thread itself. | 270 // be called on the worker thread itself. |
| 252 RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); | 271 RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 312 #if defined(WEBRTC_WIN) | 331 #if defined(WEBRTC_WIN) |
| 313 bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) { | 332 bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) { |
| 314 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 333 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 315 RTC_DCHECK(IsRunning()); | 334 RTC_DCHECK(IsRunning()); |
| 316 | 335 |
| 317 return QueueUserAPC(function, thread_, data) != FALSE; | 336 return QueueUserAPC(function, thread_, data) != FALSE; |
| 318 } | 337 } |
| 319 #endif | 338 #endif |
| 320 | 339 |
| 321 } // namespace rtc | 340 } // namespace rtc |
| OLD | NEW |