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.

166 lines
4.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2009, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/signalthread.h"
  28. #include "talk/base/common.h"
  29. namespace talk_base {
  30. ///////////////////////////////////////////////////////////////////////////////
  31. // SignalThread
  32. ///////////////////////////////////////////////////////////////////////////////
  33. SignalThread::SignalThread()
  34. : main_(Thread::Current()),
  35. worker_(this),
  36. state_(kInit),
  37. refcount_(1) {
  38. main_->SignalQueueDestroyed.connect(this,
  39. &SignalThread::OnMainThreadDestroyed);
  40. worker_.SetName("SignalThread", this);
  41. }
  42. SignalThread::~SignalThread() {
  43. ASSERT(refcount_ == 0);
  44. }
  45. bool SignalThread::SetName(const std::string& name, const void* obj) {
  46. EnterExit ee(this);
  47. ASSERT(main_->IsCurrent());
  48. ASSERT(kInit == state_);
  49. return worker_.SetName(name, obj);
  50. }
  51. bool SignalThread::SetPriority(ThreadPriority priority) {
  52. EnterExit ee(this);
  53. ASSERT(main_->IsCurrent());
  54. ASSERT(kInit == state_);
  55. return worker_.SetPriority(priority);
  56. }
  57. void SignalThread::Start() {
  58. EnterExit ee(this);
  59. ASSERT(main_->IsCurrent());
  60. if (kInit == state_ || kComplete == state_) {
  61. state_ = kRunning;
  62. OnWorkStart();
  63. worker_.Start();
  64. } else {
  65. ASSERT(false);
  66. }
  67. }
  68. void SignalThread::Destroy(bool wait) {
  69. EnterExit ee(this);
  70. ASSERT(main_->IsCurrent());
  71. if ((kInit == state_) || (kComplete == state_)) {
  72. refcount_--;
  73. } else if (kRunning == state_ || kReleasing == state_) {
  74. state_ = kStopping;
  75. // OnWorkStop() must follow Quit(), so that when the thread wakes up due to
  76. // OWS(), ContinueWork() will return false.
  77. worker_.Quit();
  78. OnWorkStop();
  79. if (wait) {
  80. // Release the thread's lock so that it can return from ::Run.
  81. cs_.Leave();
  82. worker_.Stop();
  83. cs_.Enter();
  84. refcount_--;
  85. }
  86. } else {
  87. ASSERT(false);
  88. }
  89. }
  90. void SignalThread::Release() {
  91. EnterExit ee(this);
  92. ASSERT(main_->IsCurrent());
  93. if (kComplete == state_) {
  94. refcount_--;
  95. } else if (kRunning == state_) {
  96. state_ = kReleasing;
  97. } else {
  98. // if (kInit == state_) use Destroy()
  99. ASSERT(false);
  100. }
  101. }
  102. bool SignalThread::ContinueWork() {
  103. EnterExit ee(this);
  104. ASSERT(worker_.IsCurrent());
  105. return worker_.ProcessMessages(0);
  106. }
  107. void SignalThread::OnMessage(Message *msg) {
  108. EnterExit ee(this);
  109. if (ST_MSG_WORKER_DONE == msg->message_id) {
  110. ASSERT(main_->IsCurrent());
  111. OnWorkDone();
  112. bool do_delete = false;
  113. if (kRunning == state_) {
  114. state_ = kComplete;
  115. } else {
  116. do_delete = true;
  117. }
  118. if (kStopping != state_) {
  119. // Before signaling that the work is done, make sure that the worker
  120. // thread actually is done. We got here because DoWork() finished and
  121. // Run() posted the ST_MSG_WORKER_DONE message. This means the worker
  122. // thread is about to go away anyway, but sometimes it doesn't actually
  123. // finish before SignalWorkDone is processed, and for a reusable
  124. // SignalThread this makes an assert in thread.cc fire.
  125. //
  126. // Calling Stop() on the worker ensures that the OS thread that underlies
  127. // the worker will finish, and will be set to NULL, enabling us to call
  128. // Start() again.
  129. worker_.Stop();
  130. SignalWorkDone(this);
  131. }
  132. if (do_delete) {
  133. refcount_--;
  134. }
  135. }
  136. }
  137. void SignalThread::Run() {
  138. DoWork();
  139. {
  140. EnterExit ee(this);
  141. if (main_) {
  142. main_->Post(this, ST_MSG_WORKER_DONE);
  143. }
  144. }
  145. }
  146. void SignalThread::OnMainThreadDestroyed() {
  147. EnterExit ee(this);
  148. main_ = NULL;
  149. }
  150. } // namespace talk_base