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.

162 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 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/signal_thread.h"
  11. #include <memory>
  12. #include "rtc_base/checks.h"
  13. #include "rtc_base/location.h"
  14. #include "rtc_base/null_socket_server.h"
  15. #include "rtc_base/socket_server.h"
  16. namespace rtc {
  17. ///////////////////////////////////////////////////////////////////////////////
  18. // SignalThread
  19. ///////////////////////////////////////////////////////////////////////////////
  20. SignalThread::SignalThread()
  21. : main_(Thread::Current()), worker_(this), state_(kInit), refcount_(1) {
  22. main_->SignalQueueDestroyed.connect(this,
  23. &SignalThread::OnMainThreadDestroyed);
  24. worker_.SetName("SignalThread", this);
  25. }
  26. SignalThread::~SignalThread() {
  27. RTC_DCHECK(refcount_ == 0);
  28. }
  29. bool SignalThread::SetName(const std::string& name, const void* obj) {
  30. EnterExit ee(this);
  31. RTC_DCHECK(main_->IsCurrent());
  32. RTC_DCHECK(kInit == state_);
  33. return worker_.SetName(name, obj);
  34. }
  35. void SignalThread::Start() {
  36. EnterExit ee(this);
  37. RTC_DCHECK(main_->IsCurrent());
  38. if (kInit == state_ || kComplete == state_) {
  39. state_ = kRunning;
  40. OnWorkStart();
  41. worker_.Start();
  42. } else {
  43. RTC_NOTREACHED();
  44. }
  45. }
  46. void SignalThread::Destroy(bool wait) {
  47. EnterExit ee(this);
  48. RTC_DCHECK(main_->IsCurrent());
  49. if ((kInit == state_) || (kComplete == state_)) {
  50. refcount_--;
  51. } else if (kRunning == state_ || kReleasing == state_) {
  52. state_ = kStopping;
  53. // OnWorkStop() must follow Quit(), so that when the thread wakes up due to
  54. // OWS(), ContinueWork() will return false.
  55. worker_.Quit();
  56. OnWorkStop();
  57. if (wait) {
  58. // Release the thread's lock so that it can return from ::Run.
  59. cs_.Leave();
  60. worker_.Stop();
  61. cs_.Enter();
  62. refcount_--;
  63. }
  64. } else {
  65. RTC_NOTREACHED();
  66. }
  67. }
  68. void SignalThread::Release() {
  69. EnterExit ee(this);
  70. RTC_DCHECK(main_->IsCurrent());
  71. if (kComplete == state_) {
  72. refcount_--;
  73. } else if (kRunning == state_) {
  74. state_ = kReleasing;
  75. } else {
  76. // if (kInit == state_) use Destroy()
  77. RTC_NOTREACHED();
  78. }
  79. }
  80. bool SignalThread::ContinueWork() {
  81. EnterExit ee(this);
  82. RTC_DCHECK(worker_.IsCurrent());
  83. return worker_.ProcessMessages(0);
  84. }
  85. void SignalThread::OnMessage(Message* msg) {
  86. EnterExit ee(this);
  87. if (ST_MSG_WORKER_DONE == msg->message_id) {
  88. RTC_DCHECK(main_->IsCurrent());
  89. OnWorkDone();
  90. bool do_delete = false;
  91. if (kRunning == state_) {
  92. state_ = kComplete;
  93. } else {
  94. do_delete = true;
  95. }
  96. if (kStopping != state_) {
  97. // Before signaling that the work is done, make sure that the worker
  98. // thread actually is done. We got here because DoWork() finished and
  99. // Run() posted the ST_MSG_WORKER_DONE message. This means the worker
  100. // thread is about to go away anyway, but sometimes it doesn't actually
  101. // finish before SignalWorkDone is processed, and for a reusable
  102. // SignalThread this makes an assert in thread.cc fire.
  103. //
  104. // Calling Stop() on the worker ensures that the OS thread that underlies
  105. // the worker will finish, and will be set to null, enabling us to call
  106. // Start() again.
  107. worker_.Stop();
  108. SignalWorkDone(this);
  109. }
  110. if (do_delete) {
  111. refcount_--;
  112. }
  113. }
  114. }
  115. SignalThread::Worker::Worker(SignalThread* parent)
  116. : Thread(std::make_unique<NullSocketServer>(), /*do_init=*/false),
  117. parent_(parent) {
  118. DoInit();
  119. }
  120. SignalThread::Worker::~Worker() {
  121. Stop();
  122. }
  123. void SignalThread::Worker::Run() {
  124. parent_->Run();
  125. }
  126. void SignalThread::Run() {
  127. DoWork();
  128. {
  129. EnterExit ee(this);
  130. if (main_) {
  131. main_->Post(RTC_FROM_HERE, this, ST_MSG_WORKER_DONE);
  132. }
  133. }
  134. }
  135. void SignalThread::OnMainThreadDestroyed() {
  136. EnterExit ee(this);
  137. main_ = nullptr;
  138. }
  139. bool SignalThread::Worker::IsProcessingMessagesForTesting() {
  140. return false;
  141. }
  142. } // namespace rtc