Activity.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // Activity.h
  3. //
  4. // Library: Foundation
  5. // Package: Threading
  6. // Module: ActiveObjects
  7. //
  8. // Definition of the Activity template class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_Activity_INCLUDED
  16. #define Foundation_Activity_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/RunnableAdapter.h"
  19. #include "Poco/ThreadPool.h"
  20. #include "Poco/Event.h"
  21. #include "Poco/Mutex.h"
  22. namespace Poco {
  23. template <class C>
  24. class Activity: public Runnable
  25. /// This template class helps to implement active objects.
  26. /// An active object uses threads to decouple method
  27. /// execution from method invocation, or to perform tasks
  28. /// autonomously, without intervention of a caller.
  29. ///
  30. /// An activity is a (typically longer running) method
  31. /// that executes within its own task. Activities can
  32. /// be started automatically (upon object construction)
  33. /// or manually at a later time. Activities can also
  34. /// be stopped at any time. However, to make stopping
  35. /// an activity work, the method implementing the
  36. /// activity has to check periodically whether it
  37. /// has been requested to stop, and if so, return.
  38. /// Activities are stopped before the object they belong to is
  39. /// destroyed. Methods implementing activities cannot have arguments
  40. /// or return values.
  41. ///
  42. /// Activity objects are used as follows:
  43. ///
  44. /// class ActiveObject
  45. /// {
  46. /// public:
  47. /// ActiveObject():
  48. /// _activity(this, &ActiveObject::runActivity)
  49. /// {
  50. /// ...
  51. /// }
  52. ///
  53. /// ...
  54. ///
  55. /// protected:
  56. /// void runActivity()
  57. /// {
  58. /// while (!_activity.isStopped())
  59. /// {
  60. /// ...
  61. /// }
  62. /// }
  63. ///
  64. /// private:
  65. /// Activity<ActiveObject> _activity;
  66. /// };
  67. {
  68. public:
  69. typedef RunnableAdapter<C> RunnableAdapterType;
  70. typedef typename RunnableAdapterType::Callback Callback;
  71. Activity(C* pOwner, Callback method):
  72. _pOwner(pOwner),
  73. _runnable(*pOwner, method),
  74. _stopped(true),
  75. _running(false),
  76. _done(false)
  77. /// Creates the activity. Call start() to
  78. /// start it.
  79. {
  80. poco_check_ptr (pOwner);
  81. }
  82. ~Activity()
  83. /// Stops and destroys the activity.
  84. {
  85. try
  86. {
  87. stop();
  88. wait();
  89. }
  90. catch (...)
  91. {
  92. poco_unexpected();
  93. }
  94. }
  95. void start()
  96. /// Starts the activity by acquiring a
  97. /// thread for it from the default thread pool.
  98. {
  99. start(ThreadPool::defaultPool());
  100. }
  101. void start(ThreadPool& pool)
  102. {
  103. FastMutex::ScopedLock lock(_mutex);
  104. if (!_running)
  105. {
  106. _done.reset();
  107. _stopped = false;
  108. _running = true;
  109. try
  110. {
  111. pool.start(*this);
  112. }
  113. catch (...)
  114. {
  115. _running = false;
  116. throw;
  117. }
  118. }
  119. }
  120. void stop()
  121. /// Requests to stop the activity.
  122. {
  123. FastMutex::ScopedLock lock(_mutex);
  124. _stopped = true;
  125. }
  126. void wait()
  127. /// Waits for the activity to complete.
  128. {
  129. if (_running)
  130. {
  131. _done.wait();
  132. }
  133. }
  134. void wait(long milliseconds)
  135. /// Waits the given interval for the activity to complete.
  136. /// An TimeoutException is thrown if the activity does not
  137. /// complete within the given interval.
  138. {
  139. if (_running)
  140. {
  141. _done.wait(milliseconds);
  142. }
  143. }
  144. bool isStopped() const
  145. /// Returns true if the activity has been requested to stop.
  146. {
  147. return _stopped;
  148. }
  149. bool isRunning() const
  150. /// Returns true if the activity is running.
  151. {
  152. return _running;
  153. }
  154. protected:
  155. void run()
  156. {
  157. try
  158. {
  159. _runnable.run();
  160. }
  161. catch (...)
  162. {
  163. _running = false;
  164. _done.set();
  165. throw;
  166. }
  167. _running = false;
  168. _done.set();
  169. }
  170. private:
  171. Activity();
  172. Activity(const Activity&);
  173. Activity& operator = (const Activity&);
  174. C* _pOwner;
  175. RunnableAdapterType _runnable;
  176. volatile bool _stopped;
  177. volatile bool _running;
  178. Event _done;
  179. FastMutex _mutex;
  180. };
  181. } // namespace Poco
  182. #endif // Foundation_Activity_INCLUDED