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