Changes: * Implement patty_timer type to store both a struct timeval and a bitfield for indicating timer state; currently, this only holds the PATTY_TIMER_RUNNING flag to indicate whether a timer has been stopped (irrespective of its "expired" status) * Remove 'target' argument from patty_timer_start(); instead, rely on select() polling every 1s to cause events to trigger at a regular interval * Make patty_timer_expired() return false if PATTY_TIMER_RUNNING is not set * Implement patty_timer_stop() to clear the PATTY_TIMER_RUNNING flag of a timer * Make patty_timer_clear() set all fields to zero, including clearing the PATTY_TIMER_RUNNING flag
42 lines
994 B
C
42 lines
994 B
C
#include <patty/timer.h>
|
|
|
|
int patty_timer_running(patty_timer *timer) {
|
|
return (timer->flags & PATTY_TIMER_RUNNING)? 1: 0;
|
|
}
|
|
|
|
int patty_timer_expired(patty_timer *timer) {
|
|
return (timer->flags & PATTY_TIMER_RUNNING) && timer->t.tv_sec <= 0? 1: 0;
|
|
}
|
|
|
|
void patty_timer_clear(patty_timer *timer) {
|
|
timer->t.tv_sec = 0;
|
|
timer->t.tv_usec = 0;
|
|
|
|
timer->flags &= ~PATTY_TIMER_RUNNING;
|
|
}
|
|
|
|
void patty_timer_start(patty_timer *timer,
|
|
time_t ms) {
|
|
timer->t.tv_sec = ms / 1000;
|
|
timer->t.tv_usec = (ms % 1000) * 1000;
|
|
|
|
timer->flags |= PATTY_TIMER_RUNNING;
|
|
}
|
|
|
|
void patty_timer_stop(patty_timer *timer) {
|
|
timer->flags &= ~PATTY_TIMER_RUNNING;
|
|
}
|
|
|
|
void patty_timer_tick(patty_timer *timer,
|
|
struct timeval *elapsed) {
|
|
struct timeval res;
|
|
|
|
if (!(timer->flags & PATTY_TIMER_RUNNING)) {
|
|
return;
|
|
}
|
|
|
|
timersub(&timer->t, elapsed, &res);
|
|
|
|
timer->t.tv_sec = res.tv_sec;
|
|
timer->t.tv_usec = res.tv_usec;
|
|
}
|