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.

158 lines
5.3 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. #ifndef RTC_BASE_SIGNAL_THREAD_H_
  11. #define RTC_BASE_SIGNAL_THREAD_H_
  12. #include <string>
  13. #include "rtc_base/checks.h"
  14. #include "rtc_base/constructor_magic.h"
  15. #include "rtc_base/critical_section.h"
  16. #include "rtc_base/message_handler.h"
  17. #include "rtc_base/message_queue.h"
  18. #include "rtc_base/third_party/sigslot/sigslot.h"
  19. #include "rtc_base/thread.h"
  20. #include "rtc_base/thread_annotations.h"
  21. namespace rtc {
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // SignalThread - Base class for worker threads. The main thread should call
  24. // Start() to begin work, and then follow one of these models:
  25. // Normal: Wait for SignalWorkDone, and then call Release to destroy.
  26. // Cancellation: Call Release(true), to abort the worker thread.
  27. // Fire-and-forget: Call Release(false), which allows the thread to run to
  28. // completion, and then self-destruct without further notification.
  29. // Periodic tasks: Wait for SignalWorkDone, then eventually call Start()
  30. // again to repeat the task. When the instance isn't needed anymore,
  31. // call Release. DoWork, OnWorkStart and OnWorkStop are called again,
  32. // on a new thread.
  33. // The subclass should override DoWork() to perform the background task. By
  34. // periodically calling ContinueWork(), it can check for cancellation.
  35. // OnWorkStart and OnWorkDone can be overridden to do pre- or post-work
  36. // tasks in the context of the main thread.
  37. ///////////////////////////////////////////////////////////////////////////////
  38. class SignalThread : public sigslot::has_slots<>, protected MessageHandler {
  39. public:
  40. SignalThread();
  41. // Context: Main Thread. Call before Start to change the worker's name.
  42. bool SetName(const std::string& name, const void* obj);
  43. // Context: Main Thread. Call to begin the worker thread.
  44. void Start();
  45. // Context: Main Thread. If the worker thread is not running, deletes the
  46. // object immediately. Otherwise, asks the worker thread to abort processing,
  47. // and schedules the object to be deleted once the worker exits.
  48. // SignalWorkDone will not be signalled. If wait is true, does not return
  49. // until the thread is deleted.
  50. void Destroy(bool wait);
  51. // Context: Main Thread. If the worker thread is complete, deletes the
  52. // object immediately. Otherwise, schedules the object to be deleted once
  53. // the worker thread completes. SignalWorkDone will be signalled.
  54. void Release();
  55. // Context: Main Thread. Signalled when work is complete.
  56. sigslot::signal1<SignalThread*> SignalWorkDone;
  57. enum { ST_MSG_WORKER_DONE, ST_MSG_FIRST_AVAILABLE };
  58. protected:
  59. ~SignalThread() override;
  60. Thread* worker() { return &worker_; }
  61. // Context: Main Thread. Subclass should override to do pre-work setup.
  62. virtual void OnWorkStart() {}
  63. // Context: Worker Thread. Subclass should override to do work.
  64. virtual void DoWork() = 0;
  65. // Context: Worker Thread. Subclass should call periodically to
  66. // dispatch messages and determine if the thread should terminate.
  67. bool ContinueWork();
  68. // Context: Worker Thread. Subclass should override when extra work is
  69. // needed to abort the worker thread.
  70. virtual void OnWorkStop() {}
  71. // Context: Main Thread. Subclass should override to do post-work cleanup.
  72. virtual void OnWorkDone() {}
  73. // Context: Any Thread. If subclass overrides, be sure to call the base
  74. // implementation. Do not use (message_id < ST_MSG_FIRST_AVAILABLE)
  75. void OnMessage(Message* msg) override;
  76. private:
  77. enum State {
  78. kInit, // Initialized, but not started
  79. kRunning, // Started and doing work
  80. kReleasing, // Same as running, but to be deleted when work is done
  81. kComplete, // Work is done
  82. kStopping, // Work is being interrupted
  83. };
  84. class Worker : public Thread {
  85. public:
  86. explicit Worker(SignalThread* parent);
  87. ~Worker() override;
  88. void Run() override;
  89. bool IsProcessingMessagesForTesting() override;
  90. private:
  91. SignalThread* parent_;
  92. RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(Worker);
  93. };
  94. class RTC_SCOPED_LOCKABLE EnterExit {
  95. public:
  96. explicit EnterExit(SignalThread* t) RTC_EXCLUSIVE_LOCK_FUNCTION(t->cs_)
  97. : t_(t) {
  98. t_->cs_.Enter();
  99. // If refcount_ is zero then the object has already been deleted and we
  100. // will be double-deleting it in ~EnterExit()! (shouldn't happen)
  101. RTC_DCHECK_NE(0, t_->refcount_);
  102. ++t_->refcount_;
  103. }
  104. ~EnterExit() RTC_UNLOCK_FUNCTION() {
  105. bool d = (0 == --t_->refcount_);
  106. t_->cs_.Leave();
  107. if (d)
  108. delete t_;
  109. }
  110. private:
  111. SignalThread* t_;
  112. RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(EnterExit);
  113. };
  114. void Run();
  115. void OnMainThreadDestroyed();
  116. Thread* main_;
  117. Worker worker_;
  118. CriticalSection cs_;
  119. State state_;
  120. int refcount_;
  121. RTC_DISALLOW_COPY_AND_ASSIGN(SignalThread);
  122. };
  123. ///////////////////////////////////////////////////////////////////////////////
  124. } // namespace rtc
  125. #endif // RTC_BASE_SIGNAL_THREAD_H_