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 "rtc_base/platform_thread.h"
12
13 #if !defined(WEBRTC_WIN)
14 #include <sched.h>
15 #endif
16 #include <stdint.h>
17 #include <time.h>
18
19 #include <algorithm>
20
21 #include "rtc_base/checks.h"
22
23 namespace rtc {
24 namespace {
25 #if !defined(WEBRTC_WIN)
26 struct ThreadAttributes {
ThreadAttributesrtc::__anon5fc841900111::ThreadAttributes27 ThreadAttributes() { pthread_attr_init(&attr); }
~ThreadAttributesrtc::__anon5fc841900111::ThreadAttributes28 ~ThreadAttributes() { pthread_attr_destroy(&attr); }
operator &rtc::__anon5fc841900111::ThreadAttributes29 pthread_attr_t* operator&() { return &attr; }
30 pthread_attr_t attr;
31 };
32 #endif // defined(WEBRTC_WIN)
33 } // namespace
34
PlatformThread(ThreadRunFunction func,void * obj,absl::string_view thread_name,ThreadPriority priority)35 PlatformThread::PlatformThread(ThreadRunFunction func,
36 void* obj,
37 absl::string_view thread_name,
38 ThreadPriority priority /*= kNormalPriority*/)
39 : run_function_(func), priority_(priority), obj_(obj), name_(thread_name) {
40 RTC_DCHECK(func);
41 RTC_DCHECK(!name_.empty());
42 // TODO(tommi): Consider lowering the limit to 15 (limit on Linux).
43 RTC_DCHECK(name_.length() < 64);
44 spawned_thread_checker_.Detach();
45 }
46
~PlatformThread()47 PlatformThread::~PlatformThread() {
48 RTC_DCHECK(thread_checker_.IsCurrent());
49 #if defined(WEBRTC_WIN)
50 RTC_DCHECK(!thread_);
51 RTC_DCHECK(!thread_id_);
52 #endif // defined(WEBRTC_WIN)
53 }
54
55 #if defined(WEBRTC_WIN)
StartThread(void * param)56 DWORD WINAPI PlatformThread::StartThread(void* param) {
57 // The GetLastError() function only returns valid results when it is called
58 // after a Win32 API function that returns a "failed" result. A crash dump
59 // contains the result from GetLastError() and to make sure it does not
60 // falsely report a Windows error we call SetLastError here.
61 ::SetLastError(ERROR_SUCCESS);
62 static_cast<PlatformThread*>(param)->Run();
63 return 0;
64 }
65 #else
StartThread(void * param)66 void* PlatformThread::StartThread(void* param) {
67 static_cast<PlatformThread*>(param)->Run();
68 return 0;
69 }
70 #endif // defined(WEBRTC_WIN)
71
Start()72 void PlatformThread::Start() {
73 RTC_DCHECK(thread_checker_.IsCurrent());
74 RTC_DCHECK(!thread_) << "Thread already started?";
75 #if defined(WEBRTC_WIN)
76 // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION.
77 // Set the reserved stack stack size to 1M, which is the default on Windows
78 // and Linux.
79 thread_ = ::CreateThread(nullptr, 1024 * 1024, &StartThread, this,
80 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id_);
81 RTC_CHECK(thread_) << "CreateThread failed";
82 RTC_DCHECK(thread_id_);
83 #else
84 ThreadAttributes attr;
85 // Set the stack stack size to 1M.
86 pthread_attr_setstacksize(&attr, 1024 * 1024);
87 RTC_CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this));
88 #endif // defined(WEBRTC_WIN)
89 }
90
IsRunning() const91 bool PlatformThread::IsRunning() const {
92 RTC_DCHECK(thread_checker_.IsCurrent());
93 #if defined(WEBRTC_WIN)
94 return thread_ != nullptr;
95 #else
96 return thread_ != 0;
97 #endif // defined(WEBRTC_WIN)
98 }
99
GetThreadRef() const100 PlatformThreadRef PlatformThread::GetThreadRef() const {
101 #if defined(WEBRTC_WIN)
102 return thread_id_;
103 #else
104 return thread_;
105 #endif // defined(WEBRTC_WIN)
106 }
107
Stop()108 void PlatformThread::Stop() {
109 RTC_DCHECK(thread_checker_.IsCurrent());
110 if (!IsRunning())
111 return;
112
113 #if defined(WEBRTC_WIN)
114 WaitForSingleObject(thread_, INFINITE);
115 CloseHandle(thread_);
116 thread_ = nullptr;
117 thread_id_ = 0;
118 #else
119 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr));
120 thread_ = 0;
121 #endif // defined(WEBRTC_WIN)
122 spawned_thread_checker_.Detach();
123 }
124
Run()125 void PlatformThread::Run() {
126 // Attach the worker thread checker to this thread.
127 RTC_DCHECK(spawned_thread_checker_.IsCurrent());
128 rtc::SetCurrentThreadName(name_.c_str());
129 SetPriority(priority_);
130 run_function_(obj_);
131 }
132
SetPriority(ThreadPriority priority)133 bool PlatformThread::SetPriority(ThreadPriority priority) {
134 RTC_DCHECK(spawned_thread_checker_.IsCurrent());
135
136 #if defined(WEBRTC_WIN)
137 return SetThreadPriority(thread_, priority) != FALSE;
138 #elif defined(__native_client__) || defined(WEBRTC_FUCHSIA)
139 // Setting thread priorities is not supported in NaCl or Fuchsia.
140 return true;
141 #elif defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX)
142 // TODO(tommi): Switch to the same mechanism as Chromium uses for changing
143 // thread priorities.
144 return true;
145 #else
146 const int policy = SCHED_FIFO;
147 const int min_prio = sched_get_priority_min(policy);
148 const int max_prio = sched_get_priority_max(policy);
149 if (min_prio == -1 || max_prio == -1) {
150 return false;
151 }
152
153 if (max_prio - min_prio <= 2)
154 return false;
155
156 // Convert webrtc priority to system priorities:
157 sched_param param;
158 const int top_prio = max_prio - 1;
159 const int low_prio = min_prio + 1;
160 switch (priority) {
161 case kLowPriority:
162 param.sched_priority = low_prio;
163 break;
164 case kNormalPriority:
165 // The -1 ensures that the kHighPriority is always greater or equal to
166 // kNormalPriority.
167 param.sched_priority = (low_prio + top_prio - 1) / 2;
168 break;
169 case kHighPriority:
170 param.sched_priority = std::max(top_prio - 2, low_prio);
171 break;
172 case kHighestPriority:
173 param.sched_priority = std::max(top_prio - 1, low_prio);
174 break;
175 case kRealtimePriority:
176 param.sched_priority = top_prio;
177 break;
178 }
179 return pthread_setschedparam(thread_, policy, ¶m) == 0;
180 #endif // defined(WEBRTC_WIN)
181 }
182
183 #if defined(WEBRTC_WIN)
QueueAPC(PAPCFUNC function,ULONG_PTR data)184 bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) {
185 RTC_DCHECK(thread_checker_.IsCurrent());
186 RTC_DCHECK(IsRunning());
187
188 return QueueUserAPC(function, thread_, data) != FALSE;
189 }
190 #endif
191
192 } // namespace rtc
193