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