xref: /aosp_15_r20/external/webrtc/rtc_base/platform_thread.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/platform_thread.h"
12*d9f75844SAndroid Build Coastguard Worker 
13*d9f75844SAndroid Build Coastguard Worker #include <algorithm>
14*d9f75844SAndroid Build Coastguard Worker #include <memory>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #if !defined(WEBRTC_WIN)
17*d9f75844SAndroid Build Coastguard Worker #include <sched.h>
18*d9f75844SAndroid Build Coastguard Worker #endif
19*d9f75844SAndroid Build Coastguard Worker 
20*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/checks.h"
21*d9f75844SAndroid Build Coastguard Worker 
22*d9f75844SAndroid Build Coastguard Worker namespace rtc {
23*d9f75844SAndroid Build Coastguard Worker namespace {
24*d9f75844SAndroid Build Coastguard Worker 
25*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN)
Win32PriorityFromThreadPriority(ThreadPriority priority)26*d9f75844SAndroid Build Coastguard Worker int Win32PriorityFromThreadPriority(ThreadPriority priority) {
27*d9f75844SAndroid Build Coastguard Worker   switch (priority) {
28*d9f75844SAndroid Build Coastguard Worker     case ThreadPriority::kLow:
29*d9f75844SAndroid Build Coastguard Worker       return THREAD_PRIORITY_BELOW_NORMAL;
30*d9f75844SAndroid Build Coastguard Worker     case ThreadPriority::kNormal:
31*d9f75844SAndroid Build Coastguard Worker       return THREAD_PRIORITY_NORMAL;
32*d9f75844SAndroid Build Coastguard Worker     case ThreadPriority::kHigh:
33*d9f75844SAndroid Build Coastguard Worker       return THREAD_PRIORITY_ABOVE_NORMAL;
34*d9f75844SAndroid Build Coastguard Worker     case ThreadPriority::kRealtime:
35*d9f75844SAndroid Build Coastguard Worker       return THREAD_PRIORITY_TIME_CRITICAL;
36*d9f75844SAndroid Build Coastguard Worker   }
37*d9f75844SAndroid Build Coastguard Worker }
38*d9f75844SAndroid Build Coastguard Worker #endif
39*d9f75844SAndroid Build Coastguard Worker 
SetPriority(ThreadPriority priority)40*d9f75844SAndroid Build Coastguard Worker bool SetPriority(ThreadPriority priority) {
41*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN)
42*d9f75844SAndroid Build Coastguard Worker   return SetThreadPriority(GetCurrentThread(),
43*d9f75844SAndroid Build Coastguard Worker                            Win32PriorityFromThreadPriority(priority)) != FALSE;
44*d9f75844SAndroid Build Coastguard Worker #elif defined(__native_client__) || defined(WEBRTC_FUCHSIA)
45*d9f75844SAndroid Build Coastguard Worker   // Setting thread priorities is not supported in NaCl or Fuchsia.
46*d9f75844SAndroid Build Coastguard Worker   return true;
47*d9f75844SAndroid Build Coastguard Worker #elif defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX)
48*d9f75844SAndroid Build Coastguard Worker   // TODO(tommi): Switch to the same mechanism as Chromium uses for changing
49*d9f75844SAndroid Build Coastguard Worker   // thread priorities.
50*d9f75844SAndroid Build Coastguard Worker   return true;
51*d9f75844SAndroid Build Coastguard Worker #else
52*d9f75844SAndroid Build Coastguard Worker   const int policy = SCHED_FIFO;
53*d9f75844SAndroid Build Coastguard Worker   const int min_prio = sched_get_priority_min(policy);
54*d9f75844SAndroid Build Coastguard Worker   const int max_prio = sched_get_priority_max(policy);
55*d9f75844SAndroid Build Coastguard Worker   if (min_prio == -1 || max_prio == -1) {
56*d9f75844SAndroid Build Coastguard Worker     return false;
57*d9f75844SAndroid Build Coastguard Worker   }
58*d9f75844SAndroid Build Coastguard Worker 
59*d9f75844SAndroid Build Coastguard Worker   if (max_prio - min_prio <= 2)
60*d9f75844SAndroid Build Coastguard Worker     return false;
61*d9f75844SAndroid Build Coastguard Worker 
62*d9f75844SAndroid Build Coastguard Worker   // Convert webrtc priority to system priorities:
63*d9f75844SAndroid Build Coastguard Worker   sched_param param;
64*d9f75844SAndroid Build Coastguard Worker   const int top_prio = max_prio - 1;
65*d9f75844SAndroid Build Coastguard Worker   const int low_prio = min_prio + 1;
66*d9f75844SAndroid Build Coastguard Worker   switch (priority) {
67*d9f75844SAndroid Build Coastguard Worker     case ThreadPriority::kLow:
68*d9f75844SAndroid Build Coastguard Worker       param.sched_priority = low_prio;
69*d9f75844SAndroid Build Coastguard Worker       break;
70*d9f75844SAndroid Build Coastguard Worker     case ThreadPriority::kNormal:
71*d9f75844SAndroid Build Coastguard Worker       // The -1 ensures that the kHighPriority is always greater or equal to
72*d9f75844SAndroid Build Coastguard Worker       // kNormalPriority.
73*d9f75844SAndroid Build Coastguard Worker       param.sched_priority = (low_prio + top_prio - 1) / 2;
74*d9f75844SAndroid Build Coastguard Worker       break;
75*d9f75844SAndroid Build Coastguard Worker     case ThreadPriority::kHigh:
76*d9f75844SAndroid Build Coastguard Worker       param.sched_priority = std::max(top_prio - 2, low_prio);
77*d9f75844SAndroid Build Coastguard Worker       break;
78*d9f75844SAndroid Build Coastguard Worker     case ThreadPriority::kRealtime:
79*d9f75844SAndroid Build Coastguard Worker       param.sched_priority = top_prio;
80*d9f75844SAndroid Build Coastguard Worker       break;
81*d9f75844SAndroid Build Coastguard Worker   }
82*d9f75844SAndroid Build Coastguard Worker   return pthread_setschedparam(pthread_self(), policy, &param) == 0;
83*d9f75844SAndroid Build Coastguard Worker #endif  // defined(WEBRTC_WIN)
84*d9f75844SAndroid Build Coastguard Worker }
85*d9f75844SAndroid Build Coastguard Worker 
86*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN)
RunPlatformThread(void * param)87*d9f75844SAndroid Build Coastguard Worker DWORD WINAPI RunPlatformThread(void* param) {
88*d9f75844SAndroid Build Coastguard Worker   // The GetLastError() function only returns valid results when it is called
89*d9f75844SAndroid Build Coastguard Worker   // after a Win32 API function that returns a "failed" result. A crash dump
90*d9f75844SAndroid Build Coastguard Worker   // contains the result from GetLastError() and to make sure it does not
91*d9f75844SAndroid Build Coastguard Worker   // falsely report a Windows error we call SetLastError here.
92*d9f75844SAndroid Build Coastguard Worker   ::SetLastError(ERROR_SUCCESS);
93*d9f75844SAndroid Build Coastguard Worker   auto function = static_cast<std::function<void()>*>(param);
94*d9f75844SAndroid Build Coastguard Worker   (*function)();
95*d9f75844SAndroid Build Coastguard Worker   delete function;
96*d9f75844SAndroid Build Coastguard Worker   return 0;
97*d9f75844SAndroid Build Coastguard Worker }
98*d9f75844SAndroid Build Coastguard Worker #else
RunPlatformThread(void * param)99*d9f75844SAndroid Build Coastguard Worker void* RunPlatformThread(void* param) {
100*d9f75844SAndroid Build Coastguard Worker   auto function = static_cast<std::function<void()>*>(param);
101*d9f75844SAndroid Build Coastguard Worker   (*function)();
102*d9f75844SAndroid Build Coastguard Worker   delete function;
103*d9f75844SAndroid Build Coastguard Worker   return 0;
104*d9f75844SAndroid Build Coastguard Worker }
105*d9f75844SAndroid Build Coastguard Worker #endif  // defined(WEBRTC_WIN)
106*d9f75844SAndroid Build Coastguard Worker 
107*d9f75844SAndroid Build Coastguard Worker }  // namespace
108*d9f75844SAndroid Build Coastguard Worker 
PlatformThread(Handle handle,bool joinable)109*d9f75844SAndroid Build Coastguard Worker PlatformThread::PlatformThread(Handle handle, bool joinable)
110*d9f75844SAndroid Build Coastguard Worker     : handle_(handle), joinable_(joinable) {}
111*d9f75844SAndroid Build Coastguard Worker 
PlatformThread(PlatformThread && rhs)112*d9f75844SAndroid Build Coastguard Worker PlatformThread::PlatformThread(PlatformThread&& rhs)
113*d9f75844SAndroid Build Coastguard Worker     : handle_(rhs.handle_), joinable_(rhs.joinable_) {
114*d9f75844SAndroid Build Coastguard Worker   rhs.handle_ = absl::nullopt;
115*d9f75844SAndroid Build Coastguard Worker }
116*d9f75844SAndroid Build Coastguard Worker 
operator =(PlatformThread && rhs)117*d9f75844SAndroid Build Coastguard Worker PlatformThread& PlatformThread::operator=(PlatformThread&& rhs) {
118*d9f75844SAndroid Build Coastguard Worker   Finalize();
119*d9f75844SAndroid Build Coastguard Worker   handle_ = rhs.handle_;
120*d9f75844SAndroid Build Coastguard Worker   joinable_ = rhs.joinable_;
121*d9f75844SAndroid Build Coastguard Worker   rhs.handle_ = absl::nullopt;
122*d9f75844SAndroid Build Coastguard Worker   return *this;
123*d9f75844SAndroid Build Coastguard Worker }
124*d9f75844SAndroid Build Coastguard Worker 
~PlatformThread()125*d9f75844SAndroid Build Coastguard Worker PlatformThread::~PlatformThread() {
126*d9f75844SAndroid Build Coastguard Worker   Finalize();
127*d9f75844SAndroid Build Coastguard Worker }
128*d9f75844SAndroid Build Coastguard Worker 
SpawnJoinable(std::function<void ()> thread_function,absl::string_view name,ThreadAttributes attributes)129*d9f75844SAndroid Build Coastguard Worker PlatformThread PlatformThread::SpawnJoinable(
130*d9f75844SAndroid Build Coastguard Worker     std::function<void()> thread_function,
131*d9f75844SAndroid Build Coastguard Worker     absl::string_view name,
132*d9f75844SAndroid Build Coastguard Worker     ThreadAttributes attributes) {
133*d9f75844SAndroid Build Coastguard Worker   return SpawnThread(std::move(thread_function), name, attributes,
134*d9f75844SAndroid Build Coastguard Worker                      /*joinable=*/true);
135*d9f75844SAndroid Build Coastguard Worker }
136*d9f75844SAndroid Build Coastguard Worker 
SpawnDetached(std::function<void ()> thread_function,absl::string_view name,ThreadAttributes attributes)137*d9f75844SAndroid Build Coastguard Worker PlatformThread PlatformThread::SpawnDetached(
138*d9f75844SAndroid Build Coastguard Worker     std::function<void()> thread_function,
139*d9f75844SAndroid Build Coastguard Worker     absl::string_view name,
140*d9f75844SAndroid Build Coastguard Worker     ThreadAttributes attributes) {
141*d9f75844SAndroid Build Coastguard Worker   return SpawnThread(std::move(thread_function), name, attributes,
142*d9f75844SAndroid Build Coastguard Worker                      /*joinable=*/false);
143*d9f75844SAndroid Build Coastguard Worker }
144*d9f75844SAndroid Build Coastguard Worker 
GetHandle() const145*d9f75844SAndroid Build Coastguard Worker absl::optional<PlatformThread::Handle> PlatformThread::GetHandle() const {
146*d9f75844SAndroid Build Coastguard Worker   return handle_;
147*d9f75844SAndroid Build Coastguard Worker }
148*d9f75844SAndroid Build Coastguard Worker 
149*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN)
QueueAPC(PAPCFUNC function,ULONG_PTR data)150*d9f75844SAndroid Build Coastguard Worker bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) {
151*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(handle_.has_value());
152*d9f75844SAndroid Build Coastguard Worker   return handle_.has_value() ? QueueUserAPC(function, *handle_, data) != FALSE
153*d9f75844SAndroid Build Coastguard Worker                              : false;
154*d9f75844SAndroid Build Coastguard Worker }
155*d9f75844SAndroid Build Coastguard Worker #endif
156*d9f75844SAndroid Build Coastguard Worker 
Finalize()157*d9f75844SAndroid Build Coastguard Worker void PlatformThread::Finalize() {
158*d9f75844SAndroid Build Coastguard Worker   if (!handle_.has_value())
159*d9f75844SAndroid Build Coastguard Worker     return;
160*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN)
161*d9f75844SAndroid Build Coastguard Worker   if (joinable_)
162*d9f75844SAndroid Build Coastguard Worker     WaitForSingleObject(*handle_, INFINITE);
163*d9f75844SAndroid Build Coastguard Worker   CloseHandle(*handle_);
164*d9f75844SAndroid Build Coastguard Worker #else
165*d9f75844SAndroid Build Coastguard Worker   if (joinable_)
166*d9f75844SAndroid Build Coastguard Worker     RTC_CHECK_EQ(0, pthread_join(*handle_, nullptr));
167*d9f75844SAndroid Build Coastguard Worker #endif
168*d9f75844SAndroid Build Coastguard Worker   handle_ = absl::nullopt;
169*d9f75844SAndroid Build Coastguard Worker }
170*d9f75844SAndroid Build Coastguard Worker 
SpawnThread(std::function<void ()> thread_function,absl::string_view name,ThreadAttributes attributes,bool joinable)171*d9f75844SAndroid Build Coastguard Worker PlatformThread PlatformThread::SpawnThread(
172*d9f75844SAndroid Build Coastguard Worker     std::function<void()> thread_function,
173*d9f75844SAndroid Build Coastguard Worker     absl::string_view name,
174*d9f75844SAndroid Build Coastguard Worker     ThreadAttributes attributes,
175*d9f75844SAndroid Build Coastguard Worker     bool joinable) {
176*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(thread_function);
177*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(!name.empty());
178*d9f75844SAndroid Build Coastguard Worker   // TODO(tommi): Consider lowering the limit to 15 (limit on Linux).
179*d9f75844SAndroid Build Coastguard Worker   RTC_DCHECK(name.length() < 64);
180*d9f75844SAndroid Build Coastguard Worker   auto start_thread_function_ptr =
181*d9f75844SAndroid Build Coastguard Worker       new std::function<void()>([thread_function = std::move(thread_function),
182*d9f75844SAndroid Build Coastguard Worker                                  name = std::string(name), attributes] {
183*d9f75844SAndroid Build Coastguard Worker         rtc::SetCurrentThreadName(name.c_str());
184*d9f75844SAndroid Build Coastguard Worker         SetPriority(attributes.priority);
185*d9f75844SAndroid Build Coastguard Worker         thread_function();
186*d9f75844SAndroid Build Coastguard Worker       });
187*d9f75844SAndroid Build Coastguard Worker #if defined(WEBRTC_WIN)
188*d9f75844SAndroid Build Coastguard Worker   // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION.
189*d9f75844SAndroid Build Coastguard Worker   // Set the reserved stack stack size to 1M, which is the default on Windows
190*d9f75844SAndroid Build Coastguard Worker   // and Linux.
191*d9f75844SAndroid Build Coastguard Worker   DWORD thread_id = 0;
192*d9f75844SAndroid Build Coastguard Worker   PlatformThread::Handle handle = ::CreateThread(
193*d9f75844SAndroid Build Coastguard Worker       nullptr, 1024 * 1024, &RunPlatformThread, start_thread_function_ptr,
194*d9f75844SAndroid Build Coastguard Worker       STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id);
195*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK(handle) << "CreateThread failed";
196*d9f75844SAndroid Build Coastguard Worker #else
197*d9f75844SAndroid Build Coastguard Worker   pthread_attr_t attr;
198*d9f75844SAndroid Build Coastguard Worker   pthread_attr_init(&attr);
199*d9f75844SAndroid Build Coastguard Worker   // Set the stack stack size to 1M.
200*d9f75844SAndroid Build Coastguard Worker   pthread_attr_setstacksize(&attr, 1024 * 1024);
201*d9f75844SAndroid Build Coastguard Worker   pthread_attr_setdetachstate(
202*d9f75844SAndroid Build Coastguard Worker       &attr, joinable ? PTHREAD_CREATE_JOINABLE : PTHREAD_CREATE_DETACHED);
203*d9f75844SAndroid Build Coastguard Worker   PlatformThread::Handle handle;
204*d9f75844SAndroid Build Coastguard Worker   RTC_CHECK_EQ(0, pthread_create(&handle, &attr, &RunPlatformThread,
205*d9f75844SAndroid Build Coastguard Worker                                  start_thread_function_ptr));
206*d9f75844SAndroid Build Coastguard Worker   pthread_attr_destroy(&attr);
207*d9f75844SAndroid Build Coastguard Worker #endif  // defined(WEBRTC_WIN)
208*d9f75844SAndroid Build Coastguard Worker   return PlatformThread(handle, joinable);
209*d9f75844SAndroid Build Coastguard Worker }
210*d9f75844SAndroid Build Coastguard Worker 
211*d9f75844SAndroid Build Coastguard Worker }  // namespace rtc
212