2024-01-23 16:23:11 +01:00
|
|
|
#ifndef _timer_H_
|
|
|
|
#define _timer_H_
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
namespace sc {
|
|
|
|
|
|
|
|
class timer {
|
|
|
|
static uint64_t next_id;
|
|
|
|
static std::mutex mutex;
|
|
|
|
|
2024-01-23 20:43:39 +01:00
|
|
|
uint64_t _id {};
|
|
|
|
double _time {};
|
|
|
|
bool _repeat {};
|
2024-01-24 09:03:30 +01:00
|
|
|
void (*_expired_func)(const timer& self);
|
2024-01-24 09:19:34 +01:00
|
|
|
mutable void* _context {};
|
|
|
|
timer_t _tid;
|
2024-01-23 20:43:39 +01:00
|
|
|
|
2024-01-23 16:23:11 +01:00
|
|
|
public:
|
2024-01-24 08:57:45 +01:00
|
|
|
timer(double time,
|
2024-01-23 20:43:39 +01:00
|
|
|
bool repeat,
|
2024-01-24 09:03:30 +01:00
|
|
|
void(*expired_func)(const timer&),
|
2024-01-24 08:57:45 +01:00
|
|
|
void* context = nullptr);
|
2024-01-23 16:23:11 +01:00
|
|
|
~timer();
|
|
|
|
|
2024-01-23 20:43:39 +01:00
|
|
|
uint64_t id() const { return _id; }
|
2024-01-23 19:05:20 +01:00
|
|
|
double time() const { return _time; }
|
2024-01-23 16:23:11 +01:00
|
|
|
bool repeat() const { return _repeat; }
|
2024-01-24 08:57:45 +01:00
|
|
|
void* context() const { return _context; }
|
2024-01-23 20:43:39 +01:00
|
|
|
|
|
|
|
bool is_armed() const;
|
|
|
|
void time_left(struct itimerspec& cur_value) const;
|
|
|
|
double time_left() const;
|
2024-01-24 09:59:11 +01:00
|
|
|
|
|
|
|
void stop();
|
|
|
|
void start();
|
|
|
|
void start(double time, bool repeat);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void callback(union sigval);
|
|
|
|
|
|
|
|
void setup_timer(double time, bool repeat);
|
2024-01-23 16:23:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // _timer_H_
|