timers.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. Copyright (c) 2007-2016 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "precompiled.hpp"
  25. #include "timers.hpp"
  26. #include "err.hpp"
  27. #include <algorithm>
  28. zmq::timers_t::timers_t () : _tag (0xCAFEDADA), _next_timer_id (0)
  29. {
  30. }
  31. zmq::timers_t::~timers_t ()
  32. {
  33. // Mark the timers as dead
  34. _tag = 0xdeadbeef;
  35. }
  36. bool zmq::timers_t::check_tag () const
  37. {
  38. return _tag == 0xCAFEDADA;
  39. }
  40. int zmq::timers_t::add (size_t interval_, timers_timer_fn handler_, void *arg_)
  41. {
  42. if (handler_ == NULL) {
  43. errno = EFAULT;
  44. return -1;
  45. }
  46. uint64_t when = _clock.now_ms () + interval_;
  47. timer_t timer = {++_next_timer_id, interval_, handler_, arg_};
  48. _timers.insert (timersmap_t::value_type (when, timer));
  49. return timer.timer_id;
  50. }
  51. struct zmq::timers_t::match_by_id
  52. {
  53. match_by_id (int timer_id_) : _timer_id (timer_id_) {}
  54. bool operator() (timersmap_t::value_type const &entry_) const
  55. {
  56. return entry_.second.timer_id == _timer_id;
  57. }
  58. private:
  59. int _timer_id;
  60. };
  61. int zmq::timers_t::cancel (int timer_id_)
  62. {
  63. // check first if timer exists at all
  64. if (_timers.end ()
  65. == std::find_if (_timers.begin (), _timers.end (),
  66. match_by_id (timer_id_))) {
  67. errno = EINVAL;
  68. return -1;
  69. }
  70. // check if timer was already canceled
  71. if (_cancelled_timers.count (timer_id_)) {
  72. errno = EINVAL;
  73. return -1;
  74. }
  75. _cancelled_timers.insert (timer_id_);
  76. return 0;
  77. }
  78. int zmq::timers_t::set_interval (int timer_id_, size_t interval_)
  79. {
  80. const timersmap_t::iterator end = _timers.end ();
  81. const timersmap_t::iterator it =
  82. std::find_if (_timers.begin (), end, match_by_id (timer_id_));
  83. if (it != end) {
  84. timer_t timer = it->second;
  85. timer.interval = interval_;
  86. uint64_t when = _clock.now_ms () + interval_;
  87. _timers.erase (it);
  88. _timers.insert (timersmap_t::value_type (when, timer));
  89. return 0;
  90. }
  91. errno = EINVAL;
  92. return -1;
  93. }
  94. int zmq::timers_t::reset (int timer_id_)
  95. {
  96. const timersmap_t::iterator end = _timers.end ();
  97. const timersmap_t::iterator it =
  98. std::find_if (_timers.begin (), end, match_by_id (timer_id_));
  99. if (it != end) {
  100. timer_t timer = it->second;
  101. uint64_t when = _clock.now_ms () + timer.interval;
  102. _timers.erase (it);
  103. _timers.insert (timersmap_t::value_type (when, timer));
  104. return 0;
  105. }
  106. errno = EINVAL;
  107. return -1;
  108. }
  109. long zmq::timers_t::timeout ()
  110. {
  111. const uint64_t now = _clock.now_ms ();
  112. long res = -1;
  113. const timersmap_t::iterator begin = _timers.begin ();
  114. const timersmap_t::iterator end = _timers.end ();
  115. timersmap_t::iterator it = begin;
  116. for (; it != end; ++it) {
  117. if (0 == _cancelled_timers.erase (it->second.timer_id)) {
  118. // Live timer, lets return the timeout
  119. res = std::max (static_cast<long> (it->first - now), 0l);
  120. break;
  121. }
  122. }
  123. // Remove timed-out timers
  124. _timers.erase (begin, it);
  125. return res;
  126. }
  127. int zmq::timers_t::execute ()
  128. {
  129. const uint64_t now = _clock.now_ms ();
  130. const timersmap_t::iterator begin = _timers.begin ();
  131. const timersmap_t::iterator end = _timers.end ();
  132. timersmap_t::iterator it = _timers.begin ();
  133. for (; it != end; ++it) {
  134. if (0 == _cancelled_timers.erase (it->second.timer_id)) {
  135. // Timer is not cancelled
  136. // Map is ordered, if we have to wait for current timer we can stop.
  137. if (it->first > now)
  138. break;
  139. const timer_t &timer = it->second;
  140. timer.handler (timer.timer_id, timer.arg);
  141. _timers.insert (
  142. timersmap_t::value_type (now + timer.interval, timer));
  143. }
  144. }
  145. _timers.erase (begin, it);
  146. return 0;
  147. }