Add microsecond accuracy to set_timer
parent
6b988de17d
commit
4694811eab
|
@ -34,8 +34,8 @@ timerfd_manager_t::~timerfd_manager_t()
|
||||||
|
|
||||||
void timerfd_manager_t::inc_timer(timerfd_timer_t & t)
|
void timerfd_manager_t::inc_timer(timerfd_timer_t & t)
|
||||||
{
|
{
|
||||||
t.next.tv_sec += t.millis/1000;
|
t.next.tv_sec += t.micros/1000000;
|
||||||
t.next.tv_nsec += (t.millis%1000)*1000000;
|
t.next.tv_nsec += (t.micros%1000000)*1000;
|
||||||
if (t.next.tv_nsec > 1000000000)
|
if (t.next.tv_nsec > 1000000000)
|
||||||
{
|
{
|
||||||
t.next.tv_sec++;
|
t.next.tv_sec++;
|
||||||
|
@ -44,13 +44,18 @@ void timerfd_manager_t::inc_timer(timerfd_timer_t & t)
|
||||||
}
|
}
|
||||||
|
|
||||||
int timerfd_manager_t::set_timer(uint64_t millis, bool repeat, std::function<void(int)> callback)
|
int timerfd_manager_t::set_timer(uint64_t millis, bool repeat, std::function<void(int)> callback)
|
||||||
|
{
|
||||||
|
return set_timer_us(millis*1000, repeat, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
int timerfd_manager_t::set_timer_us(uint64_t micros, bool repeat, std::function<void(int)> callback)
|
||||||
{
|
{
|
||||||
int timer_id = id++;
|
int timer_id = id++;
|
||||||
timespec start;
|
timespec start;
|
||||||
clock_gettime(CLOCK_MONOTONIC, &start);
|
clock_gettime(CLOCK_MONOTONIC, &start);
|
||||||
timers.push_back({
|
timers.push_back({
|
||||||
.id = timer_id,
|
.id = timer_id,
|
||||||
.millis = millis,
|
.micros = micros,
|
||||||
.start = start,
|
.start = start,
|
||||||
.next = start,
|
.next = start,
|
||||||
.repeat = repeat,
|
.repeat = repeat,
|
||||||
|
|
|
@ -10,7 +10,7 @@
|
||||||
struct timerfd_timer_t
|
struct timerfd_timer_t
|
||||||
{
|
{
|
||||||
int id;
|
int id;
|
||||||
uint64_t millis;
|
uint64_t micros;
|
||||||
timespec start, next;
|
timespec start, next;
|
||||||
bool repeat;
|
bool repeat;
|
||||||
std::function<void(int)> callback;
|
std::function<void(int)> callback;
|
||||||
|
@ -34,5 +34,6 @@ public:
|
||||||
timerfd_manager_t(std::function<void(int, bool, std::function<void(int, int)>)> set_fd_handler);
|
timerfd_manager_t(std::function<void(int, bool, std::function<void(int, int)>)> set_fd_handler);
|
||||||
~timerfd_manager_t();
|
~timerfd_manager_t();
|
||||||
int set_timer(uint64_t millis, bool repeat, std::function<void(int)> callback);
|
int set_timer(uint64_t millis, bool repeat, std::function<void(int)> callback);
|
||||||
|
int set_timer_us(uint64_t micros, bool repeat, std::function<void(int)> callback);
|
||||||
void clear_timer(int timer_id);
|
void clear_timer(int timer_id);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue