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.

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