blob: f2a1f00975a236296be3e89ccf4c4b9796de90a7 [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
Yves Gerey988cc082018-10-23 12:03:01 +020013#if !defined(WEBRTC_WIN)
14#include <sched.h>
15#endif
16#include <stdint.h>
17#include <time.h>
18#include <algorithm>
19
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "rtc_base/checks.h"
Tommibebc6902015-05-18 09:51:42 +020021
Tommibebc6902015-05-18 09:51:42 +020022namespace rtc {
pbos12411ef2015-11-23 14:47:56 -080023namespace {
24#if defined(WEBRTC_WIN)
25void CALLBACK RaiseFlag(ULONG_PTR param) {
26 *reinterpret_cast<bool*>(param) = true;
27}
28#else
29struct ThreadAttributes {
30 ThreadAttributes() { pthread_attr_init(&attr); }
31 ~ThreadAttributes() { pthread_attr_destroy(&attr); }
32 pthread_attr_t* operator&() { return &attr; }
33 pthread_attr_t attr;
34};
pbos12411ef2015-11-23 14:47:56 -080035#endif // defined(WEBRTC_WIN)
36}
37
tommi0f8b4032017-02-22 11:22:05 -080038PlatformThread::PlatformThread(ThreadRunFunction func,
39 void* obj,
Danil Chapovalov5a1a6db2019-01-17 19:55:46 +010040 absl::string_view thread_name,
tommi0f8b4032017-02-22 11:22:05 -080041 ThreadPriority priority /*= kNormalPriority*/)
42 : run_function_(func), priority_(priority), obj_(obj), name_(thread_name) {
43 RTC_DCHECK(func);
44 RTC_DCHECK(!name_.empty());
45 // TODO(tommi): Consider lowering the limit to 15 (limit on Linux).
46 RTC_DCHECK(name_.length() < 64);
Sebastian Janssonc01367d2019-04-08 15:20:44 +020047 spawned_thread_checker_.Detach();
pbos12411ef2015-11-23 14:47:56 -080048}
49
50PlatformThread::~PlatformThread() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020051 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080052#if defined(WEBRTC_WIN)
53 RTC_DCHECK(!thread_);
tommi845afa82016-04-22 09:08:44 -070054 RTC_DCHECK(!thread_id_);
pbos12411ef2015-11-23 14:47:56 -080055#endif // defined(WEBRTC_WIN)
56}
57
58#if defined(WEBRTC_WIN)
59DWORD WINAPI PlatformThread::StartThread(void* param) {
perkj6a2e20a2016-11-30 04:53:08 -080060 // The GetLastError() function only returns valid results when it is called
61 // after a Win32 API function that returns a "failed" result. A crash dump
62 // contains the result from GetLastError() and to make sure it does not
63 // falsely report a Windows error we call SetLastError here.
64 ::SetLastError(ERROR_SUCCESS);
pbos12411ef2015-11-23 14:47:56 -080065 static_cast<PlatformThread*>(param)->Run();
66 return 0;
67}
68#else
69void* PlatformThread::StartThread(void* param) {
70 static_cast<PlatformThread*>(param)->Run();
71 return 0;
72}
73#endif // defined(WEBRTC_WIN)
74
Peter Boström8c38e8b2015-11-26 17:45:47 +010075void PlatformThread::Start() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020076 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080077 RTC_DCHECK(!thread_) << "Thread already started?";
78#if defined(WEBRTC_WIN)
79 stop_ = false;
80
81 // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION.
82 // Set the reserved stack stack size to 1M, which is the default on Windows
83 // and Linux.
deadbeef37f5ecf2017-02-27 14:06:41 -080084 thread_ = ::CreateThread(nullptr, 1024 * 1024, &StartThread, this,
tommi845afa82016-04-22 09:08:44 -070085 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id_);
pbos12411ef2015-11-23 14:47:56 -080086 RTC_CHECK(thread_) << "CreateThread failed";
tommi845afa82016-04-22 09:08:44 -070087 RTC_DCHECK(thread_id_);
pbos12411ef2015-11-23 14:47:56 -080088#else
89 ThreadAttributes attr;
90 // Set the stack stack size to 1M.
91 pthread_attr_setstacksize(&attr, 1024 * 1024);
92 RTC_CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this));
93#endif // defined(WEBRTC_WIN)
pbos12411ef2015-11-23 14:47:56 -080094}
95
Peter Boström8c38e8b2015-11-26 17:45:47 +010096bool PlatformThread::IsRunning() const {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020097 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080098#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +010099 return thread_ != nullptr;
pbos12411ef2015-11-23 14:47:56 -0800100#else
Peter Boström8c38e8b2015-11-26 17:45:47 +0100101 return thread_ != 0;
102#endif // defined(WEBRTC_WIN)
103}
pbos12411ef2015-11-23 14:47:56 -0800104
tommi845afa82016-04-22 09:08:44 -0700105PlatformThreadRef PlatformThread::GetThreadRef() const {
106#if defined(WEBRTC_WIN)
107 return thread_id_;
108#else
109 return thread_;
110#endif // defined(WEBRTC_WIN)
111}
112
Peter Boström8c38e8b2015-11-26 17:45:47 +0100113void PlatformThread::Stop() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200114 RTC_DCHECK(thread_checker_.IsCurrent());
Peter Boström8c38e8b2015-11-26 17:45:47 +0100115 if (!IsRunning())
116 return;
117
118#if defined(WEBRTC_WIN)
119 // Set stop_ to |true| on the worker thread.
tommi845afa82016-04-22 09:08:44 -0700120 bool queued = QueueAPC(&RaiseFlag, reinterpret_cast<ULONG_PTR>(&stop_));
Noah Richardsdbfb58b2019-04-02 16:18:57 -0700121 if (!queued) {
122 // Queuing the APC can fail if the thread is being terminated. This should
123 // return ERROR_GEN_FAILURE, though Wine returns ERROR_ACCESS_DENIED, so
124 // allow for either.
125 auto error = ::GetLastError();
126 if (error != ERROR_GEN_FAILURE && error != ERROR_ACCESS_DENIED) {
127 RTC_CHECK(false) << "Failed to QueueUserAPC, error: " << error;
128 }
129 }
Peter Boström8c38e8b2015-11-26 17:45:47 +0100130 WaitForSingleObject(thread_, INFINITE);
131 CloseHandle(thread_);
132 thread_ = nullptr;
tommi845afa82016-04-22 09:08:44 -0700133 thread_id_ = 0;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100134#else
pbos12411ef2015-11-23 14:47:56 -0800135 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr));
136 thread_ = 0;
137#endif // defined(WEBRTC_WIN)
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200138 spawned_thread_checker_.Detach();
pbos12411ef2015-11-23 14:47:56 -0800139}
140
141void PlatformThread::Run() {
tommi0f8b4032017-02-22 11:22:05 -0800142 // Attach the worker thread checker to this thread.
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200143 RTC_DCHECK(spawned_thread_checker_.IsCurrent());
tommi0f8b4032017-02-22 11:22:05 -0800144 rtc::SetCurrentThreadName(name_.c_str());
Niels Möller4731f002019-05-03 09:34:24 +0200145 SetPriority(priority_);
146 run_function_(obj_);
pbos12411ef2015-11-23 14:47:56 -0800147}
148
149bool PlatformThread::SetPriority(ThreadPriority priority) {
Niels Möller4731f002019-05-03 09:34:24 +0200150 RTC_DCHECK(spawned_thread_checker_.IsCurrent());
tommi0f8b4032017-02-22 11:22:05 -0800151
Peter Boströmc6612132015-11-24 18:10:24 +0100152#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100153 return SetThreadPriority(thread_, priority) != FALSE;
Wez0614ed92018-02-06 13:38:21 -0800154#elif defined(__native_client__) || defined(WEBRTC_FUCHSIA)
155 // Setting thread priorities is not supported in NaCl or Fuchsia.
Peter Boströmc6612132015-11-24 18:10:24 +0100156 return true;
157#elif defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX)
158 // TODO(tommi): Switch to the same mechanism as Chromium uses for changing
159 // thread priorities.
pbos12411ef2015-11-23 14:47:56 -0800160 return true;
161#else
pbos12411ef2015-11-23 14:47:56 -0800162 const int policy = SCHED_FIFO;
pbos12411ef2015-11-23 14:47:56 -0800163 const int min_prio = sched_get_priority_min(policy);
164 const int max_prio = sched_get_priority_max(policy);
165 if (min_prio == -1 || max_prio == -1) {
166 return false;
167 }
168
169 if (max_prio - min_prio <= 2)
170 return false;
171
Peter Boström97c821d2015-11-24 13:48:13 +0100172 // Convert webrtc priority to system priorities:
pbos12411ef2015-11-23 14:47:56 -0800173 sched_param param;
Peter Boström97c821d2015-11-24 13:48:13 +0100174 const int top_prio = max_prio - 1;
175 const int low_prio = min_prio + 1;
176 switch (priority) {
177 case kLowPriority:
178 param.sched_priority = low_prio;
179 break;
180 case kNormalPriority:
181 // The -1 ensures that the kHighPriority is always greater or equal to
182 // kNormalPriority.
183 param.sched_priority = (low_prio + top_prio - 1) / 2;
184 break;
185 case kHighPriority:
186 param.sched_priority = std::max(top_prio - 2, low_prio);
187 break;
188 case kHighestPriority:
189 param.sched_priority = std::max(top_prio - 1, low_prio);
190 break;
191 case kRealtimePriority:
192 param.sched_priority = top_prio;
193 break;
pbos12411ef2015-11-23 14:47:56 -0800194 }
Peter Boström97c821d2015-11-24 13:48:13 +0100195 return pthread_setschedparam(thread_, policy, &param) == 0;
pbos12411ef2015-11-23 14:47:56 -0800196#endif // defined(WEBRTC_WIN)
197}
198
tommi845afa82016-04-22 09:08:44 -0700199#if defined(WEBRTC_WIN)
200bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200201 RTC_DCHECK(thread_checker_.IsCurrent());
tommi845afa82016-04-22 09:08:44 -0700202 RTC_DCHECK(IsRunning());
203
204 return QueueUserAPC(function, thread_, data) != FALSE;
205}
206#endif
207
Peter Boström8c38e8b2015-11-26 17:45:47 +0100208} // namespace rtc