blob: cb5eb8b96e67c65db68dde48fd8afc8f0fa7dbd0 [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 {
Niels Möller22660f32019-05-03 10:41:36 +020024#if !defined(WEBRTC_WIN)
pbos12411ef2015-11-23 14:47:56 -080025struct ThreadAttributes {
26 ThreadAttributes() { pthread_attr_init(&attr); }
27 ~ThreadAttributes() { pthread_attr_destroy(&attr); }
28 pthread_attr_t* operator&() { return &attr; }
29 pthread_attr_t attr;
30};
pbos12411ef2015-11-23 14:47:56 -080031#endif // defined(WEBRTC_WIN)
32}
33
tommi0f8b4032017-02-22 11:22:05 -080034PlatformThread::PlatformThread(ThreadRunFunction func,
35 void* obj,
Danil Chapovalov5a1a6db2019-01-17 19:55:46 +010036 absl::string_view thread_name,
tommi0f8b4032017-02-22 11:22:05 -080037 ThreadPriority priority /*= kNormalPriority*/)
38 : run_function_(func), priority_(priority), obj_(obj), name_(thread_name) {
39 RTC_DCHECK(func);
40 RTC_DCHECK(!name_.empty());
41 // TODO(tommi): Consider lowering the limit to 15 (limit on Linux).
42 RTC_DCHECK(name_.length() < 64);
Sebastian Janssonc01367d2019-04-08 15:20:44 +020043 spawned_thread_checker_.Detach();
pbos12411ef2015-11-23 14:47:56 -080044}
45
46PlatformThread::~PlatformThread() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020047 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080048#if defined(WEBRTC_WIN)
49 RTC_DCHECK(!thread_);
tommi845afa82016-04-22 09:08:44 -070050 RTC_DCHECK(!thread_id_);
pbos12411ef2015-11-23 14:47:56 -080051#endif // defined(WEBRTC_WIN)
52}
53
54#if defined(WEBRTC_WIN)
55DWORD WINAPI PlatformThread::StartThread(void* param) {
perkj6a2e20a2016-11-30 04:53:08 -080056 // The GetLastError() function only returns valid results when it is called
57 // after a Win32 API function that returns a "failed" result. A crash dump
58 // contains the result from GetLastError() and to make sure it does not
59 // falsely report a Windows error we call SetLastError here.
60 ::SetLastError(ERROR_SUCCESS);
pbos12411ef2015-11-23 14:47:56 -080061 static_cast<PlatformThread*>(param)->Run();
62 return 0;
63}
64#else
65void* PlatformThread::StartThread(void* param) {
66 static_cast<PlatformThread*>(param)->Run();
67 return 0;
68}
69#endif // defined(WEBRTC_WIN)
70
Peter Boström8c38e8b2015-11-26 17:45:47 +010071void PlatformThread::Start() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020072 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080073 RTC_DCHECK(!thread_) << "Thread already started?";
74#if defined(WEBRTC_WIN)
pbos12411ef2015-11-23 14:47:56 -080075 // See bug 2902 for background on STACK_SIZE_PARAM_IS_A_RESERVATION.
76 // Set the reserved stack stack size to 1M, which is the default on Windows
77 // and Linux.
deadbeef37f5ecf2017-02-27 14:06:41 -080078 thread_ = ::CreateThread(nullptr, 1024 * 1024, &StartThread, this,
tommi845afa82016-04-22 09:08:44 -070079 STACK_SIZE_PARAM_IS_A_RESERVATION, &thread_id_);
pbos12411ef2015-11-23 14:47:56 -080080 RTC_CHECK(thread_) << "CreateThread failed";
tommi845afa82016-04-22 09:08:44 -070081 RTC_DCHECK(thread_id_);
pbos12411ef2015-11-23 14:47:56 -080082#else
83 ThreadAttributes attr;
84 // Set the stack stack size to 1M.
85 pthread_attr_setstacksize(&attr, 1024 * 1024);
86 RTC_CHECK_EQ(0, pthread_create(&thread_, &attr, &StartThread, this));
87#endif // defined(WEBRTC_WIN)
pbos12411ef2015-11-23 14:47:56 -080088}
89
Peter Boström8c38e8b2015-11-26 17:45:47 +010090bool PlatformThread::IsRunning() const {
Sebastian Janssonc01367d2019-04-08 15:20:44 +020091 RTC_DCHECK(thread_checker_.IsCurrent());
pbos12411ef2015-11-23 14:47:56 -080092#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +010093 return thread_ != nullptr;
pbos12411ef2015-11-23 14:47:56 -080094#else
Peter Boström8c38e8b2015-11-26 17:45:47 +010095 return thread_ != 0;
96#endif // defined(WEBRTC_WIN)
97}
pbos12411ef2015-11-23 14:47:56 -080098
tommi845afa82016-04-22 09:08:44 -070099PlatformThreadRef PlatformThread::GetThreadRef() const {
100#if defined(WEBRTC_WIN)
101 return thread_id_;
102#else
103 return thread_;
104#endif // defined(WEBRTC_WIN)
105}
106
Peter Boström8c38e8b2015-11-26 17:45:47 +0100107void PlatformThread::Stop() {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200108 RTC_DCHECK(thread_checker_.IsCurrent());
Peter Boström8c38e8b2015-11-26 17:45:47 +0100109 if (!IsRunning())
110 return;
111
112#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100113 WaitForSingleObject(thread_, INFINITE);
114 CloseHandle(thread_);
115 thread_ = nullptr;
tommi845afa82016-04-22 09:08:44 -0700116 thread_id_ = 0;
Peter Boström8c38e8b2015-11-26 17:45:47 +0100117#else
pbos12411ef2015-11-23 14:47:56 -0800118 RTC_CHECK_EQ(0, pthread_join(thread_, nullptr));
119 thread_ = 0;
120#endif // defined(WEBRTC_WIN)
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200121 spawned_thread_checker_.Detach();
pbos12411ef2015-11-23 14:47:56 -0800122}
123
124void PlatformThread::Run() {
tommi0f8b4032017-02-22 11:22:05 -0800125 // Attach the worker thread checker to this thread.
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200126 RTC_DCHECK(spawned_thread_checker_.IsCurrent());
tommi0f8b4032017-02-22 11:22:05 -0800127 rtc::SetCurrentThreadName(name_.c_str());
Niels Möller4731f002019-05-03 09:34:24 +0200128 SetPriority(priority_);
129 run_function_(obj_);
pbos12411ef2015-11-23 14:47:56 -0800130}
131
132bool PlatformThread::SetPriority(ThreadPriority priority) {
Niels Möller4731f002019-05-03 09:34:24 +0200133 RTC_DCHECK(spawned_thread_checker_.IsCurrent());
tommi0f8b4032017-02-22 11:22:05 -0800134
Peter Boströmc6612132015-11-24 18:10:24 +0100135#if defined(WEBRTC_WIN)
Peter Boström8c38e8b2015-11-26 17:45:47 +0100136 return SetThreadPriority(thread_, priority) != FALSE;
Wez0614ed92018-02-06 13:38:21 -0800137#elif defined(__native_client__) || defined(WEBRTC_FUCHSIA)
138 // Setting thread priorities is not supported in NaCl or Fuchsia.
Peter Boströmc6612132015-11-24 18:10:24 +0100139 return true;
140#elif defined(WEBRTC_CHROMIUM_BUILD) && defined(WEBRTC_LINUX)
141 // TODO(tommi): Switch to the same mechanism as Chromium uses for changing
142 // thread priorities.
pbos12411ef2015-11-23 14:47:56 -0800143 return true;
144#else
pbos12411ef2015-11-23 14:47:56 -0800145 const int policy = SCHED_FIFO;
pbos12411ef2015-11-23 14:47:56 -0800146 const int min_prio = sched_get_priority_min(policy);
147 const int max_prio = sched_get_priority_max(policy);
148 if (min_prio == -1 || max_prio == -1) {
149 return false;
150 }
151
152 if (max_prio - min_prio <= 2)
153 return false;
154
Peter Boström97c821d2015-11-24 13:48:13 +0100155 // Convert webrtc priority to system priorities:
pbos12411ef2015-11-23 14:47:56 -0800156 sched_param param;
Peter Boström97c821d2015-11-24 13:48:13 +0100157 const int top_prio = max_prio - 1;
158 const int low_prio = min_prio + 1;
159 switch (priority) {
160 case kLowPriority:
161 param.sched_priority = low_prio;
162 break;
163 case kNormalPriority:
164 // The -1 ensures that the kHighPriority is always greater or equal to
165 // kNormalPriority.
166 param.sched_priority = (low_prio + top_prio - 1) / 2;
167 break;
168 case kHighPriority:
169 param.sched_priority = std::max(top_prio - 2, low_prio);
170 break;
171 case kHighestPriority:
172 param.sched_priority = std::max(top_prio - 1, low_prio);
173 break;
174 case kRealtimePriority:
175 param.sched_priority = top_prio;
176 break;
pbos12411ef2015-11-23 14:47:56 -0800177 }
Peter Boström97c821d2015-11-24 13:48:13 +0100178 return pthread_setschedparam(thread_, policy, &param) == 0;
pbos12411ef2015-11-23 14:47:56 -0800179#endif // defined(WEBRTC_WIN)
180}
181
tommi845afa82016-04-22 09:08:44 -0700182#if defined(WEBRTC_WIN)
183bool PlatformThread::QueueAPC(PAPCFUNC function, ULONG_PTR data) {
Sebastian Janssonc01367d2019-04-08 15:20:44 +0200184 RTC_DCHECK(thread_checker_.IsCurrent());
tommi845afa82016-04-22 09:08:44 -0700185 RTC_DCHECK(IsRunning());
186
187 return QueueUserAPC(function, thread_, data) != FALSE;
188}
189#endif
190
Peter Boström8c38e8b2015-11-26 17:45:47 +0100191} // namespace rtc