blob: 8a5f2c9d6d235a609ea4a11b71641dce4d5659e3 [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>
Jonas Olssona4d87372019-07-05 19:08:33 +020018
Yves Gerey988cc082018-10-23 12:03:01 +020019#include <algorithm>
20
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/checks.h"
Tommibebc6902015-05-18 09:51:42 +020022
Tommibebc6902015-05-18 09:51:42 +020023namespace rtc {
pbos12411ef2015-11-23 14:47:56 -080024namespace {
Niels Möller22660f32019-05-03 10:41:36 +020025#if !defined(WEBRTC_WIN)
pbos12411ef2015-11-23 14:47:56 -080026struct ThreadAttributes {
27 ThreadAttributes() { pthread_attr_init(&attr); }
28 ~ThreadAttributes() { pthread_attr_destroy(&attr); }
29 pthread_attr_t* operator&() { return &attr; }
30 pthread_attr_t attr;
31};
pbos12411ef2015-11-23 14:47:56 -080032#endif // defined(WEBRTC_WIN)
Jonas Olssona4d87372019-07-05 19:08:33 +020033} // namespace
pbos12411ef2015-11-23 14:47:56 -080034
tommi0f8b4032017-02-22 11:22:05 -080035PlatformThread::PlatformThread(ThreadRunFunction func,
36 void* obj,
Danil Chapovalov5a1a6db2019-01-17 19:55:46 +010037 absl::string_view thread_name,
tommi0f8b4032017-02-22 11:22:05 -080038 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);
Sebastian Janssonc01367d2019-04-08 15:20:44 +020044 spawned_thread_checker_.Detach();
pbos12411ef2015-11-23 14:47:56 -080045}
46
47PlatformThread::~PlatformThread() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020048 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080049#if defined(WEBRTC_WIN)
50 RTC_DCHECK(!thread_);
tommi845afa82016-04-22 09:08:44 -070051 RTC_DCHECK(!thread_id_);
pbos12411ef2015-11-23 14:47:56 -080052#endif // defined(WEBRTC_WIN)
53}
54
55#if defined(WEBRTC_WIN)
56DWORD WINAPI PlatformThread::StartThread(void* param) {
perkj6a2e20a2016-11-30 04:53:08 -080057 // 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);
pbos12411ef2015-11-23 14:47:56 -080062 static_cast<PlatformThread*>(param)->Run();
63 return 0;
64}
65#else
66void* PlatformThread::StartThread(void* param) {
67 static_cast<PlatformThread*>(param)->Run();
68 return 0;
69}
70#endif // defined(WEBRTC_WIN)
71
Peter Boström8c38e8b2015-11-26 17:45:47 +010072void PlatformThread::Start() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020073 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080074 RTC_DCHECK(!thread_) << "Thread already started?";
75#if defined(WEBRTC_WIN)
pbos12411ef2015-11-23 14:47:56 -080076 // 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.
deadbeef37f5ecf2017-02-27 14:06:41 -080079 thread_ = ::CreateThread(nullptr, 1024 * 1024, &StartThread, this,
tommi845afa82016-04-22 09:08:44 -070080 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id_);
pbos12411ef2015-11-23 14:47:56 -080081 RTC_CHECK(thread_) << "CreateThread failed";
tommi845afa82016-04-22 09:08:44 -070082 RTC_DCHECK(thread_id_);
pbos12411ef2015-11-23 14:47:56 -080083#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)
pbos12411ef2015-11-23 14:47:56 -080089}
90
Peter Boström8c38e8b2015-11-26 17:45:47 +010091bool PlatformThread::IsRunning() const {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020092 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080093#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +010094 return thread_ != nullptr;
pbos12411ef2015-11-23 14:47:56 -080095#else
Peter Boström8c38e8b2015-11-26 17:45:47 +010096 return thread_ != 0;
97#endif // defined(WEBRTC_WIN)
98}
pbos12411ef2015-11-23 14:47:56 -080099
tommi845afa82016-04-22 09:08:44 -0700100PlatformThreadRef PlatformThread::GetThreadRef() const {
101#if defined(WEBRTC_WIN)
102 return thread_id_;
103#else
104 return thread_;
105#endif // defined(WEBRTC_WIN)
106}
107
Peter Boström8c38e8b2015-11-26 17:45:47 +0100108void PlatformThread::Stop() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200109 RTC_DCHECK(thread_checker_.IsCurrent());
Peter Boström8c38e8b2015-11-26 17:45:47 +0100110 if (!IsRunning())
111 return;
112
113#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100114 WaitForSingleObject(thread_, INFINITE);
115 CloseHandle(thread_);
116 thread_ = nullptr;
tommi845afa82016-04-22 09:08:44 -0700117 thread_id_ = 0;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100118#else
pbos12411ef2015-11-23 14:47:56 -0800119 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr));
120 thread_ = 0;
121#endif // defined(WEBRTC_WIN)
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200122 spawned_thread_checker_.Detach();
pbos12411ef2015-11-23 14:47:56 -0800123}
124
125void PlatformThread::Run() {
tommi0f8b4032017-02-22 11:22:05 -0800126 // Attach the worker thread checker to this thread.
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200127 RTC_DCHECK(spawned_thread_checker_.IsCurrent());
tommi0f8b4032017-02-22 11:22:05 -0800128 rtc::SetCurrentThreadName(name_.c_str());
Niels Möller4731f002019-05-03 09:34:24 +0200129 SetPriority(priority_);
130 run_function_(obj_);
pbos12411ef2015-11-23 14:47:56 -0800131}
132
133bool PlatformThread::SetPriority(ThreadPriority priority) {
Niels Möller4731f002019-05-03 09:34:24 +0200134 RTC_DCHECK(spawned_thread_checker_.IsCurrent());
tommi0f8b4032017-02-22 11:22:05 -0800135
Peter Boströmc6612132015-11-24 18:10:24 +0100136#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100137 return SetThreadPriority(thread_, priority) != FALSE;
Wez0614ed92018-02-06 13:38:21 -0800138#elif defined(__native_client__) || defined(WEBRTC_FUCHSIA)
139 // Setting thread priorities is not supported in NaCl or Fuchsia.
Peter Boströmc6612132015-11-24 18:10:24 +0100140 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.
pbos12411ef2015-11-23 14:47:56 -0800144 return true;
145#else
pbos12411ef2015-11-23 14:47:56 -0800146 const int policy = SCHED_FIFO;
pbos12411ef2015-11-23 14:47:56 -0800147 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
Peter Boström97c821d2015-11-24 13:48:13 +0100156 // Convert webrtc priority to system priorities:
pbos12411ef2015-11-23 14:47:56 -0800157 sched_param param;
Peter Boström97c821d2015-11-24 13:48:13 +0100158 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;
pbos12411ef2015-11-23 14:47:56 -0800178 }
Peter Boström97c821d2015-11-24 13:48:13 +0100179 return pthread_setschedparam(thread_, policy, &param) == 0;
pbos12411ef2015-11-23 14:47:56 -0800180#endif // defined(WEBRTC_WIN)
181}
182
tommi845afa82016-04-22 09:08:44 -0700183#if defined(WEBRTC_WIN)
184bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200185 RTC_DCHECK(thread_checker_.IsCurrent());
tommi845afa82016-04-22 09:08:44 -0700186 RTC_DCHECK(IsRunning());
187
188 return QueueUserAPC(function, thread_, data) != FALSE;
189}
190#endif
191
Peter Boström8c38e8b2015-11-26 17:45:47 +0100192} // namespace rtc