Make TimerMgr an IOSource

- This allows the loop to check what the next timeout is and use that as the basis for the timeout of the poll
- This commit also removes the TimerMgr::Tag type, since it causes a name clash with other code in IOSource
This commit is contained in:
Tim Wojtulewicz 2019-11-26 12:37:22 -07:00
parent 4fa3e4b9b4
commit 92bde61b78
7 changed files with 101 additions and 39 deletions

View file

@ -2,10 +2,9 @@
#pragma once
#include <string>
#include <string>
#include "PriorityQueue.h"
#include "iosource/IOSource.h"
// If you add a timer here, adjust TimerNames in Timer.cc.
enum TimerType : uint8_t {
@ -48,10 +47,10 @@ class ODesc;
class Timer : public PQ_Element {
public:
Timer(double t, TimerType arg_type) : PQ_Element(t), type(arg_type) { }
Timer(double t, TimerType arg_type) : PQ_Element(t), type(arg_type) {}
~Timer() override { }
TimerType Type() const { return (TimerType) type; }
TimerType Type() const { return type; }
// t gives the dispatch time. is_expire is true if the
// timer is being dispatched because we're expiring all
@ -65,53 +64,73 @@ protected:
TimerType type;
};
class TimerMgr {
class TimerMgr : public iosource::IOSource {
public:
virtual ~TimerMgr();
virtual void Add(Timer* timer) = 0;
// Advance the clock to time t, expiring at most max_expire timers.
// Returns number of timers expired.
/**
* Advance the clock to time t, expiring at most max_expire timers.
*
* @param t the new time.
* @param max_expire the maximum number of timers to expire.
* @return the number of timers expired.
*/
int Advance(double t, int max_expire);
// Returns the number of timers expired (so far) during the current
// or most recent advance.
/**
* Returns the number of timers expired (so far) during the current
* or most recent advance.
*/
int NumExpiredDuringCurrentAdvance() { return num_expired; }
// Expire all timers.
/**
* Expire all timers.
*/
virtual void Expire() = 0;
// Cancel() is a method separate from Remove because
// (1) Remove is protected, but, more importantly, (2) in some
// timer schemes we have wound up separating timer cancelation
// from removing it from the manager's data structures, because
// the manager lacked an efficient way to find it.
/**
* Removes a timer. Cancel() is a method separate from Remove()
* because (1) Remove is protected, but, more importantly, (2)
* in some timer schemes we have wound up separating timer
* cancelation from removing it from the manager's data structures,
* because the manager lacked an efficient way to find it.
*
* @param timer the timer to cancel
*/
void Cancel(Timer* timer) { Remove(timer); }
double Time() const { return t ? t : 1; } // 1 > 0
typedef std::string Tag;
const Tag& GetTag() const { return tag; }
const std::string& GetTag() const { return tag; }
virtual int Size() const = 0;
virtual int PeakSize() const = 0;
virtual uint64_t CumulativeNum() const = 0;
double LastTimestamp() const { return last_timestamp; }
// Returns time of last advance in global network time.
/**
* Returns time of last advance in global network time
*/
double LastAdvance() const { return last_advance; }
static unsigned int* CurrentTimers() { return current_timers; }
// IOSource API methods
virtual double GetNextTimeout() override { return -1; }
virtual void Process() override;
virtual const char* Tag() override { return fmt("TimerMgr %s", tag.c_str()); }
/**
* Performs some extra initialization on a timer manager. This shouldn't
* need to be called for managers other than the global one.
*/
void InitPostScript();
protected:
explicit TimerMgr(const Tag& arg_tag)
{
t = 0.0;
num_expired = 0;
last_advance = last_timestamp = 0;
tag = arg_tag;
}
explicit TimerMgr(const std::string& arg_tag);
virtual int DoAdvance(double t, int max_expire) = 0;
virtual void Remove(Timer* timer) = 0;
@ -119,7 +138,7 @@ protected:
double t;
double last_timestamp;
double last_advance;
Tag tag;
std::string tag;
int num_expired;
@ -128,7 +147,7 @@ protected:
class PQ_TimerMgr : public TimerMgr {
public:
explicit PQ_TimerMgr(const Tag& arg_tag);
explicit PQ_TimerMgr(const std::string& arg_tag);
~PQ_TimerMgr() override;
void Add(Timer* timer) override;
@ -137,6 +156,7 @@ public:
int Size() const override { return q->Size(); }
int PeakSize() const override { return q->PeakSize(); }
uint64_t CumulativeNum() const override { return q->CumulativeNum(); }
double GetNextTimeout() override;
protected:
int DoAdvance(double t, int max_expire) override;