You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

192 lines
5.7 KiB

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