timer_mgr.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #ifndef TIMER_MGR_H
  7. #define TIMER_MGR_H
  8. #include <asiolink/interval_timer.h>
  9. #include <boost/noncopyable.hpp>
  10. #include <boost/shared_ptr.hpp>
  11. #include <string>
  12. namespace isc {
  13. namespace dhcp {
  14. /// @brief Forward declaration of the @c TimerMgr implementation.
  15. class TimerMgrImpl;
  16. /// @brief Forward declaration of the @c TimerMgr.
  17. class TimerMgr;
  18. /// @brief Type definition of the shared pointer to @c TimerMgr.
  19. typedef boost::shared_ptr<TimerMgr> TimerMgrPtr;
  20. /// @brief Manages a pool of asynchronous interval timers for DHCP server.
  21. ///
  22. /// This class holds a pool of asynchronous interval timers which are
  23. /// capable of interrupting the blocking call to @c select() function in
  24. /// the main threads of the DHCP servers. The main thread can then
  25. /// timely execute the callback function associated with the particular
  26. /// timer.
  27. ///
  28. /// This class is useful for performing periodic actions at the specified
  29. /// intervals, e.g. act upon expired leases (leases reclamation) or
  30. /// return declined leases back to the address pool. Other applications
  31. /// may be added in the future.
  32. ///
  33. /// The @c TimerMgr is a singleton, thus its instance is available from
  34. /// different places in the server code. This is convenient because timers
  35. /// can be installed by different configuration parsers or they can be
  36. /// re-scheduled from the callback functions.
  37. ///
  38. /// The timer is registered using the @c TimerMgr::registerTimer method.
  39. /// Each registered timer has a unique name. It is not possible to register
  40. /// multiple timers with the same name. Each registered timer is associated
  41. /// with the callback function supplied by the caller. This callback function
  42. /// performs the tasks to be executed periodically according to the timer's
  43. /// interval.
  44. ///
  45. /// The registered timer's interval does not begin to elapse until the
  46. /// @c TimerMgr::setup method is called for it.
  47. ///
  48. /// The @c TimerMgr uses worker thread to run the timers. The thread is
  49. /// started and stopped using the @c TimerMgr::startThread and
  50. /// @c TimerMgr::stopThread respectively. The thread calls the blocking
  51. /// @c IOService::run. All the registered timers are associated with
  52. /// this instance of the @c IOService that the thread is running.
  53. /// When the timer elapses a generic callback function is executed
  54. /// @c TimerMgr::timerCallback with the parameter giving the name
  55. /// of the timer for which it has been executed.
  56. ///
  57. /// Every registered timer is associated with an instance of the
  58. /// @c util::WatchSocket object. The socket is registered in the
  59. /// @c IfaceMgr as an "external" socket. When the generic callback
  60. /// function is invoked for the timer, it obtains the instance of the
  61. /// @c util::WatchSocket and marks it "ready". This call effectively
  62. /// writes the data to a socket (pipe) which interrupts the call
  63. /// to the @c select() function in the main thread. When the
  64. /// @c IfaceMgr (in the main thread) detects data transmitted over
  65. /// the external socket it will invoke a callback function
  66. /// associated with this socket. This is the
  67. /// @c TimerMgr::ifaceMgrCallback associated with the socket when the
  68. /// timer is registered. This callback function is executed in the
  69. /// main thread. It clears the socket, which unblocks the worker
  70. /// thread. It also invokes the user callback function specified
  71. /// for a given timer.
  72. ///
  73. /// The @c TimerMgr::timerCallback function searches for the
  74. /// registered timer for which it has been called. This may cause
  75. /// race conditions between the worker thread and the main thread
  76. /// if the latter is modifying the collection of the registered
  77. /// timers. Therefore, the @c TimerMgr does not allow for
  78. /// registering or unregistering the timers when the worker thread
  79. /// is running. The worker thread must be stopped first.
  80. /// It is possible to call @c TimerMgr::setup and @c TimerMgr::cancel
  81. /// while the worker thread is running but this is considered
  82. /// unreliable (may cause race conditions) except the case when the
  83. /// @c TimerMgr::setup is called from the installed callback
  84. /// function to reschedule the ONE_SHOT timer. This is thread safe
  85. /// because the worker thread is blocked while the callback function
  86. /// is executed.
  87. ///
  88. /// The worker thread is blocked when it executes a generic callback
  89. /// function in the @c TimerMgr, which marks the watch socket
  90. /// associated with the elapsed timer as "ready". The thread waits
  91. /// in the callback function until it is notified by the main thread
  92. /// (via conditional variable), that one of the watch sockets has
  93. /// been cleared. It then checks if the main thread cleared the
  94. /// socket that the worker thread had set. It continues to block
  95. /// if this was a different socket. It returns (unblocks) otherwise.
  96. /// The main thread clears the socket when the @c IfaceMgr detects
  97. /// that this socket has been marked ready by the worker thread.
  98. /// This is triggered only when the @c IfaceMgr::receive4 or
  99. /// @c IfaceMgr::receive6 is called. They are called in the main
  100. /// loops of the DHCP servers, which are also responsible for
  101. /// processing received packets. Therefore it may take some
  102. /// time for the main loop to detect that the socket has been
  103. /// marked ready, call appropriate handler for it and clear it.
  104. /// In the mean time, the worker thread will remain blocked.
  105. ///
  106. class TimerMgr : public boost::noncopyable {
  107. public:
  108. /// @brief Returns pointer to the sole instance of the @c TimerMgr.
  109. static const TimerMgrPtr& instance();
  110. /// @brief Destructor.
  111. ///
  112. /// Stops the worker thread if it is running and unregisteres any
  113. /// registered timers.
  114. ~TimerMgr();
  115. /// @name Registering, unregistering and scheduling the timers.
  116. //@{
  117. /// @brief Registers new timers in the @c TimerMgr.
  118. ///
  119. /// This method must not be called while the worker thread is running,
  120. /// as it modifies the internal data structure holding registered
  121. /// timers, which is also accessed from the worker thread via the
  122. /// callback. Inserting new element to this data structure and
  123. /// reading it at the same time would yield undefined behavior.
  124. ///
  125. /// @param timer_name Unique name for the timer.
  126. /// @param callback Pointer to the callback function to be invoked
  127. /// when the timer elapses, e.g. function processing expired leases
  128. /// in the DHCP server.
  129. /// @param interval Timer interval in milliseconds.
  130. /// @param scheduling_mode Scheduling mode of the timer as described in
  131. /// @c asiolink::IntervalTimer::Mode.
  132. ///
  133. /// @throw BadValue if the timer name is invalid or duplicate.
  134. /// @throw InvalidOperation if the worker thread is running.
  135. void registerTimer(const std::string& timer_name,
  136. const asiolink::IntervalTimer::Callback& callback,
  137. const long interval,
  138. const asiolink::IntervalTimer::Mode& scheduling_mode);
  139. /// @brief Unregisters specified timer.
  140. ///
  141. /// This method cancels the timer if it is setup. It removes the external
  142. /// socket from the @c IfaceMgr and closes it. It finally removes the
  143. /// timer from the internal collection of timers.
  144. ///
  145. /// This method must not be called while the worker thread is running,
  146. /// as it modifies the internal data structure holding registered
  147. /// timers, which is also accessed from the worker thread via the
  148. /// callback. Removing element from this data structure and
  149. /// reading it at the same time would yield undefined behavior.
  150. ///
  151. /// @param timer_name Name of the timer to be unregistered.
  152. ///
  153. /// @throw BadValue if the specified timer hasn't been registered.
  154. void unregisterTimer(const std::string& timer_name);
  155. /// @brief Unregisters all timers.
  156. void unregisterTimers();
  157. /// @brief Returns the number of registered timers.
  158. size_t timersCount() const;
  159. /// @brief Schedules the execution of the interval timer.
  160. ///
  161. /// This method schedules the timer, i.e. the callback will be executed
  162. /// after specified interval elapses. The interval has been specified
  163. /// during timer registration. Depending on the mode selected during the
  164. /// timer registration, the callback will be executed once after it has
  165. /// been scheduled or until it is cancelled. Though, in the former case
  166. /// the timer can be re-scheduled in the callback function.
  167. ///
  168. /// @param timer_name Unique timer name.
  169. ///
  170. /// @throw BadValue if the timer hasn't been registered.
  171. void setup(const std::string& timer_name);
  172. /// @brief Cancels the execution of the interval timer.
  173. ///
  174. /// This method has no effect if the timer hasn't been scheduled with
  175. /// the @c TimerMgr::setup method.
  176. ///
  177. /// @param timer_name Unique timer name.
  178. ///
  179. /// @throw BadValue if the timer hasn't been registered.
  180. void cancel(const std::string& timer_name);
  181. //@}
  182. /// @name Starting and stopping the worker thread.
  183. //@{
  184. /// @brief Starts worker thread
  185. ///
  186. /// This method has no effect if the thread has already been started.
  187. void startThread();
  188. /// @brief Stops worker thread.
  189. ///
  190. /// When the thread is being stopped, it is possible that some of the
  191. /// timers have elapsed and marked their respective watch sockets
  192. /// as "ready", but the sockets haven't been yet cleared in the
  193. /// main thread and the installed callbacks haven't been executed.
  194. /// It is possible to control whether those pending callbacks should
  195. /// be executed or not before the call to @c stopThread ends.
  196. /// If the thread is being stopped as a result of the DHCP server
  197. /// reconfiguration running pending callback may take significant
  198. /// amount of time, e.g. when operations on the lease database are
  199. /// involved. If this is a concern, the function parameter should
  200. /// be left at its default value. In this case, however, it is
  201. /// important to note that callbacks installed on ONE_SHOT timers
  202. /// often reschedule the timer. If such callback is not executed
  203. /// the timer will have to be setup by the application when the
  204. /// thread is started again.
  205. ///
  206. /// Setting the @c run_pending_callbacks to true will guarantee
  207. /// that all callbacks for which the timers have already elapsed
  208. /// (and marked their watch sockets as ready) will be executed
  209. /// prior to the return from @c stopThread method. However, this
  210. /// should be avoided if the longer execution time of the
  211. /// @c stopThread function is a concern.
  212. ///
  213. /// This method has no effect if the thread is not running.
  214. ///
  215. /// @param run_pending_callbacks Indicates if the pending callbacks
  216. /// should be executed (if true).
  217. void stopThread(const bool run_pending_callbacks = false);
  218. //@}
  219. /// @brief Returns a reference to IO service used by the @c TimerMgr.
  220. asiolink::IOService& getIOService() const;
  221. private:
  222. /// @brief Private default constructor.
  223. ///
  224. /// The @c TimerMgr is a singleton class which instance must be created
  225. /// using the @c TimerMgr::instance method. Private constructor enforces
  226. /// construction via @c TimerMgr::instance.
  227. TimerMgr();
  228. /// @brief Pointer to @c TimerMgr implementation.
  229. TimerMgrImpl* impl_;
  230. };
  231. } // end of namespace isc::dhcp
  232. } // end of namespace isc
  233. #endif // TIMER_MGR_H