Timer.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Timer.h
  3. //
  4. // Library: Util
  5. // Package: Timer
  6. // Module: Timer
  7. //
  8. // Definition of the Timer class.
  9. //
  10. // Copyright (c) 2009, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Util_Timer_INCLUDED
  16. #define Util_Timer_INCLUDED
  17. #include "Poco/Util/Util.h"
  18. #include "Poco/Util/TimerTask.h"
  19. #include "Poco/TimedNotificationQueue.h"
  20. #include "Poco/Thread.h"
  21. #include "Poco/Runnable.h"
  22. namespace Poco {
  23. namespace Util {
  24. class Util_API Timer: protected Poco::Runnable
  25. /// A Timer allows to schedule tasks (TimerTask objects) for future execution
  26. /// in a background thread. Tasks may be scheduled for one-time execution,
  27. /// or for repeated execution at regular intervals.
  28. ///
  29. /// The Timer object creates a thread that executes all scheduled tasks
  30. /// sequentially. Therefore, tasks should complete their work as quickly
  31. /// as possible, otherwise subsequent tasks may be delayed.
  32. ///
  33. /// Timer is safe for multithreaded use - multiple threads can schedule
  34. /// new tasks simultaneously.
  35. ///
  36. /// Acknowledgement: The interface of this class has been inspired by
  37. /// the java.util.Timer class from Java 1.3.
  38. {
  39. public:
  40. Timer();
  41. /// Creates the Timer.
  42. explicit Timer(Poco::Thread::Priority priority);
  43. /// Creates the Timer, using a timer thread with
  44. /// the given priority.
  45. ~Timer();
  46. /// Destroys the Timer, cancelling all pending tasks.
  47. void cancel(bool wait = false);
  48. /// Cancels all pending tasks.
  49. ///
  50. /// If a task is currently running, it is allowed to finish.
  51. ///
  52. /// Task cancellation is done asynchronously. If wait
  53. /// is false, cancel() returns immediately and the
  54. /// task queue will be purged as soon as the currently
  55. /// running task finishes. If wait is true, waits
  56. /// until the queue has been purged.
  57. void schedule(TimerTask::Ptr pTask, Poco::Timestamp time);
  58. /// Schedules a task for execution at the specified time.
  59. ///
  60. /// If the time lies in the past, the task is executed
  61. /// immediately.
  62. ///
  63. /// Note: the relative time the task will be executed
  64. /// won't change if the system's time changes. If the
  65. /// given time is 10 seconds in the future at the point
  66. /// schedule() is called, the task will be executed 10
  67. /// seconds later, even if the system time changes in
  68. /// between.
  69. void schedule(TimerTask::Ptr pTask, Poco::Clock clock);
  70. /// Schedules a task for execution at the specified time.
  71. ///
  72. /// If the time lies in the past, the task is executed
  73. /// immediately.
  74. void schedule(TimerTask::Ptr pTask, long delay, long interval);
  75. /// Schedules a task for periodic execution.
  76. ///
  77. /// The task is first executed after the given delay.
  78. /// Subsequently, the task is executed periodically with
  79. /// the given interval in milliseconds between invocations.
  80. void schedule(TimerTask::Ptr pTask, Poco::Timestamp time, long interval);
  81. /// Schedules a task for periodic execution.
  82. ///
  83. /// The task is first executed at the given time.
  84. /// Subsequently, the task is executed periodically with
  85. /// the given interval in milliseconds between invocations.
  86. ///
  87. /// Note: the relative time the task will be executed
  88. /// won't change if the system's time changes. If the
  89. /// given time is 10 seconds in the future at the point
  90. /// schedule() is called, the task will be executed 10
  91. /// seconds later, even if the system time changes in
  92. /// between.
  93. void schedule(TimerTask::Ptr pTask, Poco::Clock clock, long interval);
  94. /// Schedules a task for periodic execution.
  95. ///
  96. /// The task is first executed at the given time.
  97. /// Subsequently, the task is executed periodically with
  98. /// the given interval in milliseconds between invocations.
  99. void scheduleAtFixedRate(TimerTask::Ptr pTask, long delay, long interval);
  100. /// Schedules a task for periodic execution at a fixed rate.
  101. ///
  102. /// The task is first executed after the given delay.
  103. /// Subsequently, the task is executed periodically
  104. /// every number of milliseconds specified by interval.
  105. ///
  106. /// If task execution takes longer than the given interval,
  107. /// further executions are delayed.
  108. void scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Timestamp time, long interval);
  109. /// Schedules a task for periodic execution at a fixed rate.
  110. ///
  111. /// The task is first executed at the given time.
  112. /// Subsequently, the task is executed periodically
  113. /// every number of milliseconds specified by interval.
  114. ///
  115. /// If task execution takes longer than the given interval,
  116. /// further executions are delayed.
  117. ///
  118. /// Note: the relative time the task will be executed
  119. /// won't change if the system's time changes. If the
  120. /// given time is 10 seconds in the future at the point
  121. /// scheduleAtFixedRate() is called, the task will be executed 10
  122. /// seconds later, even if the system time changes in
  123. /// between.
  124. void scheduleAtFixedRate(TimerTask::Ptr pTask, Poco::Clock clock, long interval);
  125. /// Schedules a task for periodic execution at a fixed rate.
  126. ///
  127. /// The task is first executed at the given time.
  128. /// Subsequently, the task is executed periodically
  129. /// every number of milliseconds specified by interval.
  130. ///
  131. /// If task execution takes longer than the given interval,
  132. /// further executions are delayed.
  133. protected:
  134. void run();
  135. static void validateTask(const TimerTask::Ptr& pTask);
  136. private:
  137. Timer(const Timer&);
  138. Timer& operator = (const Timer&);
  139. Poco::TimedNotificationQueue _queue;
  140. Poco::Thread _thread;
  141. };
  142. } } // namespace Poco::Util
  143. #endif // Util_Timer_INCLUDED