dingusppc/core/timermanager.cpp

159 lines
4.2 KiB
C++
Raw Permalink Normal View History

2022-01-10 15:36:14 +00:00
/*
DingusPPC - The Experimental PowerPC Macintosh emulator
2023-08-01 15:36:29 +00:00
Copyright (C) 2018-23 divingkatae and maximum
2022-01-10 15:36:14 +00:00
(theweirdo) spatium
(Contact divingkatae#1017 or powermax#2286 on Discord for more info)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
/** Timer management system. */
#include <loguru.hpp>
#include "timermanager.h"
#include <cinttypes>
2024-03-16 20:45:28 +00:00
#include <memory>
#include <mutex>
2022-01-10 15:36:14 +00:00
TimerManager* TimerManager::timer_manager;
uint32_t TimerManager::add_oneshot_timer(uint64_t timeout, timer_cb cb)
{
TimerInfo* ti = new TimerInfo;
ti->id = ++this->id;
ti->timeout_ns = this->get_time_now() + timeout;
ti->interval_ns = 0;
ti->cb = cb;
std::shared_ptr<TimerInfo> timer_desc(ti);
// add new timer to the timer queue
this->timer_queue.push(timer_desc);
// notify listeners about changes in the timer queue
if (!this->cb_active) {
this->notify_timer_changes();
}
2022-01-10 15:36:14 +00:00
return ti->id;
}
uint32_t TimerManager::add_immediate_timer(timer_cb cb) {
TimerInfo* ti = new TimerInfo;
ti->id = ++this->id;
ti->timeout_ns = this->get_time_now();
ti->interval_ns = 0;
ti->cb = cb;
std::shared_ptr<TimerInfo> timer_desc(ti);
// add new timer to the timer queue
this->timer_queue.push(timer_desc);
// notify listeners about changes in the timer queue
if (!this->cb_active) {
this->notify_timer_changes();
}
return ti->id;
}
2023-08-01 15:36:29 +00:00
uint32_t TimerManager::add_cyclic_timer(uint64_t interval, uint64_t delay, timer_cb cb)
2022-01-10 15:36:14 +00:00
{
TimerInfo* ti = new TimerInfo;
ti->id = ++this->id;
2023-08-01 15:36:29 +00:00
ti->timeout_ns = this->get_time_now() + delay;
2022-01-10 15:36:14 +00:00
ti->interval_ns = interval;
ti->cb = cb;
std::shared_ptr<TimerInfo> timer_desc(ti);
// add new timer to the timer queue
this->timer_queue.push(timer_desc);
// notify listeners about changes in the timer queue
if (!this->cb_active) {
this->notify_timer_changes();
}
2022-01-10 15:36:14 +00:00
return ti->id;
}
2023-08-01 15:36:29 +00:00
uint32_t TimerManager::add_cyclic_timer(uint64_t interval, timer_cb cb) {
return this->add_cyclic_timer(interval, interval, cb);
}
2022-01-10 15:36:14 +00:00
void TimerManager::cancel_timer(uint32_t id)
{
2023-11-23 19:56:36 +00:00
this->timer_queue.remove_by_id(id);
if (!this->cb_active) {
this->notify_timer_changes();
}
2022-01-10 15:36:14 +00:00
}
uint64_t TimerManager::process_timers()
2022-01-10 15:36:14 +00:00
{
2023-11-23 19:56:36 +00:00
std::shared_ptr<TimerInfo> cur_timer;
uint64_t time_now = get_time_now();
2022-01-10 15:36:14 +00:00
2023-11-23 19:56:36 +00:00
{ // mtx scope
std::lock_guard<std::recursive_mutex> lk(this->timer_queue.get_mtx());
2022-01-10 15:36:14 +00:00
if (this->timer_queue.empty()) {
return 0ULL;
}
// scan for expired timers
2023-11-23 19:56:36 +00:00
cur_timer = this->timer_queue.top();
} // ] mtx scope
while (cur_timer->timeout_ns <= time_now) {
timer_cb cb = cur_timer->cb;
2022-01-10 15:36:14 +00:00
// re-arm cyclic timers
if (cur_timer->interval_ns) {
2023-11-23 19:56:36 +00:00
std::lock_guard<std::recursive_mutex> lk(this->timer_queue.get_mtx());
cur_timer->timeout_ns = time_now + cur_timer->interval_ns;
this->timer_queue.remove_by_id(cur_timer->id);
this->timer_queue.push(cur_timer);
2022-01-10 15:36:14 +00:00
} else {
// remove one-shot timers from queue
this->timer_queue.pop();
}
this->cb_active = true;
// invoke timer callback
cb();
this->cb_active = false;
2022-01-10 15:36:14 +00:00
// process next timer
2023-11-23 19:56:36 +00:00
{ // [ mtx scope
std::lock_guard<std::recursive_mutex> lk(this->timer_queue.get_mtx());
2022-01-10 15:36:14 +00:00
if (this->timer_queue.empty()) {
return 0ULL;
}
2023-11-23 19:56:36 +00:00
cur_timer = this->timer_queue.top();
} // ] mtx scope
2022-01-10 15:36:14 +00:00
}
// return time slice in nanoseconds until next timer's expiry
return cur_timer->timeout_ns - time_now;
}