blob: 286bee95f1299823920ec03175962dbf61e9cba8 [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
11#include "webrtc/base/platform_thread.h"
12
13#include "webrtc/base/checks.h"
14
15#if defined(WEBRTC_LINUX)
Tommiea14f0a2015-05-18 13:51:06 +020016#include <sys/prctl.h>
Tommibebc6902015-05-18 09:51:42 +020017#include <sys/syscall.h>
18#endif
19
20namespace rtc {
21
22PlatformThreadId 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)
henrikg91d6ede2015-09-17 00:24:34 -070038 RTC_DCHECK(ret);
Tommibebc6902015-05-18 09:51:42 +020039 return ret;
40}
41
42PlatformThreadRef CurrentThreadRef() {
43#if defined(WEBRTC_WIN)
44 return GetCurrentThreadId();
45#elif defined(WEBRTC_POSIX)
46 return pthread_self();
47#endif
48}
49
50bool 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
Tommiea14f0a2015-05-18 13:51:06 +020058void SetCurrentThreadName(const char* name) {
Tommiea14f0a2015-05-18 13:51:06 +020059#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
pbos12411ef2015-11-23 14:47:56 -080079namespace {
80#if defined(WEBRTC_WIN)
81void CALLBACK RaiseFlag(ULONG_PTR param) {
82 *reinterpret_cast<bool*>(param) = true;
83}
84#else
85struct 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};
pbos12411ef2015-11-23 14:47:56 -080091#endif // defined(WEBRTC_WIN)
92}
93
94PlatformThread::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),
tommi845afa82016-04-22 09:08:44 -0700102 thread_(NULL),
103 thread_id_(0) {
pbos12411ef2015-11-23 14:47:56 -0800104#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
112PlatformThread::~PlatformThread() {
113 RTC_DCHECK(thread_checker_.CalledOnValidThread());
114#if defined(WEBRTC_WIN)
115 RTC_DCHECK(!thread_);
tommi845afa82016-04-22 09:08:44 -0700116 RTC_DCHECK(!thread_id_);
pbos12411ef2015-11-23 14:47:56 -0800117#endif // defined(WEBRTC_WIN)
118}
119
120#if defined(WEBRTC_WIN)
121DWORD WINAPI PlatformThread::StartThread(void* param) {
122 static_cast<PlatformThread*>(param)->Run();
123 return 0;
124}
125#else
126void* PlatformThread::StartThread(void* param) {
127 static_cast<PlatformThread*>(param)->Run();
128 return 0;
129}
130#endif // defined(WEBRTC_WIN)
131
Peter Boström8c38e8b2015-11-26 17:45:47 +0100132void PlatformThread::Start() {
pbos12411ef2015-11-23 14:47:56 -0800133 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.
pbos12411ef2015-11-23 14:47:56 -0800141 thread_ = ::CreateThread(NULL, 1024 * 1024, &StartThread, this,
tommi845afa82016-04-22 09:08:44 -0700142 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id_);
pbos12411ef2015-11-23 14:47:56 -0800143 RTC_CHECK(thread_) << "CreateThread failed";
tommi845afa82016-04-22 09:08:44 -0700144 RTC_DCHECK(thread_id_);
pbos12411ef2015-11-23 14:47:56 -0800145#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)
pbos12411ef2015-11-23 14:47:56 -0800151}
152
Peter Boström8c38e8b2015-11-26 17:45:47 +0100153bool PlatformThread::IsRunning() const {
pbos12411ef2015-11-23 14:47:56 -0800154 RTC_DCHECK(thread_checker_.CalledOnValidThread());
155#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100156 return thread_ != nullptr;
pbos12411ef2015-11-23 14:47:56 -0800157#else
Peter Boström8c38e8b2015-11-26 17:45:47 +0100158 return thread_ != 0;
159#endif // defined(WEBRTC_WIN)
160}
pbos12411ef2015-11-23 14:47:56 -0800161
tommi845afa82016-04-22 09:08:44 -0700162PlatformThreadRef 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öm8c38e8b2015-11-26 17:45:47 +0100170void 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.
tommi845afa82016-04-22 09:08:44 -0700177 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öm8c38e8b2015-11-26 17:45:47 +0100180 WaitForSingleObject(thread_, INFINITE);
181 CloseHandle(thread_);
182 thread_ = nullptr;
tommi845afa82016-04-22 09:08:44 -0700183 thread_id_ = 0;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100184#else
pbos12411ef2015-11-23 14:47:56 -0800185 stop_event_.Set();
186 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr));
187 thread_ = 0;
188#endif // defined(WEBRTC_WIN)
pbos12411ef2015-11-23 14:47:56 -0800189}
190
191void PlatformThread::Run() {
192 if (!name_.empty())
193 rtc::SetCurrentThreadName(name_.c_str());
194 do {
sprange791ffd2016-01-26 01:53:20 -0800195 // The interface contract of Start/Stop is that for a successful call to
pbos12411ef2015-11-23 14:47:56 -0800196 // 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
209bool PlatformThread::SetPriority(ThreadPriority priority) {
210 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Peter Boström8c38e8b2015-11-26 17:45:47 +0100211 RTC_DCHECK(IsRunning());
Peter Boströmc6612132015-11-24 18:10:24 +0100212#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100213 return SetThreadPriority(thread_, priority) != FALSE;
Peter Boströmc6612132015-11-24 18:10:24 +0100214#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.
pbos12411ef2015-11-23 14:47:56 -0800220 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öm97c821d2015-11-24 13:48:13 +0100236 // Convert webrtc priority to system priorities:
pbos12411ef2015-11-23 14:47:56 -0800237 sched_param param;
Peter Boström97c821d2015-11-24 13:48:13 +0100238 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;
pbos12411ef2015-11-23 14:47:56 -0800258 }
Peter Boström97c821d2015-11-24 13:48:13 +0100259 return pthread_setschedparam(thread_, policy, &param) == 0;
pbos12411ef2015-11-23 14:47:56 -0800260#endif // defined(WEBRTC_WIN)
261}
262
tommi845afa82016-04-22 09:08:44 -0700263#if defined(WEBRTC_WIN)
264bool 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öm8c38e8b2015-11-26 17:45:47 +0100272} // namespace rtc