DelayQueue.hh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**********
  2. This library is free software; you can redistribute it and/or modify it under
  3. the terms of the GNU Lesser General Public License as published by the
  4. Free Software Foundation; either version 3 of the License, or (at your
  5. option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
  6. This library is distributed in the hope that it will be useful, but WITHOUT
  7. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  9. more details.
  10. You should have received a copy of the GNU Lesser General Public License
  11. along with this library; if not, write to the Free Software Foundation, Inc.,
  12. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13. **********/
  14. // Copyright (c) 1996-2019, Live Networks, Inc. All rights reserved
  15. // Delay queue
  16. // C++ header
  17. #ifndef _DELAY_QUEUE_HH
  18. #define _DELAY_QUEUE_HH
  19. #ifndef _NET_COMMON_H
  20. #include "NetCommon.h"
  21. #endif
  22. #ifdef TIME_BASE
  23. typedef TIME_BASE time_base_seconds;
  24. #else
  25. typedef long time_base_seconds;
  26. #endif
  27. ///// A "Timeval" can be either an absolute time, or a time interval /////
  28. class Timeval {
  29. public:
  30. time_base_seconds seconds() const {
  31. return fTv.tv_sec;
  32. }
  33. time_base_seconds seconds() {
  34. return fTv.tv_sec;
  35. }
  36. time_base_seconds useconds() const {
  37. return fTv.tv_usec;
  38. }
  39. time_base_seconds useconds() {
  40. return fTv.tv_usec;
  41. }
  42. int operator>=(Timeval const& arg2) const;
  43. int operator<=(Timeval const& arg2) const {
  44. return arg2 >= *this;
  45. }
  46. int operator<(Timeval const& arg2) const {
  47. return !(*this >= arg2);
  48. }
  49. int operator>(Timeval const& arg2) const {
  50. return arg2 < *this;
  51. }
  52. int operator==(Timeval const& arg2) const {
  53. return *this >= arg2 && arg2 >= *this;
  54. }
  55. int operator!=(Timeval const& arg2) const {
  56. return !(*this == arg2);
  57. }
  58. void operator+=(class DelayInterval const& arg2);
  59. void operator-=(class DelayInterval const& arg2);
  60. // returns ZERO iff arg2 >= arg1
  61. protected:
  62. Timeval(time_base_seconds seconds, time_base_seconds useconds) {
  63. fTv.tv_sec = seconds; fTv.tv_usec = useconds;
  64. }
  65. private:
  66. time_base_seconds& secs() {
  67. return (time_base_seconds&)fTv.tv_sec;
  68. }
  69. time_base_seconds& usecs() {
  70. return (time_base_seconds&)fTv.tv_usec;
  71. }
  72. struct timeval fTv;
  73. };
  74. #ifndef max
  75. inline Timeval max(Timeval const& arg1, Timeval const& arg2) {
  76. return arg1 >= arg2 ? arg1 : arg2;
  77. }
  78. #endif
  79. #ifndef min
  80. inline Timeval min(Timeval const& arg1, Timeval const& arg2) {
  81. return arg1 <= arg2 ? arg1 : arg2;
  82. }
  83. #endif
  84. class DelayInterval operator-(Timeval const& arg1, Timeval const& arg2);
  85. // returns ZERO iff arg2 >= arg1
  86. ///// DelayInterval /////
  87. class DelayInterval: public Timeval {
  88. public:
  89. DelayInterval(time_base_seconds seconds, time_base_seconds useconds)
  90. : Timeval(seconds, useconds) {}
  91. };
  92. DelayInterval operator*(short arg1, DelayInterval const& arg2);
  93. extern DelayInterval const DELAY_ZERO;
  94. extern DelayInterval const DELAY_SECOND;
  95. extern DelayInterval const DELAY_MINUTE;
  96. extern DelayInterval const DELAY_HOUR;
  97. extern DelayInterval const DELAY_DAY;
  98. ///// _EventTime /////
  99. class _EventTime: public Timeval {
  100. public:
  101. _EventTime(unsigned secondsSinceEpoch = 0,
  102. unsigned usecondsSinceEpoch = 0)
  103. // We use the Unix standard epoch: January 1, 1970
  104. : Timeval(secondsSinceEpoch, usecondsSinceEpoch) {}
  105. };
  106. _EventTime TimeNow();
  107. extern _EventTime const THE_END_OF_TIME;
  108. ///// DelayQueueEntry /////
  109. class DelayQueueEntry {
  110. public:
  111. virtual ~DelayQueueEntry();
  112. intptr_t token() {
  113. return fToken;
  114. }
  115. protected: // abstract base class
  116. DelayQueueEntry(DelayInterval delay);
  117. virtual void handleTimeout();
  118. private:
  119. friend class DelayQueue;
  120. DelayQueueEntry* fNext;
  121. DelayQueueEntry* fPrev;
  122. DelayInterval fDeltaTimeRemaining;
  123. intptr_t fToken;
  124. static intptr_t tokenCounter;
  125. };
  126. ///// DelayQueue /////
  127. class DelayQueue: public DelayQueueEntry {
  128. public:
  129. DelayQueue();
  130. virtual ~DelayQueue();
  131. void addEntry(DelayQueueEntry* newEntry); // returns a token for the entry
  132. void updateEntry(DelayQueueEntry* entry, DelayInterval newDelay);
  133. void updateEntry(intptr_t tokenToFind, DelayInterval newDelay);
  134. void removeEntry(DelayQueueEntry* entry); // but doesn't delete it
  135. DelayQueueEntry* removeEntry(intptr_t tokenToFind); // but doesn't delete it
  136. DelayInterval const& timeToNextAlarm();
  137. void handleAlarm();
  138. private:
  139. DelayQueueEntry* head() { return fNext; }
  140. DelayQueueEntry* findEntryByToken(intptr_t token);
  141. void synchronize(); // bring the 'time remaining' fields up-to-date
  142. _EventTime fLastSyncTime;
  143. };
  144. #endif