live
DelayQueue.hh
Go to the documentation of this file.
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 
7 This library is distributed in the hope that it will be useful, but WITHOUT
8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10 more details.
11 
12 You should have received a copy of the GNU Lesser General Public License
13 along with this library; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 **********/
16  // Copyright (c) 1996-2021, Live Networks, Inc. All rights reserved
17 // Delay queue
18 // C++ header
19 
20 #ifndef _DELAY_QUEUE_HH
21 #define _DELAY_QUEUE_HH
22 
23 #ifndef _NET_COMMON_H
24 #include "NetCommon.h"
25 #endif
26 
27 #ifdef TIME_BASE
28 typedef TIME_BASE time_base_seconds;
29 #else
30 typedef long time_base_seconds;
31 #endif
32 
34 
35 class Timeval {
36 public:
38  return fTv.tv_sec;
39  }
41  return fTv.tv_sec;
42  }
44  return fTv.tv_usec;
45  }
47  return fTv.tv_usec;
48  }
49 
50  int operator>=(Timeval const& arg2) const;
51  int operator<=(Timeval const& arg2) const {
52  return arg2 >= *this;
53  }
54  int operator<(Timeval const& arg2) const {
55  return !(*this >= arg2);
56  }
57  int operator>(Timeval const& arg2) const {
58  return arg2 < *this;
59  }
60  int operator==(Timeval const& arg2) const {
61  return *this >= arg2 && arg2 >= *this;
62  }
63  int operator!=(Timeval const& arg2) const {
64  return !(*this == arg2);
65  }
66 
67  void operator+=(class DelayInterval const& arg2);
68  void operator-=(class DelayInterval const& arg2);
69  // returns ZERO iff arg2 >= arg1
70 
71 protected:
73  fTv.tv_sec = seconds; fTv.tv_usec = useconds;
74  }
75 
76 private:
78  return (time_base_seconds&)fTv.tv_sec;
79  }
81  return (time_base_seconds&)fTv.tv_usec;
82  }
83 
84  struct timeval fTv;
85 };
86 
87 #ifndef max
88 inline Timeval max(Timeval const& arg1, Timeval const& arg2) {
89  return arg1 >= arg2 ? arg1 : arg2;
90 }
91 #endif
92 #ifndef min
93 inline Timeval min(Timeval const& arg1, Timeval const& arg2) {
94  return arg1 <= arg2 ? arg1 : arg2;
95 }
96 #endif
97 
98 class DelayInterval operator-(Timeval const& arg1, Timeval const& arg2);
99 // returns ZERO iff arg2 >= arg1
100 
101 
103 
104 class DelayInterval: public Timeval {
105 public:
107  : Timeval(seconds, useconds) {}
108 };
109 
110 DelayInterval operator*(short arg1, DelayInterval const& arg2);
111 
112 extern DelayInterval const DELAY_ZERO;
113 extern DelayInterval const DELAY_SECOND;
114 extern DelayInterval const DELAY_MINUTE;
115 extern DelayInterval const DELAY_HOUR;
116 extern DelayInterval const DELAY_DAY;
117 
119 
120 class _EventTime: public Timeval {
121 public:
122  _EventTime(unsigned secondsSinceEpoch = 0,
123  unsigned usecondsSinceEpoch = 0)
124  // We use the Unix standard epoch: January 1, 1970
125  : Timeval(secondsSinceEpoch, usecondsSinceEpoch) {}
126 };
127 
129 
130 extern _EventTime const THE_END_OF_TIME;
131 
132 
134 
136 public:
137  virtual ~DelayQueueEntry();
138 
139  intptr_t token() {
140  return fToken;
141  }
142 
143 protected: // abstract base class
145 
146  virtual void handleTimeout();
147 
148 private:
149  friend class DelayQueue;
153 
154  intptr_t fToken;
155  static intptr_t tokenCounter;
156 };
157 
159 
161 public:
163  virtual ~DelayQueue();
164 
165  void addEntry(DelayQueueEntry* newEntry); // returns a token for the entry
166  void updateEntry(DelayQueueEntry* entry, DelayInterval newDelay);
167  void updateEntry(intptr_t tokenToFind, DelayInterval newDelay);
168  void removeEntry(DelayQueueEntry* entry); // but doesn't delete it
169  DelayQueueEntry* removeEntry(intptr_t tokenToFind); // but doesn't delete it
170 
172  void handleAlarm();
173 
174 private:
175  DelayQueueEntry* head() { return fNext; }
177  void synchronize(); // bring the 'time remaining' fields up-to-date
178 
180 };
181 
182 #endif
DelayInterval const DELAY_SECOND
DelayInterval const DELAY_ZERO
Timeval max(Timeval const &arg1, Timeval const &arg2)
Definition: DelayQueue.hh:88
DelayInterval const DELAY_DAY
DelayInterval const DELAY_HOUR
Timeval min(Timeval const &arg1, Timeval const &arg2)
Definition: DelayQueue.hh:93
_EventTime const THE_END_OF_TIME
DelayInterval operator*(short arg1, DelayInterval const &arg2)
long time_base_seconds
Definition: DelayQueue.hh:30
_EventTime TimeNow()
DelayInterval const DELAY_MINUTE
DelayInterval(time_base_seconds seconds, time_base_seconds useconds)
Definition: DelayQueue.hh:106
DelayQueueEntry * fNext
Definition: DelayQueue.hh:150
intptr_t fToken
Definition: DelayQueue.hh:154
DelayInterval fDeltaTimeRemaining
Definition: DelayQueue.hh:152
DelayQueueEntry(DelayInterval delay)
intptr_t token()
Definition: DelayQueue.hh:139
DelayQueueEntry * fPrev
Definition: DelayQueue.hh:151
virtual void handleTimeout()
virtual ~DelayQueueEntry()
static intptr_t tokenCounter
Definition: DelayQueue.hh:155
void addEntry(DelayQueueEntry *newEntry)
virtual ~DelayQueue()
DelayInterval const & timeToNextAlarm()
DelayQueueEntry * removeEntry(intptr_t tokenToFind)
void synchronize()
void updateEntry(DelayQueueEntry *entry, DelayInterval newDelay)
_EventTime fLastSyncTime
Definition: DelayQueue.hh:179
DelayQueueEntry * findEntryByToken(intptr_t token)
DelayQueueEntry * head()
Definition: DelayQueue.hh:175
void handleAlarm()
void updateEntry(intptr_t tokenToFind, DelayInterval newDelay)
void removeEntry(DelayQueueEntry *entry)
void operator+=(class DelayInterval const &arg2)
Timeval(time_base_seconds seconds, time_base_seconds useconds)
Definition: DelayQueue.hh:72
int operator<=(Timeval const &arg2) const
Definition: DelayQueue.hh:51
time_base_seconds seconds()
Definition: DelayQueue.hh:40
int operator>(Timeval const &arg2) const
Definition: DelayQueue.hh:57
void operator-=(class DelayInterval const &arg2)
int operator>=(Timeval const &arg2) const
time_base_seconds & usecs()
Definition: DelayQueue.hh:80
int operator<(Timeval const &arg2) const
Definition: DelayQueue.hh:54
time_base_seconds & secs()
Definition: DelayQueue.hh:77
struct timeval fTv
Definition: DelayQueue.hh:84
int operator!=(Timeval const &arg2) const
Definition: DelayQueue.hh:63
time_base_seconds seconds() const
Definition: DelayQueue.hh:37
time_base_seconds useconds() const
Definition: DelayQueue.hh:43
time_base_seconds useconds()
Definition: DelayQueue.hh:46
int operator==(Timeval const &arg2) const
Definition: DelayQueue.hh:60
_EventTime(unsigned secondsSinceEpoch=0, unsigned usecondsSinceEpoch=0)
Definition: DelayQueue.hh:122