Add period query class
This commit is contained in:
parent
6aa89a740d
commit
5bcc35612c
18
time.cpp
18
time.cpp
@ -92,6 +92,24 @@ delta_clock::seconds (void) {
|
|||||||
return (time.curr - time.prev) / static_cast<double> (SECOND);
|
return (time.curr - time.prev) / static_cast<double> (SECOND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
util::period_query::period_query (double seconds) {
|
||||||
|
m_time.start = nanoseconds ();
|
||||||
|
m_time.period = seconds * SECOND;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool
|
||||||
|
util::period_query::poll (void) {
|
||||||
|
uint64_t now = nanoseconds ();
|
||||||
|
uint64_t diff = now - m_time.start;
|
||||||
|
if (diff < m_time.period)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
m_time.start += diff % m_time.period;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
util::rate_limiter::rate_limiter (unsigned rate):
|
util::rate_limiter::rate_limiter (unsigned rate):
|
||||||
|
14
time.hpp
14
time.hpp
@ -43,6 +43,20 @@ namespace util {
|
|||||||
} time;
|
} time;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// ------------------------------------------------------------------------
|
||||||
|
class period_query {
|
||||||
|
public:
|
||||||
|
period_query (double seconds);
|
||||||
|
|
||||||
|
bool poll (void);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
struct {
|
||||||
|
uint64_t start;
|
||||||
|
uint64_t period;
|
||||||
|
} m_time;
|
||||||
|
};
|
||||||
|
|
||||||
// ------------------------------------------------------------------------
|
// ------------------------------------------------------------------------
|
||||||
class rate_limiter {
|
class rate_limiter {
|
||||||
public:
|
public:
|
||||||
|
Loading…
Reference in New Issue
Block a user