|
|
@ -29,7 +29,6 @@ std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callbac
|
|
|
|
struct CoreTiming::Event {
|
|
|
|
struct CoreTiming::Event {
|
|
|
|
s64 time;
|
|
|
|
s64 time;
|
|
|
|
u64 fifo_order;
|
|
|
|
u64 fifo_order;
|
|
|
|
std::uintptr_t user_data;
|
|
|
|
|
|
|
|
std::weak_ptr<EventType> type;
|
|
|
|
std::weak_ptr<EventType> type;
|
|
|
|
s64 reschedule_time;
|
|
|
|
s64 reschedule_time;
|
|
|
|
heap_t::handle_type handle{};
|
|
|
|
heap_t::handle_type handle{};
|
|
|
@ -67,17 +66,15 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
|
|
|
|
event_fifo_id = 0;
|
|
|
|
event_fifo_id = 0;
|
|
|
|
shutting_down = false;
|
|
|
|
shutting_down = false;
|
|
|
|
cpu_ticks = 0;
|
|
|
|
cpu_ticks = 0;
|
|
|
|
const auto empty_timed_callback = [](std::uintptr_t, u64, std::chrono::nanoseconds)
|
|
|
|
|
|
|
|
-> std::optional<std::chrono::nanoseconds> { return std::nullopt; };
|
|
|
|
|
|
|
|
ev_lost = CreateEvent("_lost_event", empty_timed_callback);
|
|
|
|
|
|
|
|
if (is_multicore) {
|
|
|
|
if (is_multicore) {
|
|
|
|
timer_thread = std::make_unique<std::jthread>(ThreadEntry, std::ref(*this));
|
|
|
|
timer_thread = std::make_unique<std::jthread>(ThreadEntry, std::ref(*this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CoreTiming::ClearPendingEvents() {
|
|
|
|
void CoreTiming::ClearPendingEvents() {
|
|
|
|
std::scoped_lock lock{basic_lock};
|
|
|
|
std::scoped_lock lock{advance_lock, basic_lock};
|
|
|
|
event_queue.clear();
|
|
|
|
event_queue.clear();
|
|
|
|
|
|
|
|
event.Set();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CoreTiming::Pause(bool is_paused) {
|
|
|
|
void CoreTiming::Pause(bool is_paused) {
|
|
|
@ -119,14 +116,12 @@ bool CoreTiming::HasPendingEvents() const {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
|
|
|
void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
|
|
|
const std::shared_ptr<EventType>& event_type,
|
|
|
|
const std::shared_ptr<EventType>& event_type, bool absolute_time) {
|
|
|
|
std::uintptr_t user_data, bool absolute_time) {
|
|
|
|
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::scoped_lock scope{basic_lock};
|
|
|
|
std::scoped_lock scope{basic_lock};
|
|
|
|
const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future};
|
|
|
|
const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future};
|
|
|
|
|
|
|
|
|
|
|
|
auto h{event_queue.emplace(
|
|
|
|
auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, event_type, 0})};
|
|
|
|
Event{next_time.count(), event_fifo_id++, user_data, event_type, 0})};
|
|
|
|
|
|
|
|
(*h).handle = h;
|
|
|
|
(*h).handle = h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -136,13 +131,13 @@ void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
|
|
|
|
void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
|
|
|
|
void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
|
|
|
|
std::chrono::nanoseconds resched_time,
|
|
|
|
std::chrono::nanoseconds resched_time,
|
|
|
|
const std::shared_ptr<EventType>& event_type,
|
|
|
|
const std::shared_ptr<EventType>& event_type,
|
|
|
|
std::uintptr_t user_data, bool absolute_time) {
|
|
|
|
bool absolute_time) {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::scoped_lock scope{basic_lock};
|
|
|
|
std::scoped_lock scope{basic_lock};
|
|
|
|
const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
|
|
|
|
const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
|
|
|
|
|
|
|
|
|
|
|
|
auto h{event_queue.emplace(Event{next_time.count(), event_fifo_id++, user_data, event_type,
|
|
|
|
auto h{event_queue.emplace(
|
|
|
|
resched_time.count()})};
|
|
|
|
Event{next_time.count(), event_fifo_id++, event_type, resched_time.count()})};
|
|
|
|
(*h).handle = h;
|
|
|
|
(*h).handle = h;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -150,14 +145,14 @@ void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
|
|
|
|
void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
|
|
|
|
std::uintptr_t user_data, bool wait) {
|
|
|
|
UnscheduleEventType type) {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::scoped_lock lk{basic_lock};
|
|
|
|
std::scoped_lock lk{basic_lock};
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<heap_t::handle_type> to_remove;
|
|
|
|
std::vector<heap_t::handle_type> to_remove;
|
|
|
|
for (auto itr = event_queue.begin(); itr != event_queue.end(); itr++) {
|
|
|
|
for (auto itr = event_queue.begin(); itr != event_queue.end(); itr++) {
|
|
|
|
const Event& e = *itr;
|
|
|
|
const Event& e = *itr;
|
|
|
|
if (e.type.lock().get() == event_type.get() && e.user_data == user_data) {
|
|
|
|
if (e.type.lock().get() == event_type.get()) {
|
|
|
|
to_remove.push_back(itr->handle);
|
|
|
|
to_remove.push_back(itr->handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -165,10 +160,12 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
|
|
|
|
for (auto h : to_remove) {
|
|
|
|
for (auto h : to_remove) {
|
|
|
|
event_queue.erase(h);
|
|
|
|
event_queue.erase(h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
event_type->sequence_number++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Force any in-progress events to finish
|
|
|
|
// Force any in-progress events to finish
|
|
|
|
if (wait) {
|
|
|
|
if (type == UnscheduleEventType::Wait) {
|
|
|
|
std::scoped_lock lk{advance_lock};
|
|
|
|
std::scoped_lock lk{advance_lock};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -208,28 +205,31 @@ std::optional<s64> CoreTiming::Advance() {
|
|
|
|
const Event& evt = event_queue.top();
|
|
|
|
const Event& evt = event_queue.top();
|
|
|
|
|
|
|
|
|
|
|
|
if (const auto event_type{evt.type.lock()}) {
|
|
|
|
if (const auto event_type{evt.type.lock()}) {
|
|
|
|
if (evt.reschedule_time == 0) {
|
|
|
|
|
|
|
|
const auto evt_user_data = evt.user_data;
|
|
|
|
|
|
|
|
const auto evt_time = evt.time;
|
|
|
|
const auto evt_time = evt.time;
|
|
|
|
|
|
|
|
const auto evt_sequence_num = event_type->sequence_number;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (evt.reschedule_time == 0) {
|
|
|
|
event_queue.pop();
|
|
|
|
event_queue.pop();
|
|
|
|
|
|
|
|
|
|
|
|
basic_lock.unlock();
|
|
|
|
basic_lock.unlock();
|
|
|
|
|
|
|
|
|
|
|
|
event_type->callback(
|
|
|
|
event_type->callback(
|
|
|
|
evt_user_data, evt_time,
|
|
|
|
evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time});
|
|
|
|
std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
basic_lock.lock();
|
|
|
|
basic_lock.lock();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
basic_lock.unlock();
|
|
|
|
basic_lock.unlock();
|
|
|
|
|
|
|
|
|
|
|
|
const auto new_schedule_time{event_type->callback(
|
|
|
|
const auto new_schedule_time{event_type->callback(
|
|
|
|
evt.user_data, evt.time,
|
|
|
|
evt_time, std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt_time})};
|
|
|
|
std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
basic_lock.lock();
|
|
|
|
basic_lock.lock();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (evt_sequence_num != event_type->sequence_number) {
|
|
|
|
|
|
|
|
// Heap handle is invalidated after external modification.
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const auto next_schedule_time{new_schedule_time.has_value()
|
|
|
|
const auto next_schedule_time{new_schedule_time.has_value()
|
|
|
|
? new_schedule_time.value().count()
|
|
|
|
? new_schedule_time.value().count()
|
|
|
|
: evt.reschedule_time};
|
|
|
|
: evt.reschedule_time};
|
|
|
@ -241,8 +241,8 @@ std::optional<s64> CoreTiming::Advance() {
|
|
|
|
next_time = pause_end_time + next_schedule_time;
|
|
|
|
next_time = pause_end_time + next_schedule_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.user_data,
|
|
|
|
event_queue.update(evt.handle, Event{next_time, event_fifo_id++, evt.type,
|
|
|
|
evt.type, next_schedule_time, evt.handle});
|
|
|
|
next_schedule_time, evt.handle});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|