Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/platform_thread.h" |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 12 | |
Yves Gerey | 988cc08 | 2018-10-23 12:03:01 +0200 | [diff] [blame] | 13 | #if !defined(WEBRTC_WIN) |
| 14 | #include <sched.h> |
| 15 | #endif |
| 16 | #include <stdint.h> |
| 17 | #include <time.h> |
| 18 | #include <algorithm> |
| 19 | |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 20 | #include "rtc_base/atomic_ops.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/checks.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame^] | 22 | #include "rtc_base/time_utils.h" |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 23 | |
Tommi | bebc690 | 2015-05-18 09:51:42 +0200 | [diff] [blame] | 24 | namespace rtc { |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 25 | namespace { |
| 26 | #if defined(WEBRTC_WIN) |
| 27 | void CALLBACK RaiseFlag(ULONG_PTR param) { |
| 28 | *reinterpret_cast<bool*>(param) = true; |
| 29 | } |
| 30 | #else |
| 31 | struct ThreadAttributes { |
| 32 | ThreadAttributes() { pthread_attr_init(&attr); } |
| 33 | ~ThreadAttributes() { pthread_attr_destroy(&attr); } |
| 34 | pthread_attr_t* operator&() { return &attr; } |
| 35 | pthread_attr_t attr; |
| 36 | }; |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 37 | #endif // defined(WEBRTC_WIN) |
| 38 | } |
| 39 | |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 40 | PlatformThread::PlatformThread(ThreadRunFunctionDeprecated func, |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 41 | void* obj, |
| 42 | const char* thread_name) |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 43 | : run_function_deprecated_(func), |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 44 | obj_(obj), |
tommi | 82ead60 | 2017-02-19 16:09:55 -0800 | [diff] [blame] | 45 | name_(thread_name ? thread_name : "webrtc") { |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 46 | RTC_DCHECK(func); |
| 47 | RTC_DCHECK(name_.length() < 64); |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 48 | spawned_thread_checker_.DetachFromThread(); |
| 49 | } |
| 50 | |
| 51 | PlatformThread::PlatformThread(ThreadRunFunction func, |
| 52 | void* obj, |
| 53 | const char* thread_name, |
| 54 | ThreadPriority priority /*= kNormalPriority*/) |
| 55 | : run_function_(func), priority_(priority), obj_(obj), name_(thread_name) { |
| 56 | RTC_DCHECK(func); |
| 57 | RTC_DCHECK(!name_.empty()); |
| 58 | // TODO(tommi): Consider lowering the limit to 15 (limit on Linux). |
| 59 | RTC_DCHECK(name_.length() < 64); |
| 60 | spawned_thread_checker_.DetachFromThread(); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | PlatformThread::~PlatformThread() { |
| 64 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 65 | #if defined(WEBRTC_WIN) |
| 66 | RTC_DCHECK(!thread_); |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 67 | RTC_DCHECK(!thread_id_); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 68 | #endif // defined(WEBRTC_WIN) |
| 69 | } |
| 70 | |
| 71 | #if defined(WEBRTC_WIN) |
| 72 | DWORD WINAPI PlatformThread::StartThread(void* param) { |
perkj | 6a2e20a | 2016-11-30 04:53:08 -0800 | [diff] [blame] | 73 | // The GetLastError() function only returns valid results when it is called |
| 74 | // after a Win32 API function that returns a "failed" result. A crash dump |
| 75 | // contains the result from GetLastError() and to make sure it does not |
| 76 | // falsely report a Windows error we call SetLastError here. |
| 77 | ::SetLastError(ERROR_SUCCESS); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 78 | static_cast<PlatformThread*>(param)->Run(); |
| 79 | return 0; |
| 80 | } |
| 81 | #else |
| 82 | void* PlatformThread::StartThread(void* param) { |
| 83 | static_cast<PlatformThread*>(param)->Run(); |
| 84 | return 0; |
| 85 | } |
| 86 | #endif // defined(WEBRTC_WIN) |
| 87 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 88 | void PlatformThread::Start() { |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 89 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 90 | RTC_DCHECK(!thread_) << "Thread already started?"; |
| 91 | #if defined(WEBRTC_WIN) |
| 92 | stop_ = false; |
| 93 | |
| 94 | // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION. |
| 95 | // Set the reserved stack stack size to 1M, which is the default on Windows |
| 96 | // and Linux. |
deadbeef | 37f5ecf | 2017-02-27 14:06:41 -0800 | [diff] [blame] | 97 | thread_ = ::CreateThread(nullptr, 1024 * 1024, &StartThread, this, |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 98 | STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id_); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 99 | RTC_CHECK(thread_) << "CreateThread failed"; |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 100 | RTC_DCHECK(thread_id_); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 101 | #else |
| 102 | ThreadAttributes attr; |
| 103 | // Set the stack stack size to 1M. |
| 104 | pthread_attr_setstacksize(&attr, 1024 * 1024); |
| 105 | RTC_CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this)); |
| 106 | #endif // defined(WEBRTC_WIN) |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 109 | bool PlatformThread::IsRunning() const { |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 110 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 111 | #if defined(WEBRTC_WIN) |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 112 | return thread_ != nullptr; |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 113 | #else |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 114 | return thread_ != 0; |
| 115 | #endif // defined(WEBRTC_WIN) |
| 116 | } |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 117 | |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 118 | PlatformThreadRef PlatformThread::GetThreadRef() const { |
| 119 | #if defined(WEBRTC_WIN) |
| 120 | return thread_id_; |
| 121 | #else |
| 122 | return thread_; |
| 123 | #endif // defined(WEBRTC_WIN) |
| 124 | } |
| 125 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 126 | void PlatformThread::Stop() { |
| 127 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 128 | if (!IsRunning()) |
| 129 | return; |
| 130 | |
| 131 | #if defined(WEBRTC_WIN) |
| 132 | // Set stop_ to |true| on the worker thread. |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 133 | bool queued = QueueAPC(&RaiseFlag, reinterpret_cast<ULONG_PTR>(&stop_)); |
| 134 | // Queuing the APC can fail if the thread is being terminated. |
| 135 | RTC_CHECK(queued || GetLastError() == ERROR_GEN_FAILURE); |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 136 | WaitForSingleObject(thread_, INFINITE); |
| 137 | CloseHandle(thread_); |
| 138 | thread_ = nullptr; |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 139 | thread_id_ = 0; |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 140 | #else |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 141 | if (!run_function_) |
| 142 | RTC_CHECK_EQ(1, AtomicOps::Increment(&stop_flag_)); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 143 | RTC_CHECK_EQ(0, pthread_join(thread_, nullptr)); |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 144 | if (!run_function_) |
| 145 | AtomicOps::ReleaseStore(&stop_flag_, 0); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 146 | thread_ = 0; |
| 147 | #endif // defined(WEBRTC_WIN) |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 148 | spawned_thread_checker_.DetachFromThread(); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 149 | } |
| 150 | |
tommi | 82ead60 | 2017-02-19 16:09:55 -0800 | [diff] [blame] | 151 | // TODO(tommi): Deprecate the loop behavior in PlatformThread. |
| 152 | // * Introduce a new callback type that returns void. |
| 153 | // * Remove potential for a busy loop in PlatformThread. |
| 154 | // * Delegate the responsibility for how to stop the thread, to the |
| 155 | // implementation that actually uses the thread. |
| 156 | // All implementations will need to be aware of how the thread should be stopped |
| 157 | // and encouraging a busy polling loop, can be costly in terms of power and cpu. |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 158 | void PlatformThread::Run() { |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 159 | // Attach the worker thread checker to this thread. |
| 160 | RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); |
| 161 | rtc::SetCurrentThreadName(name_.c_str()); |
| 162 | |
| 163 | if (run_function_) { |
| 164 | SetPriority(priority_); |
| 165 | run_function_(obj_); |
| 166 | return; |
| 167 | } |
tommi | 500f1b7 | 2017-03-02 07:07:09 -0800 | [diff] [blame] | 168 | |
| 169 | // TODO(tommi): Delete the rest of this function when looping isn't supported. |
| 170 | #if RTC_DCHECK_IS_ON |
| 171 | // These constants control the busy loop detection algorithm below. |
| 172 | // |kMaxLoopCount| controls the limit for how many times we allow the loop |
| 173 | // to run within a period, before DCHECKing. |
| 174 | // |kPeriodToMeasureMs| controls how long that period is. |
| 175 | static const int kMaxLoopCount = 1000; |
| 176 | static const int kPeriodToMeasureMs = 100; |
| 177 | int64_t loop_stamps[kMaxLoopCount] = {}; |
| 178 | int64_t sequence_nr = 0; |
danilchap | 2752347 | 2017-02-28 06:20:38 -0800 | [diff] [blame] | 179 | #endif |
tommi | 500f1b7 | 2017-03-02 07:07:09 -0800 | [diff] [blame] | 180 | |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 181 | do { |
sprang | e791ffd | 2016-01-26 01:53:20 -0800 | [diff] [blame] | 182 | // The interface contract of Start/Stop is that for a successful call to |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 183 | // Start, there should be at least one call to the run function. So we |
| 184 | // call the function before checking |stop_|. |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 185 | if (!run_function_deprecated_(obj_)) |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 186 | break; |
tommi | 500f1b7 | 2017-03-02 07:07:09 -0800 | [diff] [blame] | 187 | #if RTC_DCHECK_IS_ON |
| 188 | auto id = sequence_nr % kMaxLoopCount; |
| 189 | loop_stamps[id] = rtc::TimeMillis(); |
| 190 | if (sequence_nr > kMaxLoopCount) { |
| 191 | auto compare_id = (id + 1) % kMaxLoopCount; |
| 192 | auto diff = loop_stamps[id] - loop_stamps[compare_id]; |
| 193 | RTC_DCHECK_GE(diff, 0); |
| 194 | if (diff < kPeriodToMeasureMs) { |
| 195 | RTC_NOTREACHED() << "This thread is too busy: " << name_ << " " << diff |
| 196 | << "ms sequence=" << sequence_nr << " " |
| 197 | << loop_stamps[id] << " vs " << loop_stamps[compare_id] |
| 198 | << ", " << id << " vs " << compare_id; |
| 199 | } |
| 200 | } |
| 201 | ++sequence_nr; |
| 202 | #endif |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 203 | #if defined(WEBRTC_WIN) |
| 204 | // Alertable sleep to permit RaiseFlag to run and update |stop_|. |
| 205 | SleepEx(0, true); |
| 206 | } while (!stop_); |
| 207 | #else |
Yura Yaroshevich | 665d18e | 2018-01-23 17:10:29 +0300 | [diff] [blame] | 208 | #if defined(WEBRTC_MAC) || defined(WEBRTC_ANDROID) |
tommi | 0473b1d | 2017-03-02 08:08:59 -0800 | [diff] [blame] | 209 | sched_yield(); |
| 210 | #else |
tommi | 500f1b7 | 2017-03-02 07:07:09 -0800 | [diff] [blame] | 211 | static const struct timespec ts_null = {0}; |
danilchap | 2752347 | 2017-02-28 06:20:38 -0800 | [diff] [blame] | 212 | nanosleep(&ts_null, nullptr); |
| 213 | #endif |
tommi | 82ead60 | 2017-02-19 16:09:55 -0800 | [diff] [blame] | 214 | } while (!AtomicOps::AcquireLoad(&stop_flag_)); |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 215 | #endif // defined(WEBRTC_WIN) |
| 216 | } |
| 217 | |
| 218 | bool PlatformThread::SetPriority(ThreadPriority priority) { |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 219 | #if RTC_DCHECK_IS_ON |
| 220 | if (run_function_) { |
| 221 | // The non-deprecated way of how this function gets called, is that it must |
| 222 | // be called on the worker thread itself. |
tommi | 7c3da27 | 2017-03-20 03:47:17 -0700 | [diff] [blame] | 223 | RTC_DCHECK(!thread_checker_.CalledOnValidThread()); |
tommi | 0f8b403 | 2017-02-22 11:22:05 -0800 | [diff] [blame] | 224 | RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread()); |
| 225 | } else { |
| 226 | // In the case of deprecated use of this method, it must be called on the |
| 227 | // same thread as the PlatformThread object is constructed on. |
| 228 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 229 | RTC_DCHECK(IsRunning()); |
| 230 | } |
| 231 | #endif |
| 232 | |
Peter Boström | c661213 | 2015-11-24 18:10:24 +0100 | [diff] [blame] | 233 | #if defined(WEBRTC_WIN) |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 234 | return SetThreadPriority(thread_, priority) != FALSE; |
Wez | 0614ed9 | 2018-02-06 13:38:21 -0800 | [diff] [blame] | 235 | #elif defined(__native_client__) || defined(WEBRTC_FUCHSIA) |
| 236 | // Setting thread priorities is not supported in NaCl or Fuchsia. |
Peter Boström | c661213 | 2015-11-24 18:10:24 +0100 | [diff] [blame] | 237 | return true; |
| 238 | #elif defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX) |
| 239 | // TODO(tommi): Switch to the same mechanism as Chromium uses for changing |
| 240 | // thread priorities. |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 241 | return true; |
| 242 | #else |
| 243 | #ifdef WEBRTC_THREAD_RR |
| 244 | const int policy = SCHED_RR; |
| 245 | #else |
| 246 | const int policy = SCHED_FIFO; |
| 247 | #endif |
| 248 | const int min_prio = sched_get_priority_min(policy); |
| 249 | const int max_prio = sched_get_priority_max(policy); |
| 250 | if (min_prio == -1 || max_prio == -1) { |
| 251 | return false; |
| 252 | } |
| 253 | |
| 254 | if (max_prio - min_prio <= 2) |
| 255 | return false; |
| 256 | |
Peter Boström | 97c821d | 2015-11-24 13:48:13 +0100 | [diff] [blame] | 257 | // Convert webrtc priority to system priorities: |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 258 | sched_param param; |
Peter Boström | 97c821d | 2015-11-24 13:48:13 +0100 | [diff] [blame] | 259 | const int top_prio = max_prio - 1; |
| 260 | const int low_prio = min_prio + 1; |
| 261 | switch (priority) { |
| 262 | case kLowPriority: |
| 263 | param.sched_priority = low_prio; |
| 264 | break; |
| 265 | case kNormalPriority: |
| 266 | // The -1 ensures that the kHighPriority is always greater or equal to |
| 267 | // kNormalPriority. |
| 268 | param.sched_priority = (low_prio + top_prio - 1) / 2; |
| 269 | break; |
| 270 | case kHighPriority: |
| 271 | param.sched_priority = std::max(top_prio - 2, low_prio); |
| 272 | break; |
| 273 | case kHighestPriority: |
| 274 | param.sched_priority = std::max(top_prio - 1, low_prio); |
| 275 | break; |
| 276 | case kRealtimePriority: |
| 277 | param.sched_priority = top_prio; |
| 278 | break; |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 279 | } |
Peter Boström | 97c821d | 2015-11-24 13:48:13 +0100 | [diff] [blame] | 280 | return pthread_setschedparam(thread_, policy, ¶m) == 0; |
pbos | 12411ef | 2015-11-23 14:47:56 -0800 | [diff] [blame] | 281 | #endif // defined(WEBRTC_WIN) |
| 282 | } |
| 283 | |
tommi | 845afa8 | 2016-04-22 09:08:44 -0700 | [diff] [blame] | 284 | #if defined(WEBRTC_WIN) |
| 285 | bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) { |
| 286 | RTC_DCHECK(thread_checker_.CalledOnValidThread()); |
| 287 | RTC_DCHECK(IsRunning()); |
| 288 | |
| 289 | return QueueUserAPC(function, thread_, data) != FALSE; |
| 290 | } |
| 291 | #endif |
| 292 | |
Peter Boström | 8c38e8b | 2015-11-26 17:45:47 +0100 | [diff] [blame] | 293 | } // namespace rtc |