interval_timer.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright (C) 2011, 2014-2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <config.h>
  15. #include <boost/bind.hpp>
  16. #include <boost/enable_shared_from_this.hpp>
  17. #include <boost/shared_ptr.hpp>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/asio.hpp>
  20. #include <asiolink/interval_timer.h>
  21. #include <asiolink/io_service.h>
  22. namespace isc {
  23. namespace asiolink {
  24. /// This class holds a call back function of asynchronous operations.
  25. /// To ensure the object is alive while an asynchronous operation refers
  26. /// to it, we use shared_ptr and enable_shared_from_this.
  27. /// The object will be destructed in case IntervalTimer has been destructed
  28. /// and no asynchronous operation refers to it.
  29. /// Please follow the link to get an example:
  30. /// http://think-async.com/asio/asio-1.4.8/doc/asio/tutorial/tutdaytime3.html#asio.tutorial.tutdaytime3.the_tcp_connection_class
  31. class IntervalTimerImpl :
  32. public boost::enable_shared_from_this<IntervalTimerImpl>
  33. {
  34. private:
  35. // prohibit copy
  36. IntervalTimerImpl(const IntervalTimerImpl& source);
  37. IntervalTimerImpl& operator=(const IntervalTimerImpl& source);
  38. public:
  39. IntervalTimerImpl(IOService& io_service);
  40. ~IntervalTimerImpl();
  41. void setup(const IntervalTimer::Callback& cbfunc, const long interval,
  42. const IntervalTimer::Mode& interval_mode
  43. = IntervalTimer::REPEATING);
  44. void callback(const boost::system::error_code& error);
  45. void cancel() {
  46. timer_.cancel();
  47. interval_ = 0;
  48. }
  49. long getInterval() const { return (interval_); }
  50. private:
  51. // a function to update timer_ when it expires
  52. void update();
  53. // a function to call back when timer_ expires
  54. IntervalTimer::Callback cbfunc_;
  55. // interval in milliseconds
  56. long interval_;
  57. // asio timer
  58. boost::asio::deadline_timer timer_;
  59. // Controls how the timer behaves after expiration.
  60. IntervalTimer::Mode mode_;
  61. // interval_ will be set to this value in destructor in order to detect
  62. // use-after-free type of bugs.
  63. static const long INVALIDATED_INTERVAL = -1;
  64. };
  65. IntervalTimerImpl::IntervalTimerImpl(IOService& io_service) :
  66. interval_(0), timer_(io_service.get_io_service()),
  67. mode_(IntervalTimer::REPEATING)
  68. {}
  69. IntervalTimerImpl::~IntervalTimerImpl() {
  70. interval_ = INVALIDATED_INTERVAL;
  71. }
  72. void
  73. IntervalTimerImpl::setup(const IntervalTimer::Callback& cbfunc,
  74. const long interval,
  75. const IntervalTimer::Mode& mode)
  76. {
  77. // Interval should not be less than or equal to 0.
  78. if (interval <= 0) {
  79. isc_throw(isc::BadValue, "Interval should not be less than or "
  80. "equal to 0");
  81. }
  82. // Call back function should not be empty.
  83. if (cbfunc.empty()) {
  84. isc_throw(isc::InvalidParameter, "Callback function is empty");
  85. }
  86. cbfunc_ = cbfunc;
  87. interval_ = interval;
  88. mode_ = mode;
  89. // Set initial expire time.
  90. // At this point the timer is not running yet and will not expire.
  91. // After calling IOService::run(), the timer will expire.
  92. update();
  93. }
  94. void
  95. IntervalTimerImpl::update() {
  96. try {
  97. // Update expire time to (current time + interval_).
  98. timer_.expires_from_now(boost::posix_time::millisec(interval_));
  99. // Reset timer.
  100. // Pass a function bound with a shared_ptr to this.
  101. timer_.async_wait(boost::bind(&IntervalTimerImpl::callback,
  102. shared_from_this(),
  103. boost::asio::placeholders::error));
  104. } catch (const boost::system::system_error& e) {
  105. isc_throw(isc::Unexpected, "Failed to update timer: " << e.what());
  106. } catch (const boost::bad_weak_ptr&) {
  107. // Can't happen. It means a severe internal bug.
  108. assert(0);
  109. }
  110. }
  111. void
  112. IntervalTimerImpl::callback(const boost::system::error_code& ec) {
  113. assert(interval_ != INVALIDATED_INTERVAL);
  114. if (interval_ == 0 || ec) {
  115. // timer has been canceled. Do nothing.
  116. } else {
  117. // If we should repeat, set next expire time.
  118. if (mode_ == IntervalTimer::REPEATING) {
  119. update();
  120. }
  121. // Invoke the call back function.
  122. cbfunc_();
  123. }
  124. }
  125. IntervalTimer::IntervalTimer(IOService& io_service) :
  126. impl_(new IntervalTimerImpl(io_service))
  127. {}
  128. IntervalTimer::~IntervalTimer() {
  129. // Cancel the timer to make sure cbfunc_() will not be called any more.
  130. cancel();
  131. }
  132. void
  133. IntervalTimer::setup(const Callback& cbfunc, const long interval,
  134. const IntervalTimer::Mode& mode) {
  135. return (impl_->setup(cbfunc, interval, mode));
  136. }
  137. void
  138. IntervalTimer::cancel() {
  139. impl_->cancel();
  140. }
  141. long
  142. IntervalTimer::getInterval() const {
  143. return (impl_->getInterval());
  144. }
  145. } // namespace asiolink
  146. } // namespace isc