mirror of
https://github.com/dingusdev/dingusppc.git
synced 2026-04-21 09:16:48 +00:00
Allow adding timers from timer callbacks.
This commit is contained in:
+18
-6
@@ -48,7 +48,9 @@ uint32_t TimerManager::add_oneshot_timer(uint64_t timeout, timer_cb cb)
|
||||
this->timer_queue.push(timer_desc);
|
||||
|
||||
// notify listeners about changes in the timer queue
|
||||
this->notify_timer_changes();
|
||||
if (!this->cb_active) {
|
||||
this->notify_timer_changes();
|
||||
}
|
||||
|
||||
return ti->id;
|
||||
}
|
||||
@@ -72,7 +74,9 @@ uint32_t TimerManager::add_cyclic_timer(uint64_t interval, timer_cb cb)
|
||||
this->timer_queue.push(timer_desc);
|
||||
|
||||
// notify listeners about changes in the timer queue
|
||||
this->notify_timer_changes();
|
||||
if (!this->cb_active) {
|
||||
this->notify_timer_changes();
|
||||
}
|
||||
|
||||
return ti->id;
|
||||
}
|
||||
@@ -85,7 +89,9 @@ void TimerManager::cancel_timer(uint32_t id)
|
||||
} else {
|
||||
this->timer_queue.remove_by_id(id);
|
||||
}
|
||||
this->notify_timer_changes();
|
||||
if (!this->cb_active) {
|
||||
this->notify_timer_changes();
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t TimerManager::process_timers(uint64_t time_now)
|
||||
@@ -100,8 +106,7 @@ uint64_t TimerManager::process_timers(uint64_t time_now)
|
||||
cur_timer = this->timer_queue.top().get();
|
||||
while (cur_timer->timeout_ns <= time_now ||
|
||||
cur_timer->timeout_ns <= (time_now + MIN_TIMEOUT_NS)) {
|
||||
// invoke timer callback
|
||||
cur_timer->cb();
|
||||
timer_cb cb = cur_timer->cb;
|
||||
|
||||
// re-arm cyclic timers
|
||||
if (cur_timer->interval_ns) {
|
||||
@@ -114,6 +119,13 @@ uint64_t TimerManager::process_timers(uint64_t time_now)
|
||||
this->timer_queue.pop();
|
||||
}
|
||||
|
||||
this->cb_active = true;
|
||||
|
||||
// invoke timer callback
|
||||
cb();
|
||||
|
||||
this->cb_active = false;
|
||||
|
||||
// process next timer
|
||||
if (this->timer_queue.empty()) {
|
||||
return 0ULL;
|
||||
@@ -124,4 +136,4 @@ uint64_t TimerManager::process_timers(uint64_t time_now)
|
||||
|
||||
// return time slice in nanoseconds until next timer's expiry
|
||||
return cur_timer->timeout_ns - time_now;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user