| 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 "webrtc/base/atomicops.h" | 13 #include "webrtc/base/atomicops.h" |
| 14 #include "webrtc/base/checks.h" | 14 #include "webrtc/base/checks.h" |
| 15 #include "webrtc/base/timeutils.h" |
| 15 | 16 |
| 16 #if defined(WEBRTC_LINUX) | 17 #if defined(WEBRTC_LINUX) |
| 17 #include <sys/prctl.h> | 18 #include <sys/prctl.h> |
| 18 #include <sys/syscall.h> | 19 #include <sys/syscall.h> |
| 19 #endif | 20 #endif |
| 20 | 21 |
| 21 namespace rtc { | 22 namespace rtc { |
| 22 | 23 |
| 23 PlatformThreadId CurrentThreadId() { | 24 PlatformThreadId CurrentThreadId() { |
| 24 PlatformThreadId ret; | 25 PlatformThreadId ret; |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 void PlatformThread::Run() { | 214 void PlatformThread::Run() { |
| 214 // Attach the worker thread checker to this thread. | 215 // Attach the worker thread checker to this thread. |
| 215 RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); | 216 RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); |
| 216 rtc::SetCurrentThreadName(name_.c_str()); | 217 rtc::SetCurrentThreadName(name_.c_str()); |
| 217 | 218 |
| 218 if (run_function_) { | 219 if (run_function_) { |
| 219 SetPriority(priority_); | 220 SetPriority(priority_); |
| 220 run_function_(obj_); | 221 run_function_(obj_); |
| 221 return; | 222 return; |
| 222 } | 223 } |
| 223 // TODO(tommi): Delete the below. | 224 |
| 224 #if !defined(WEBRTC_MAC) && !defined(WEBRTC_WIN) | 225 // TODO(tommi): Delete the rest of this function when looping isn't supported. |
| 225 const struct timespec ts_null = {0}; | 226 #if RTC_DCHECK_IS_ON |
| 227 // These constants control the busy loop detection algorithm below. |
| 228 // |kMaxLoopCount| controls the limit for how many times we allow the loop |
| 229 // to run within a period, before DCHECKing. |
| 230 // |kPeriodToMeasureMs| controls how long that period is. |
| 231 static const int kMaxLoopCount = 1000; |
| 232 static const int kPeriodToMeasureMs = 100; |
| 233 int64_t loop_stamps[kMaxLoopCount] = {}; |
| 234 int64_t sequence_nr = 0; |
| 226 #endif | 235 #endif |
| 236 |
| 227 do { | 237 do { |
| 228 // The interface contract of Start/Stop is that for a successful call to | 238 // 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 | 239 // Start, there should be at least one call to the run function. So we |
| 230 // call the function before checking |stop_|. | 240 // call the function before checking |stop_|. |
| 231 if (!run_function_deprecated_(obj_)) | 241 if (!run_function_deprecated_(obj_)) |
| 232 break; | 242 break; |
| 243 #if RTC_DCHECK_IS_ON |
| 244 auto id = sequence_nr % kMaxLoopCount; |
| 245 loop_stamps[id] = rtc::TimeMillis(); |
| 246 if (sequence_nr > kMaxLoopCount) { |
| 247 auto compare_id = (id + 1) % kMaxLoopCount; |
| 248 auto diff = loop_stamps[id] - loop_stamps[compare_id]; |
| 249 RTC_DCHECK_GE(diff, 0); |
| 250 if (diff < kPeriodToMeasureMs) { |
| 251 RTC_NOTREACHED() << "This thread is too busy: " << name_ << " " << diff |
| 252 << "ms sequence=" << sequence_nr << " " |
| 253 << loop_stamps[id] << " vs " << loop_stamps[compare_id] |
| 254 << ", " << id << " vs " << compare_id; |
| 255 } |
| 256 } |
| 257 ++sequence_nr; |
| 258 #endif |
| 233 #if defined(WEBRTC_WIN) | 259 #if defined(WEBRTC_WIN) |
| 234 // Alertable sleep to permit RaiseFlag to run and update |stop_|. | 260 // Alertable sleep to permit RaiseFlag to run and update |stop_|. |
| 235 SleepEx(0, true); | 261 SleepEx(0, true); |
| 236 } while (!stop_); | 262 } while (!stop_); |
| 237 #else | 263 #else |
| 238 #if defined(WEBRTC_MAC) | 264 #if defined(UNDEFINED_SANITIZER) || defined(WEBRTC_ANDROID) |
| 265 // UBSAN and Android don't like |sched_yield()| that much. |
| 266 static const struct timespec ts_null = {0}; |
| 267 nanosleep(&ts_null, nullptr); |
| 268 #else // !(defined(UNDEFINED_SANITIZER) || defined(WEBRTC_ANDROID)) |
| 269 // Mac and Linux show better performance with sched_yield. |
| 239 sched_yield(); | 270 sched_yield(); |
| 240 #else | |
| 241 nanosleep(&ts_null, nullptr); | |
| 242 #endif | 271 #endif |
| 243 } while (!AtomicOps::AcquireLoad(&stop_flag_)); | 272 } while (!AtomicOps::AcquireLoad(&stop_flag_)); |
| 244 #endif // defined(WEBRTC_WIN) | 273 #endif // defined(WEBRTC_WIN) |
| 245 } | 274 } |
| 246 | 275 |
| 247 bool PlatformThread::SetPriority(ThreadPriority priority) { | 276 bool PlatformThread::SetPriority(ThreadPriority priority) { |
| 248 #if RTC_DCHECK_IS_ON | 277 #if RTC_DCHECK_IS_ON |
| 249 if (run_function_) { | 278 if (run_function_) { |
| 250 // The non-deprecated way of how this function gets called, is that it must | 279 // The non-deprecated way of how this function gets called, is that it must |
| 251 // be called on the worker thread itself. | 280 // be called on the worker thread itself. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 #if defined(WEBRTC_WIN) | 341 #if defined(WEBRTC_WIN) |
| 313 bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) { | 342 bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) { |
| 314 RTC_DCHECK(thread_checker_.CalledOnValidThread()); | 343 RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 315 RTC_DCHECK(IsRunning()); | 344 RTC_DCHECK(IsRunning()); |
| 316 | 345 |
| 317 return QueueUserAPC(function, thread_, data) != FALSE; | 346 return QueueUserAPC(function, thread_, data) != FALSE; |
| 318 } | 347 } |
| 319 #endif | 348 #endif |
| 320 | 349 |
| 321 } // namespace rtc | 350 } // namespace rtc |
| OLD | NEW |