From: Remi Gacogne Date: Tue, 22 Sep 2020 09:58:09 +0000 (+0200) Subject: rec: Better const-ness when dealing with timestamps X-Git-Tag: auth-4.4.0-alpha1~12^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c95554f36f97cbf3bc1d3a3e84c3fdab7ca32ff0;p=thirdparty%2Fpdns.git rec: Better const-ness when dealing with timestamps --- diff --git a/pdns/lwres.hh b/pdns/lwres.hh index 36a6d714fc..2e813bd8cb 100644 --- a/pdns/lwres.hh +++ b/pdns/lwres.hh @@ -47,7 +47,7 @@ int asendto(const char *data, size_t len, int flags, const ComboAddress& ip, uint16_t id, const DNSName& domain, uint16_t qtype, int* fd); int arecvfrom(std::string& packet, int flags, const ComboAddress& ip, size_t *d_len, uint16_t id, - const DNSName& domain, uint16_t qtype, int fd, struct timeval* now); + const DNSName& domain, uint16_t qtype, int fd, const struct timeval* now); class LWResException : public PDNSException { diff --git a/pdns/misc.hh b/pdns/misc.hh index 314764d0c3..488441912b 100644 --- a/pdns/misc.hh +++ b/pdns/misc.hh @@ -196,8 +196,12 @@ public: DTime & operator=(const DTime &dt) = default; time_t time(); inline void set(); //!< Reset the timer - inline int udiff(); //!< Return the number of microseconds since the timer was last set. - inline int udiffNoReset(); //!< Return the number of microseconds since the timer was last set. + inline int udiff(bool reset = true); //!< Return the number of microseconds since the timer was last set. + + int udiffNoReset() //!< Return the number of microseconds since the timer was last set. + { + return udiff(false); + } void setTimeval(const struct timeval& tv) { d_set=tv; @@ -215,19 +219,17 @@ inline void DTime::set() gettimeofday(&d_set,0); } -inline int DTime::udiff() -{ - int res=udiffNoReset(); - gettimeofday(&d_set,0); - return res; -} - -inline int DTime::udiffNoReset() +inline int DTime::udiff(bool reset) { struct timeval now; - gettimeofday(&now,0); + int ret=1000000*(now.tv_sec-d_set.tv_sec)+(now.tv_usec-d_set.tv_usec); + + if (reset) { + d_set = now; + } + return ret; } diff --git a/pdns/mtasker.cc b/pdns/mtasker.cc index 2d01f12f27..7afc7d4012 100644 --- a/pdns/mtasker.cc +++ b/pdns/mtasker.cc @@ -170,7 +170,7 @@ int main() \return returns -1 in case of error, 0 in case of timeout, 1 in case of an answer */ -templateint MTasker::waitEvent(EventKey &key, EventVal *val, unsigned int timeoutMsec, struct timeval* now) +templateint MTasker::waitEvent(EventKey &key, EventVal *val, unsigned int timeoutMsec, const struct timeval* now) { if(d_waiters.count(key)) { // there was already an exact same waiter return -1; @@ -301,7 +301,7 @@ templatevoid MTasker::makeThread(tfunc_t *start, \return Returns if there is more work scheduled and recalling schedule now would be useful */ -templatebool MTasker::schedule(struct timeval* now) +templatebool MTasker::schedule(const struct timeval* now) { if(!d_runQueue.empty()) { d_tid=d_runQueue.front(); diff --git a/pdns/mtasker.hh b/pdns/mtasker.hh index bff0eca0ff..ed6b02d0c5 100644 --- a/pdns/mtasker.hh +++ b/pdns/mtasker.hh @@ -119,12 +119,12 @@ public: } typedef void tfunc_t(void *); //!< type of the pointer that starts a thread - int waitEvent(EventKey &key, EventVal *val=0, unsigned int timeoutMsec=0, struct timeval* now=0); + int waitEvent(EventKey &key, EventVal *val=nullptr, unsigned int timeoutMsec=0, const struct timeval* now=nullptr); void yield(); - int sendEvent(const EventKey& key, const EventVal* val=0); + int sendEvent(const EventKey& key, const EventVal* val=nullptr); void getEvents(std::vector& events); void makeThread(tfunc_t *start, void* val); - bool schedule(struct timeval* now=0); + bool schedule(const struct timeval* now=nullptr); bool noProcesses() const; unsigned int numProcesses() const; int getTid() const; diff --git a/pdns/pdns_recursor.cc b/pdns/pdns_recursor.cc index 2cd4a86dd6..8f1c001747 100644 --- a/pdns/pdns_recursor.cc +++ b/pdns/pdns_recursor.cc @@ -676,7 +676,7 @@ int asendto(const char *data, size_t len, int flags, // -1 is error, 0 is timeout, 1 is success int arecvfrom(std::string& packet, int flags, const ComboAddress& fromaddr, size_t *d_len, - uint16_t id, const DNSName& domain, uint16_t qtype, int fd, struct timeval* now) + uint16_t id, const DNSName& domain, uint16_t qtype, int fd, const struct timeval* now) { static optional nearMissLimit; if(!nearMissLimit)