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