49 lines
1.1 KiB
C++
49 lines
1.1 KiB
C++
#ifndef _timer_H_
|
|
#define _timer_H_
|
|
|
|
#include <cstdint>
|
|
#include <mutex>
|
|
|
|
namespace sc {
|
|
|
|
class timer {
|
|
static uint64_t next_id;
|
|
static std::mutex mutex;
|
|
|
|
uint64_t _id {};
|
|
double _time {};
|
|
bool _repeat {};
|
|
void (*_expired_func)(const timer& self);
|
|
mutable void* _context {};
|
|
timer_t _tid;
|
|
|
|
public:
|
|
timer(double time,
|
|
bool repeat,
|
|
void(*expired_func)(const timer&),
|
|
void* context = nullptr);
|
|
~timer();
|
|
|
|
uint64_t id() const { return _id; }
|
|
double time() const { return _time; }
|
|
bool repeat() const { return _repeat; }
|
|
void* context() const { return _context; }
|
|
|
|
bool is_armed() const;
|
|
void time_left(struct itimerspec& cur_value) const;
|
|
double time_left() const;
|
|
|
|
void stop();
|
|
void start();
|
|
void start(double time, bool repeat);
|
|
|
|
private:
|
|
static void callback(union sigval);
|
|
|
|
void setup_timer(double time, bool repeat);
|
|
};
|
|
|
|
}
|
|
|
|
#endif // _timer_H_
|