48 static unsigned timerCallback(
unsigned interval,
void* param);
49 unsigned timerCallback2();
51 std::vector<Alarm*> alarms;
55 static volatile bool enabled;
60 volatile bool AlarmManager::enabled =
false;
62 AlarmManager::AlarmManager()
65 if (SDL_Init(SDL_INIT_TIMER) < 0) {
67 "Couldn't initialize SDL timer subsystem" <<
73 AlarmManager::~AlarmManager()
75 assert(alarms.empty());
92 alarms.push_back(&alarm);
101 static int convert(
int period)
103 return std::max(1, period / 1000);
109 alarm.period = period;
115 int64_t diff = time - alarm.time;
122 id = SDL_AddTimer(convert(period), timerCallback,
this);
127 id = SDL_AddTimer(convert(period), timerCallback,
this);
134 alarm.active =
false;
145 unsigned AlarmManager::timerCallback(
unsigned ,
void* param)
148 if (!enabled)
return 0;
150 return manager->timerCallback2();
153 unsigned AlarmManager::timerCallback2()
156 ScopedLock lock(sem);
159 int64_t earliest = std::numeric_limits<int64_t>::max();
160 for (
auto& a : alarms) {
165 int64_t left = a->time - now;
170 a->time += a->period;
171 left = a->time - now;
175 earliest = std::min(earliest, left);
181 earliest = std::min(earliest, left);
185 if (earliest != std::numeric_limits<int64_t>::max()) {
186 time = earliest + now;
190 for (
auto& a : alarms) {
191 assert(a->active ==
false); (void)a;
216 assert(!destructing);
223 manager.
start(*
this, newPeriod);
bool isPending(const Alarm &alarm)
ITER find_unguarded(ITER first, ITER last, const VAL &val)
Faster alternative to 'find' when it's guaranteed that the value will be found (if not the behavior i...
void cancel()
Cancel a previous schedule() request.
bool pending() const
Is there a pending alarm?
bool contains(ITER first, ITER last, const VAL &val)
Check if a range contains a given value, using linear search.
void registerAlarm(Alarm &alarm)
static AlarmManager & instance()
void prepareDelete()
Concrete subclasses MUST call this method in their destructor.
void unregisterAlarm(Alarm &alarm)
void start(Alarm &alarm, unsigned newPeriod)
void convert(const th_ycbcr_buffer &input, RawFrame &output)
void schedule(unsigned period)
Arrange for the alarm() method to be called after some time.
uint64_t getTime()
Get current (real) time in us.