blob: 58beed379d5c8b4bd29c4ce503564578fd345de3 [file] [log] [blame]
Tommibebc6902015-05-18 09:51:42 +02001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/platform_thread.h"
Tommibebc6902015-05-18 09:51:42 +020012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "rtc_base/atomicops.h"
14#include "rtc_base/checks.h"
15#include "rtc_base/timeutils.h"
16#include "rtc_base/trace_event.h"
Tommibebc6902015-05-18 09:51:42 +020017
18#if defined(WEBRTC_LINUX)
Tommiea14f0a2015-05-18 13:51:06 +020019#include <sys/prctl.h>
Tommibebc6902015-05-18 09:51:42 +020020#include <sys/syscall.h>
21#endif
22
23namespace rtc {
24
25PlatformThreadId CurrentThreadId() {
26 PlatformThreadId ret;
27#if defined(WEBRTC_WIN)
28 ret = GetCurrentThreadId();
29#elif defined(WEBRTC_POSIX)
30#if defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
31 ret = pthread_mach_thread_np(pthread_self());
Tommibebc6902015-05-18 09:51:42 +020032#elif defined(WEBRTC_ANDROID)
33 ret = gettid();
magjedd950d9e2017-08-24 06:41:05 -070034#elif defined(WEBRTC_LINUX)
35 ret = syscall(__NR_gettid);
Tommibebc6902015-05-18 09:51:42 +020036#else
37 // Default implementation for nacl and solaris.
38 ret = reinterpret_cast<pid_t>(pthread_self());
39#endif
40#endif // defined(WEBRTC_POSIX)
henrikg91d6ede2015-09-17 00:24:34 -070041 RTC_DCHECK(ret);
Tommibebc6902015-05-18 09:51:42 +020042 return ret;
43}
44
45PlatformThreadRef CurrentThreadRef() {
46#if defined(WEBRTC_WIN)
47 return GetCurrentThreadId();
48#elif defined(WEBRTC_POSIX)
49 return pthread_self();
50#endif
51}
52
53bool IsThreadRefEqual(const PlatformThreadRef& a, const PlatformThreadRef& b) {
54#if defined(WEBRTC_WIN)
55 return a == b;
56#elif defined(WEBRTC_POSIX)
57 return pthread_equal(a, b);
58#endif
59}
60
Tommiea14f0a2015-05-18 13:51:06 +020061void SetCurrentThreadName(const char* name) {
Tommiea14f0a2015-05-18 13:51:06 +020062#if defined(WEBRTC_WIN)
63 struct {
64 DWORD dwType;
65 LPCSTR szName;
66 DWORD dwThreadID;
67 DWORD dwFlags;
68 } threadname_info = {0x1000, name, static_cast<DWORD>(-1), 0};
69
70 __try {
71 ::RaiseException(0x406D1388, 0, sizeof(threadname_info) / sizeof(DWORD),
72 reinterpret_cast<ULONG_PTR*>(&threadname_info));
73 } __except (EXCEPTION_EXECUTE_HANDLER) {
74 }
75#elif defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID)
76 prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(name));
77#elif defined(WEBRTC_MAC) || defined(WEBRTC_IOS)
78 pthread_setname_np(name);
79#endif
80}
81
pbos12411ef2015-11-23 14:47:56 -080082namespace {
83#if defined(WEBRTC_WIN)
84void CALLBACK RaiseFlag(ULONG_PTR param) {
85 *reinterpret_cast<bool*>(param) = true;
86}
87#else
88struct ThreadAttributes {
89 ThreadAttributes() { pthread_attr_init(&attr); }
90 ~ThreadAttributes() { pthread_attr_destroy(&attr); }
91 pthread_attr_t* operator&() { return &attr; }
92 pthread_attr_t attr;
93};
pbos12411ef2015-11-23 14:47:56 -080094#endif // defined(WEBRTC_WIN)
95}
96
tommi0f8b4032017-02-22 11:22:05 -080097PlatformThread::PlatformThread(ThreadRunFunctionDeprecated func,
pbos12411ef2015-11-23 14:47:56 -080098 void* obj,
99 const char* thread_name)
tommi0f8b4032017-02-22 11:22:05 -0800100 : run_function_deprecated_(func),
pbos12411ef2015-11-23 14:47:56 -0800101 obj_(obj),
tommi82ead602017-02-19 16:09:55 -0800102 name_(thread_name ? thread_name : "webrtc") {
pbos12411ef2015-11-23 14:47:56 -0800103 RTC_DCHECK(func);
104 RTC_DCHECK(name_.length() < 64);
tommi0f8b4032017-02-22 11:22:05 -0800105 spawned_thread_checker_.DetachFromThread();
106}
107
108PlatformThread::PlatformThread(ThreadRunFunction func,
109 void* obj,
110 const char* thread_name,
111 ThreadPriority priority /*= kNormalPriority*/)
112 : run_function_(func), priority_(priority), obj_(obj), name_(thread_name) {
113 RTC_DCHECK(func);
114 RTC_DCHECK(!name_.empty());
115 // TODO(tommi): Consider lowering the limit to 15 (limit on Linux).
116 RTC_DCHECK(name_.length() < 64);
117 spawned_thread_checker_.DetachFromThread();
pbos12411ef2015-11-23 14:47:56 -0800118}
119
120PlatformThread::~PlatformThread() {
121 RTC_DCHECK(thread_checker_.CalledOnValidThread());
122#if defined(WEBRTC_WIN)
123 RTC_DCHECK(!thread_);
tommi845afa82016-04-22 09:08:44 -0700124 RTC_DCHECK(!thread_id_);
pbos12411ef2015-11-23 14:47:56 -0800125#endif // defined(WEBRTC_WIN)
126}
127
128#if defined(WEBRTC_WIN)
129DWORD WINAPI PlatformThread::StartThread(void* param) {
perkj6a2e20a2016-11-30 04:53:08 -0800130 // The GetLastError() function only returns valid results when it is called
131 // after a Win32 API function that returns a "failed" result. A crash dump
132 // contains the result from GetLastError() and to make sure it does not
133 // falsely report a Windows error we call SetLastError here.
134 ::SetLastError(ERROR_SUCCESS);
pbos12411ef2015-11-23 14:47:56 -0800135 static_cast<PlatformThread*>(param)->Run();
136 return 0;
137}
138#else
139void* PlatformThread::StartThread(void* param) {
140 static_cast<PlatformThread*>(param)->Run();
141 return 0;
142}
143#endif // defined(WEBRTC_WIN)
144
Peter Boström8c38e8b2015-11-26 17:45:47 +0100145void PlatformThread::Start() {
pbos12411ef2015-11-23 14:47:56 -0800146 RTC_DCHECK(thread_checker_.CalledOnValidThread());
147 RTC_DCHECK(!thread_) << "Thread already started?";
148#if defined(WEBRTC_WIN)
149 stop_ = false;
150
151 // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION.
152 // Set the reserved stack stack size to 1M, which is the default on Windows
153 // and Linux.
deadbeef37f5ecf2017-02-27 14:06:41 -0800154 thread_ = ::CreateThread(nullptr, 1024 * 1024, &StartThread, this,
tommi845afa82016-04-22 09:08:44 -0700155 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id_);
pbos12411ef2015-11-23 14:47:56 -0800156 RTC_CHECK(thread_) << "CreateThread failed";
tommi845afa82016-04-22 09:08:44 -0700157 RTC_DCHECK(thread_id_);
pbos12411ef2015-11-23 14:47:56 -0800158#else
159 ThreadAttributes attr;
160 // Set the stack stack size to 1M.
161 pthread_attr_setstacksize(&attr, 1024 * 1024);
162 RTC_CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this));
163#endif // defined(WEBRTC_WIN)
pbos12411ef2015-11-23 14:47:56 -0800164}
165
Peter Boström8c38e8b2015-11-26 17:45:47 +0100166bool PlatformThread::IsRunning() const {
pbos12411ef2015-11-23 14:47:56 -0800167 RTC_DCHECK(thread_checker_.CalledOnValidThread());
168#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100169 return thread_ != nullptr;
pbos12411ef2015-11-23 14:47:56 -0800170#else
Peter Boström8c38e8b2015-11-26 17:45:47 +0100171 return thread_ != 0;
172#endif // defined(WEBRTC_WIN)
173}
pbos12411ef2015-11-23 14:47:56 -0800174
tommi845afa82016-04-22 09:08:44 -0700175PlatformThreadRef PlatformThread::GetThreadRef() const {
176#if defined(WEBRTC_WIN)
177 return thread_id_;
178#else
179 return thread_;
180#endif // defined(WEBRTC_WIN)
181}
182
Peter Boström8c38e8b2015-11-26 17:45:47 +0100183void PlatformThread::Stop() {
184 RTC_DCHECK(thread_checker_.CalledOnValidThread());
185 if (!IsRunning())
186 return;
187
188#if defined(WEBRTC_WIN)
189 // Set stop_ to |true| on the worker thread.
tommi845afa82016-04-22 09:08:44 -0700190 bool queued = QueueAPC(&RaiseFlag, reinterpret_cast<ULONG_PTR>(&stop_));
191 // Queuing the APC can fail if the thread is being terminated.
192 RTC_CHECK(queued || GetLastError() == ERROR_GEN_FAILURE);
Peter Boström8c38e8b2015-11-26 17:45:47 +0100193 WaitForSingleObject(thread_, INFINITE);
194 CloseHandle(thread_);
195 thread_ = nullptr;
tommi845afa82016-04-22 09:08:44 -0700196 thread_id_ = 0;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100197#else
tommi0f8b4032017-02-22 11:22:05 -0800198 if (!run_function_)
199 RTC_CHECK_EQ(1, AtomicOps::Increment(&stop_flag_));
pbos12411ef2015-11-23 14:47:56 -0800200 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr));
tommi0f8b4032017-02-22 11:22:05 -0800201 if (!run_function_)
202 AtomicOps::ReleaseStore(&stop_flag_, 0);
pbos12411ef2015-11-23 14:47:56 -0800203 thread_ = 0;
204#endif // defined(WEBRTC_WIN)
tommi0f8b4032017-02-22 11:22:05 -0800205 spawned_thread_checker_.DetachFromThread();
pbos12411ef2015-11-23 14:47:56 -0800206}
207
tommi82ead602017-02-19 16:09:55 -0800208// TODO(tommi): Deprecate the loop behavior in PlatformThread.
209// * Introduce a new callback type that returns void.
210// * Remove potential for a busy loop in PlatformThread.
211// * Delegate the responsibility for how to stop the thread, to the
212// implementation that actually uses the thread.
213// All implementations will need to be aware of how the thread should be stopped
214// and encouraging a busy polling loop, can be costly in terms of power and cpu.
pbos12411ef2015-11-23 14:47:56 -0800215void PlatformThread::Run() {
tommi0f8b4032017-02-22 11:22:05 -0800216 // Attach the worker thread checker to this thread.
217 RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread());
218 rtc::SetCurrentThreadName(name_.c_str());
219
220 if (run_function_) {
221 SetPriority(priority_);
222 run_function_(obj_);
223 return;
224 }
tommi500f1b72017-03-02 07:07:09 -0800225
226// TODO(tommi): Delete the rest of this function when looping isn't supported.
227#if RTC_DCHECK_IS_ON
228 // These constants control the busy loop detection algorithm below.
229 // |kMaxLoopCount| controls the limit for how many times we allow the loop
230 // to run within a period, before DCHECKing.
231 // |kPeriodToMeasureMs| controls how long that period is.
232 static const int kMaxLoopCount = 1000;
233 static const int kPeriodToMeasureMs = 100;
234 int64_t loop_stamps[kMaxLoopCount] = {};
235 int64_t sequence_nr = 0;
danilchap27523472017-02-28 06:20:38 -0800236#endif
tommi500f1b72017-03-02 07:07:09 -0800237
pbos12411ef2015-11-23 14:47:56 -0800238 do {
tommidb23ea62017-03-03 07:21:18 -0800239 TRACE_EVENT1("webrtc", "PlatformThread::Run", "name", name_.c_str());
240
sprange791ffd2016-01-26 01:53:20 -0800241 // The interface contract of Start/Stop is that for a successful call to
pbos12411ef2015-11-23 14:47:56 -0800242 // Start, there should be at least one call to the run function. So we
243 // call the function before checking |stop_|.
tommi0f8b4032017-02-22 11:22:05 -0800244 if (!run_function_deprecated_(obj_))
pbos12411ef2015-11-23 14:47:56 -0800245 break;
tommi500f1b72017-03-02 07:07:09 -0800246#if RTC_DCHECK_IS_ON
247 auto id = sequence_nr % kMaxLoopCount;
248 loop_stamps[id] = rtc::TimeMillis();
249 if (sequence_nr > kMaxLoopCount) {
250 auto compare_id = (id + 1) % kMaxLoopCount;
251 auto diff = loop_stamps[id] - loop_stamps[compare_id];
252 RTC_DCHECK_GE(diff, 0);
253 if (diff < kPeriodToMeasureMs) {
254 RTC_NOTREACHED() << "This thread is too busy: " << name_ << " " << diff
255 << "ms sequence=" << sequence_nr << " "
256 << loop_stamps[id] << " vs " << loop_stamps[compare_id]
257 << ", " << id << " vs " << compare_id;
258 }
259 }
260 ++sequence_nr;
261#endif
pbos12411ef2015-11-23 14:47:56 -0800262#if defined(WEBRTC_WIN)
263 // Alertable sleep to permit RaiseFlag to run and update |stop_|.
264 SleepEx(0, true);
265 } while (!stop_);
266#else
tommi0473b1d2017-03-02 08:08:59 -0800267#if defined(WEBRTC_MAC)
268 sched_yield();
269#else
tommi500f1b72017-03-02 07:07:09 -0800270 static const struct timespec ts_null = {0};
danilchap27523472017-02-28 06:20:38 -0800271 nanosleep(&ts_null, nullptr);
272#endif
tommi82ead602017-02-19 16:09:55 -0800273 } while (!AtomicOps::AcquireLoad(&stop_flag_));
pbos12411ef2015-11-23 14:47:56 -0800274#endif // defined(WEBRTC_WIN)
275}
276
277bool PlatformThread::SetPriority(ThreadPriority priority) {
tommi0f8b4032017-02-22 11:22:05 -0800278#if RTC_DCHECK_IS_ON
279 if (run_function_) {
280 // The non-deprecated way of how this function gets called, is that it must
281 // be called on the worker thread itself.
tommi7c3da272017-03-20 03:47:17 -0700282 RTC_DCHECK(!thread_checker_.CalledOnValidThread());
tommi0f8b4032017-02-22 11:22:05 -0800283 RTC_DCHECK(spawned_thread_checker_.CalledOnValidThread());
284 } else {
285 // In the case of deprecated use of this method, it must be called on the
286 // same thread as the PlatformThread object is constructed on.
287 RTC_DCHECK(thread_checker_.CalledOnValidThread());
288 RTC_DCHECK(IsRunning());
289 }
290#endif
291
Peter Boströmc6612132015-11-24 18:10:24 +0100292#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100293 return SetThreadPriority(thread_, priority) != FALSE;
Peter Boströmc6612132015-11-24 18:10:24 +0100294#elif defined(__native_client__)
295 // Setting thread priorities is not supported in NaCl.
296 return true;
297#elif defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX)
298 // TODO(tommi): Switch to the same mechanism as Chromium uses for changing
299 // thread priorities.
pbos12411ef2015-11-23 14:47:56 -0800300 return true;
301#else
302#ifdef WEBRTC_THREAD_RR
303 const int policy = SCHED_RR;
304#else
305 const int policy = SCHED_FIFO;
306#endif
307 const int min_prio = sched_get_priority_min(policy);
308 const int max_prio = sched_get_priority_max(policy);
309 if (min_prio == -1 || max_prio == -1) {
310 return false;
311 }
312
313 if (max_prio - min_prio <= 2)
314 return false;
315
Peter Boström97c821d2015-11-24 13:48:13 +0100316 // Convert webrtc priority to system priorities:
pbos12411ef2015-11-23 14:47:56 -0800317 sched_param param;
Peter Boström97c821d2015-11-24 13:48:13 +0100318 const int top_prio = max_prio - 1;
319 const int low_prio = min_prio + 1;
320 switch (priority) {
321 case kLowPriority:
322 param.sched_priority = low_prio;
323 break;
324 case kNormalPriority:
325 // The -1 ensures that the kHighPriority is always greater or equal to
326 // kNormalPriority.
327 param.sched_priority = (low_prio + top_prio - 1) / 2;
328 break;
329 case kHighPriority:
330 param.sched_priority = std::max(top_prio - 2, low_prio);
331 break;
332 case kHighestPriority:
333 param.sched_priority = std::max(top_prio - 1, low_prio);
334 break;
335 case kRealtimePriority:
336 param.sched_priority = top_prio;
337 break;
pbos12411ef2015-11-23 14:47:56 -0800338 }
Peter Boström97c821d2015-11-24 13:48:13 +0100339 return pthread_setschedparam(thread_, policy, &param) == 0;
pbos12411ef2015-11-23 14:47:56 -0800340#endif // defined(WEBRTC_WIN)
341}
342
tommi845afa82016-04-22 09:08:44 -0700343#if defined(WEBRTC_WIN)
344bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) {
345 RTC_DCHECK(thread_checker_.CalledOnValidThread());
346 RTC_DCHECK(IsRunning());
347
348 return QueueUserAPC(function, thread_, data) != FALSE;
349}
350#endif
351
Peter Boström8c38e8b2015-11-26 17:45:47 +0100352} // namespace rtc