]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/delaypipe.cc
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "delaypipe.hh"
26 #include "threadname.hh"
29 ObjectPipe
<T
>::ObjectPipe()
36 ObjectPipe
<T
>::~ObjectPipe()
44 void ObjectPipe
<T
>::close()
48 ::close(d_fds
[1]); // the writing side
53 void ObjectPipe
<T
>::write(T
& t
)
56 if(::write(d_fds
[1], &ptr
, sizeof(ptr
)) != sizeof(ptr
)) {
63 bool ObjectPipe
<T
>::read(T
* t
)
66 int ret
= ::read(d_fds
[0], &ptr
, sizeof(ptr
));
72 if(ret
!= sizeof(ptr
))
73 throw std::runtime_error("Partial read, should not happen");
80 int ObjectPipe
<T
>::readTimeout(T
* t
, double msec
)
84 int ret
= waitForData(d_fds
[0], 0, 1000*msec
);
86 unixDie("waiting for data in object pipe");
90 ret
= ::read(d_fds
[0], &ptr
, sizeof(ptr
)); // this is BLOCKING!
96 if(ret
!= sizeof(ptr
))
97 throw std::runtime_error("Partial read, should not happen 2");
105 DelayPipe
<T
>::DelayPipe() : d_thread(&DelayPipe
<T
>::worker
, this)
110 void DelayPipe
<T
>::gettime(struct timespec
* ts
)
117 void DelayPipe
<T
>::submit(T
& t
, int msec
)
121 now
.tv_nsec
+= msec
*1e6
;
122 while(now
.tv_nsec
> 1e9
) {
131 DelayPipe
<T
>::~DelayPipe()
140 void DelayPipe
<T
>::worker()
142 setThreadName("dnsdist/delayPi");
145 /* this code is slightly too subtle, but I don't see how it could be any simpler.
146 So we have a set of work to do, and we need to wait until the time arrives to do it.
147 Simultaneously new work might come in. So we try to combine both of these things by
148 setting a timeout on listening to the pipe over which new work comes in. This timeout
149 is equal to the wait until the first thing that needs to be done.
151 Two additional cases exist: we have no work to wait for, so we can wait infinitely long.
152 The other special case is that the first we have to do.. is in the past, so we need to do it
156 double delay
=-1; // infinite
158 if(!d_work
.empty()) {
160 delay
=1000*tsdelta(d_work
.begin()->first
, now
);
162 delay
=0; // don't wait - we have work that is late already!
166 int ret
= d_pipe
.readTimeout(&c
, delay
);
167 if(ret
> 0) { // we got an object
168 d_work
.insert(make_pair(c
.when
, c
.what
));
170 else if(ret
==0) { // EOF
181 for(auto iter
= d_work
.begin() ; iter
!= d_work
.end(); ) { // do the needful
182 if(cmp(iter
->first
, now
)) {
184 d_work
.erase(iter
++);