live
DelayQueue.hh
Go to the documentation of this file.
1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16 // Copyright (c) 1996-2024, 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
28typedef TIME_BASE time_base_seconds;
29#else
30typedef long time_base_seconds;
31#endif
32
34
35class Timeval {
36public:
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
71protected:
73 fTv.tv_sec = seconds; fTv.tv_usec = useconds;
74 }
75
76private:
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
88inline Timeval max(Timeval const& arg1, Timeval const& arg2) {
89 return arg1 >= arg2 ? arg1 : arg2;
90}
91#endif
92#ifndef min
93inline Timeval min(Timeval const& arg1, Timeval const& arg2) {
94 return arg1 <= arg2 ? arg1 : arg2;
95}
96#endif
97
98class DelayInterval operator-(Timeval const& arg1, Timeval const& arg2);
99// returns ZERO iff arg2 >= arg1
100
101
103
104class DelayInterval: public Timeval {
105public:
108};
109
110DelayInterval operator*(short arg1, DelayInterval const& arg2);
111
112extern DelayInterval const DELAY_ZERO;
113extern DelayInterval const DELAY_SECOND;
114extern DelayInterval const DELAY_MINUTE;
115extern DelayInterval const DELAY_HOUR;
116extern DelayInterval const DELAY_DAY;
117
119
120class _EventTime: public Timeval {
121public:
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
130extern _EventTime const THE_END_OF_TIME;
131
132
134
136public:
138
139 intptr_t token() {
140 return fToken;
141 }
142
143protected: // abstract base class
145
146 virtual void handleTimeout();
147
148private:
149 friend class DelayQueue;
153
154 intptr_t fToken;
155};
156
158
160public:
162 virtual ~DelayQueue();
163
164 void addEntry(DelayQueueEntry* newEntry); // returns a token for the entry
166 void updateEntry(intptr_t tokenToFind, DelayInterval newDelay);
167 void removeEntry(DelayQueueEntry* entry); // but doesn't delete it
168 DelayQueueEntry* removeEntry(intptr_t tokenToFind); // but doesn't delete it
169
172
173private:
176 void synchronize(); // bring the 'time remaining' fields up-to-date
177
179};
180
181#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
intptr_t token()
Definition: DelayQueue.hh:139
DelayQueueEntry * fPrev
Definition: DelayQueue.hh:151
DelayQueueEntry(DelayInterval delay, intptr_t token)
virtual void handleTimeout()
virtual ~DelayQueueEntry()
void addEntry(DelayQueueEntry *newEntry)
virtual ~DelayQueue()
void synchronize()
DelayQueueEntry * head()
Definition: DelayQueue.hh:174
void updateEntry(DelayQueueEntry *entry, DelayInterval newDelay)
_EventTime fLastSyncTime
Definition: DelayQueue.hh:178
DelayInterval const & timeToNextAlarm()
DelayQueueEntry * findEntryByToken(intptr_t token)
void handleAlarm()
void updateEntry(intptr_t tokenToFind, DelayInterval newDelay)
DelayQueueEntry * removeEntry(intptr_t tokenToFind)
void removeEntry(DelayQueueEntry *entry)
void operator+=(class DelayInterval const &arg2)
Timeval(time_base_seconds seconds, time_base_seconds useconds)
Definition: DelayQueue.hh:72
time_base_seconds & usecs()
Definition: DelayQueue.hh:80
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
int operator<(Timeval const &arg2) const
Definition: DelayQueue.hh:54
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
time_base_seconds & secs()
Definition: DelayQueue.hh:77
int operator==(Timeval const &arg2) const
Definition: DelayQueue.hh:60
_EventTime(unsigned secondsSinceEpoch=0, unsigned usecondsSinceEpoch=0)
Definition: DelayQueue.hh:122