From 320194342357b6989a9f0e9b5fd9defa9cf6b697 Mon Sep 17 00:00:00 2001 From: Tobias Date: Sun, 6 Nov 2022 02:24:45 +0100 Subject: [PATCH] Port yuzu-emu/yuzu#4437: "core_timing: Make use of uintptr_t to represent user_data" (#5499) Co-authored-by: LC --- src/core/cheats/cheats.cpp | 2 +- src/core/cheats/cheats.h | 2 +- src/core/core_timing.cpp | 14 +++++++------- src/core/core_timing.h | 13 +++++++------ src/core/hle/kernel/shared_page.cpp | 2 +- src/core/hle/kernel/shared_page.h | 2 +- src/core/hle/service/cam/cam.cpp | 9 +++++---- src/core/hle/service/hid/hid.cpp | 24 ++++++++++++------------ src/core/hle/service/hid/hid.h | 6 +++--- src/core/hle/service/ir/ir_rst.cpp | 7 ++++--- src/core/hle/service/ir/ir_rst.h | 2 +- src/core/hle/service/mic_u.cpp | 8 ++++---- src/core/hle/service/nwm/nwm_uds.cpp | 7 ++++--- src/core/hle/service/nwm/nwm_uds.h | 2 +- src/core/hw/gpu.cpp | 2 +- src/tests/core/core_timing.cpp | 18 +++++++++--------- 16 files changed, 62 insertions(+), 58 deletions(-) diff --git a/src/core/cheats/cheats.cpp b/src/core/cheats/cheats.cpp index 440047ffe..9d9e94121 100644 --- a/src/core/cheats/cheats.cpp +++ b/src/core/cheats/cheats.cpp @@ -102,7 +102,7 @@ void CheatEngine::LoadCheatFile() { } } -void CheatEngine::RunCallback([[maybe_unused]] u64 userdata, s64 cycles_late) { +void CheatEngine::RunCallback([[maybe_unused]] std::uintptr_t user_data, s64 cycles_late) { { std::shared_lock lock(cheats_list_mutex); for (auto& cheat : cheats_list) { diff --git a/src/core/cheats/cheats.h b/src/core/cheats/cheats.h index c45c1044f..813263236 100644 --- a/src/core/cheats/cheats.h +++ b/src/core/cheats/cheats.h @@ -35,7 +35,7 @@ public: private: void LoadCheatFile(); - void RunCallback(u64 userdata, s64 cycles_late); + void RunCallback(std::uintptr_t user_data, s64 cycles_late); std::vector> cheats_list; mutable std::shared_mutex cheats_list_mutex; Core::TimingEventType* event; diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index bf9adebf5..a865bd4aa 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -47,8 +47,8 @@ TimingEventType* Timing::RegisterEvent(const std::string& name, TimedCallback ca return event_type; } -void Timing::ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_type, u64 userdata, - std::size_t core_id) { +void Timing::ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_type, + std::uintptr_t user_data, std::size_t core_id) { if (event_queue_locked) { return; } @@ -69,22 +69,22 @@ void Timing::ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_ timer->ForceExceptionCheck(cycles_into_future); timer->event_queue.emplace_back( - Event{timeout, timer->event_fifo_id++, userdata, event_type}); + Event{timeout, timer->event_fifo_id++, user_data, event_type}); std::push_heap(timer->event_queue.begin(), timer->event_queue.end(), std::greater<>()); } else { timer->ts_queue.Push(Event{static_cast(timer->GetTicks() + cycles_into_future), 0, - userdata, event_type}); + user_data, event_type}); } } -void Timing::UnscheduleEvent(const TimingEventType* event_type, u64 userdata) { +void Timing::UnscheduleEvent(const TimingEventType* event_type, std::uintptr_t user_data) { if (event_queue_locked) { return; } for (auto timer : timers) { auto itr = std::remove_if( timer->event_queue.begin(), timer->event_queue.end(), - [&](const Event& e) { return e.type == event_type && e.userdata == userdata; }); + [&](const Event& e) { return e.type == event_type && e.user_data == user_data; }); // Removing random items breaks the invariant so we have to re-establish it. if (itr != timer->event_queue.end()) { @@ -215,7 +215,7 @@ void Timing::Timer::Advance() { std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); event_queue.pop_back(); if (evt.type->callback != nullptr) { - evt.type->callback(evt.userdata, static_cast(executed_ticks - evt.time)); + evt.type->callback(evt.user_data, static_cast(executed_ticks - evt.time)); } else { LOG_ERROR(Core, "Event '{}' has no callback", *evt.type->name); } diff --git a/src/core/core_timing.h b/src/core/core_timing.h index 11ef4d35b..3fe99bf67 100644 --- a/src/core/core_timing.h +++ b/src/core/core_timing.h @@ -128,7 +128,7 @@ constexpr u64 cyclesToMs(s64 cycles) { namespace Core { -using TimedCallback = std::function; +using TimedCallback = std::function; struct TimingEventType { TimedCallback callback; @@ -141,7 +141,7 @@ public: struct Event { s64 time; u64 fifo_order; - u64 userdata; + std::uintptr_t user_data; const TimingEventType* type; bool operator>(const Event& right) const; @@ -152,7 +152,7 @@ public: void save(Archive& ar, const unsigned int) const { ar& time; ar& fifo_order; - ar& userdata; + ar& user_data; std::string name = *(type->name); ar << name; } @@ -161,7 +161,7 @@ public: void load(Archive& ar, const unsigned int) { ar& time; ar& fifo_order; - ar& userdata; + ar& user_data; std::string name; ar >> name; type = Global().RegisterEvent(name, nullptr); @@ -265,10 +265,11 @@ public: */ TimingEventType* RegisterEvent(const std::string& name, TimedCallback callback); - void ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_type, u64 userdata = 0, + void ScheduleEvent(s64 cycles_into_future, const TimingEventType* event_type, + std::uintptr_t user_data = 0, std::size_t core_id = std::numeric_limits::max()); - void UnscheduleEvent(const TimingEventType* event_type, u64 userdata); + void UnscheduleEvent(const TimingEventType* event_type, std::uintptr_t user_data); /// We only permit one event of each type in the queue at a time. void RemoveEvent(const TimingEventType* event_type); diff --git a/src/core/hle/kernel/shared_page.cpp b/src/core/hle/kernel/shared_page.cpp index 87189e26e..443ee930d 100644 --- a/src/core/hle/kernel/shared_page.cpp +++ b/src/core/hle/kernel/shared_page.cpp @@ -108,7 +108,7 @@ u64 Handler::GetSystemTime() const { return console_time; } -void Handler::UpdateTimeCallback(u64 userdata, int cycles_late) { +void Handler::UpdateTimeCallback(std::uintptr_t user_data, int cycles_late) { DateTime& date_time = shared_page.date_time_counter % 2 ? shared_page.date_time_0 : shared_page.date_time_1; diff --git a/src/core/hle/kernel/shared_page.h b/src/core/hle/kernel/shared_page.h index 9cd579d52..531cb2a28 100644 --- a/src/core/hle/kernel/shared_page.h +++ b/src/core/hle/kernel/shared_page.h @@ -114,7 +114,7 @@ public: private: u64 GetSystemTime() const; - void UpdateTimeCallback(u64 userdata, int cycles_late); + void UpdateTimeCallback(std::uintptr_t user_data, int cycles_late); Core::Timing& timing; Core::TimingEventType* update_time_event; std::chrono::seconds init_time; diff --git a/src/core/hle/service/cam/cam.cpp b/src/core/hle/service/cam/cam.cpp index 77be92a53..abd3a7ee5 100644 --- a/src/core/hle/service/cam/cam.cpp +++ b/src/core/hle/service/cam/cam.cpp @@ -1118,11 +1118,12 @@ Module::Module(Core::System& system) : system(system) { system.Kernel().CreateEvent(ResetType::OneShot, "CAM::vsync_interrupt_event"); } completion_event_callback = system.CoreTiming().RegisterEvent( - "CAM::CompletionEventCallBack", - [this](u64 userdata, s64 cycles_late) { CompletionEventCallBack(userdata, cycles_late); }); + "CAM::CompletionEventCallBack", [this](std::uintptr_t user_data, s64 cycles_late) { + CompletionEventCallBack(user_data, cycles_late); + }); vsync_interrupt_event_callback = system.CoreTiming().RegisterEvent( - "CAM::VsyncInterruptEventCallBack", [this](u64 userdata, s64 cycles_late) { - VsyncInterruptEventCallBack(userdata, cycles_late); + "CAM::VsyncInterruptEventCallBack", [this](std::uintptr_t user_data, s64 cycles_late) { + VsyncInterruptEventCallBack(user_data, cycles_late); }); } diff --git a/src/core/hle/service/hid/hid.cpp b/src/core/hle/service/hid/hid.cpp index c59826551..aba6c8291 100644 --- a/src/core/hle/service/hid/hid.cpp +++ b/src/core/hle/service/hid/hid.cpp @@ -105,7 +105,7 @@ void Module::LoadInputDevices() { } } -void Module::UpdatePadCallback(u64 userdata, s64 cycles_late) { +void Module::UpdatePadCallback(std::uintptr_t user_data, s64 cycles_late) { SharedMem* mem = reinterpret_cast(shared_mem->GetPointer()); if (is_device_reload_pending.exchange(false)) @@ -225,7 +225,7 @@ void Module::UpdatePadCallback(u64 userdata, s64 cycles_late) { system.CoreTiming().ScheduleEvent(pad_update_ticks - cycles_late, pad_update_event); } -void Module::UpdateAccelerometerCallback(u64 userdata, s64 cycles_late) { +void Module::UpdateAccelerometerCallback(std::uintptr_t user_data, s64 cycles_late) { SharedMem* mem = reinterpret_cast(shared_mem->GetPointer()); mem->accelerometer.index = next_accelerometer_index; @@ -270,7 +270,7 @@ void Module::UpdateAccelerometerCallback(u64 userdata, s64 cycles_late) { accelerometer_update_event); } -void Module::UpdateGyroscopeCallback(u64 userdata, s64 cycles_late) { +void Module::UpdateGyroscopeCallback(std::uintptr_t user_data, s64 cycles_late) { SharedMem* mem = reinterpret_cast(shared_mem->GetPointer()); mem->gyroscope.index = next_gyroscope_index; @@ -438,17 +438,17 @@ Module::Module(Core::System& system) : system(system) { // Register update callbacks Core::Timing& timing = system.CoreTiming(); - pad_update_event = - timing.RegisterEvent("HID::UpdatePadCallback", [this](u64 userdata, s64 cycles_late) { - UpdatePadCallback(userdata, cycles_late); - }); + pad_update_event = timing.RegisterEvent("HID::UpdatePadCallback", + [this](std::uintptr_t user_data, s64 cycles_late) { + UpdatePadCallback(user_data, cycles_late); + }); accelerometer_update_event = timing.RegisterEvent( - "HID::UpdateAccelerometerCallback", [this](u64 userdata, s64 cycles_late) { - UpdateAccelerometerCallback(userdata, cycles_late); + "HID::UpdateAccelerometerCallback", [this](std::uintptr_t user_data, s64 cycles_late) { + UpdateAccelerometerCallback(user_data, cycles_late); }); - gyroscope_update_event = - timing.RegisterEvent("HID::UpdateGyroscopeCallback", [this](u64 userdata, s64 cycles_late) { - UpdateGyroscopeCallback(userdata, cycles_late); + gyroscope_update_event = timing.RegisterEvent( + "HID::UpdateGyroscopeCallback", [this](std::uintptr_t user_data, s64 cycles_late) { + UpdateGyroscopeCallback(user_data, cycles_late); }); timing.ScheduleEvent(pad_update_ticks, pad_update_event); diff --git a/src/core/hle/service/hid/hid.h b/src/core/hle/service/hid/hid.h index b364c4be8..c0f74ced1 100644 --- a/src/core/hle/service/hid/hid.h +++ b/src/core/hle/service/hid/hid.h @@ -307,9 +307,9 @@ public: private: void LoadInputDevices(); - void UpdatePadCallback(u64 userdata, s64 cycles_late); - void UpdateAccelerometerCallback(u64 userdata, s64 cycles_late); - void UpdateGyroscopeCallback(u64 userdata, s64 cycles_late); + void UpdatePadCallback(std::uintptr_t user_data, s64 cycles_late); + void UpdateAccelerometerCallback(std::uintptr_t user_data, s64 cycles_late); + void UpdateGyroscopeCallback(std::uintptr_t user_data, s64 cycles_late); Core::System& system; diff --git a/src/core/hle/service/ir/ir_rst.cpp b/src/core/hle/service/ir/ir_rst.cpp index bd6c64af3..7372bec5e 100644 --- a/src/core/hle/service/ir/ir_rst.cpp +++ b/src/core/hle/service/ir/ir_rst.cpp @@ -66,7 +66,7 @@ void IR_RST::UnloadInputDevices() { c_stick = nullptr; } -void IR_RST::UpdateCallback(u64 userdata, s64 cycles_late) { +void IR_RST::UpdateCallback(std::uintptr_t user_data, s64 cycles_late) { SharedMem* mem = reinterpret_cast(shared_memory->GetPointer()); if (is_device_reload_pending.exchange(false)) @@ -175,8 +175,9 @@ IR_RST::IR_RST(Core::System& system) : ServiceFramework("ir:rst", 1), system(sys update_event = system.Kernel().CreateEvent(ResetType::OneShot, "IRRST:UpdateEvent"); update_callback_id = system.CoreTiming().RegisterEvent( - "IRRST:UpdateCallBack", - [this](u64 userdata, s64 cycles_late) { UpdateCallback(userdata, cycles_late); }); + "IRRST:UpdateCallBack", [this](std::uintptr_t user_data, s64 cycles_late) { + UpdateCallback(user_data, cycles_late); + }); static const FunctionInfo functions[] = { {0x00010000, &IR_RST::GetHandles, "GetHandles"}, diff --git a/src/core/hle/service/ir/ir_rst.h b/src/core/hle/service/ir/ir_rst.h index 8e17381ad..2514ab6f9 100644 --- a/src/core/hle/service/ir/ir_rst.h +++ b/src/core/hle/service/ir/ir_rst.h @@ -74,7 +74,7 @@ private: void LoadInputDevices(); void UnloadInputDevices(); - void UpdateCallback(u64 userdata, s64 cycles_late); + void UpdateCallback(std::uintptr_t user_data, s64 cycles_late); Core::System& system; std::shared_ptr update_event; diff --git a/src/core/hle/service/mic_u.cpp b/src/core/hle/service/mic_u.cpp index a09ef14ef..d95bcc890 100644 --- a/src/core/hle/service/mic_u.cpp +++ b/src/core/hle/service/mic_u.cpp @@ -126,9 +126,9 @@ struct MIC_U::Impl { explicit Impl(Core::System& system) : timing(system.CoreTiming()) { buffer_full_event = system.Kernel().CreateEvent(Kernel::ResetType::OneShot, "MIC_U::buffer_full_event"); - buffer_write_event = - timing.RegisterEvent("MIC_U::UpdateBuffer", [this](u64 userdata, s64 cycles_late) { - UpdateSharedMemBuffer(userdata, cycles_late); + buffer_write_event = timing.RegisterEvent( + "MIC_U::UpdateBuffer", [this](std::uintptr_t user_data, s64 cycles_late) { + UpdateSharedMemBuffer(user_data, cycles_late); }); } @@ -158,7 +158,7 @@ struct MIC_U::Impl { LOG_TRACE(Service_MIC, "called"); } - void UpdateSharedMemBuffer(u64 userdata, s64 cycles_late) { + void UpdateSharedMemBuffer(std::uintptr_t user_data, s64 cycles_late) { if (change_mic_impl_requested.exchange(false)) { CreateMic(); } diff --git a/src/core/hle/service/nwm/nwm_uds.cpp b/src/core/hle/service/nwm/nwm_uds.cpp index 988fbcc4f..579ee159c 100644 --- a/src/core/hle/service/nwm/nwm_uds.cpp +++ b/src/core/hle/service/nwm/nwm_uds.cpp @@ -1443,7 +1443,7 @@ void NWM_UDS::DecryptBeaconData(Kernel::HLERequestContext& ctx) { } // Sends a 802.11 beacon frame with information about the current network. -void NWM_UDS::BeaconBroadcastCallback(u64 userdata, s64 cycles_late) { +void NWM_UDS::BeaconBroadcastCallback(std::uintptr_t user_data, s64 cycles_late) { // Don't do anything if we're not actually hosting a network if (connection_status.status != static_cast(NetworkStatus::ConnectedAsHost)) return; @@ -1503,8 +1503,9 @@ NWM_UDS::NWM_UDS(Core::System& system) : ServiceFramework("nwm::UDS"), system(sy RegisterHandlers(functions); beacon_broadcast_event = system.CoreTiming().RegisterEvent( - "UDS::BeaconBroadcastCallback", - [this](u64 userdata, s64 cycles_late) { BeaconBroadcastCallback(userdata, cycles_late); }); + "UDS::BeaconBroadcastCallback", [this](std::uintptr_t user_data, s64 cycles_late) { + BeaconBroadcastCallback(user_data, cycles_late); + }); CryptoPP::AutoSeededRandomPool rng; auto mac = SharedPage::DefaultMac; diff --git a/src/core/hle/service/nwm/nwm_uds.h b/src/core/hle/service/nwm/nwm_uds.h index c5bff31ac..e8eeb360a 100644 --- a/src/core/hle/service/nwm/nwm_uds.h +++ b/src/core/hle/service/nwm/nwm_uds.h @@ -459,7 +459,7 @@ private: const u8* network_info_buffer, std::size_t network_info_size, u8 connection_type, std::vector passphrase); - void BeaconBroadcastCallback(u64 userdata, s64 cycles_late); + void BeaconBroadcastCallback(std::uintptr_t user_data, s64 cycles_late); /** * Returns a list of received 802.11 beacon frames from the specified sender since the last diff --git a/src/core/hw/gpu.cpp b/src/core/hw/gpu.cpp index 39b87155f..983644282 100644 --- a/src/core/hw/gpu.cpp +++ b/src/core/hw/gpu.cpp @@ -503,7 +503,7 @@ template void Write(u32 addr, const u16 data); template void Write(u32 addr, const u8 data); /// Update hardware -static void VBlankCallback(u64 userdata, s64 cycles_late) { +static void VBlankCallback(std::uintptr_t user_data, s64 cycles_late) { VideoCore::g_renderer->SwapBuffers(); // Signal to GSP that GPU interrupt has occurred diff --git a/src/tests/core/core_timing.cpp b/src/tests/core/core_timing.cpp index e82ffc8a3..970d43eb1 100644 --- a/src/tests/core/core_timing.cpp +++ b/src/tests/core/core_timing.cpp @@ -21,10 +21,10 @@ static u64 expected_callback = 0; static s64 lateness = 0; template -void CallbackTemplate(u64 userdata, s64 cycles_late) { +void CallbackTemplate(std::uintptr_t user_data, s64 cycles_late) { static_assert(IDX < CB_IDS.size(), "IDX out of range"); callbacks_ran_flags.set(IDX); - REQUIRE(CB_IDS[IDX] == userdata); + REQUIRE(CB_IDS[IDX] == user_data); REQUIRE(CB_IDS[IDX] == expected_callback); REQUIRE(lateness == cycles_late); } @@ -81,10 +81,10 @@ namespace SharedSlotTest { static unsigned int counter = 0; template -void FifoCallback(u64 userdata, s64 cycles_late) { +void FifoCallback(std::uintptr_t user_data, s64 cycles_late) { static_assert(ID < CB_IDS.size(), "ID out of range"); callbacks_ran_flags.set(ID); - REQUIRE(CB_IDS[ID] == userdata); + REQUIRE(CB_IDS[ID] == user_data); REQUIRE(ID == counter); REQUIRE(lateness == cycles_late); ++counter; @@ -143,13 +143,13 @@ TEST_CASE("CoreTiming[PredictableLateness]", "[core]") { namespace ChainSchedulingTest { static int reschedules = 0; -static void RescheduleCallback(Core::Timing& timing, u64 userdata, s64 cycles_late) { +static void RescheduleCallback(Core::Timing& timing, std::uintptr_t user_data, s64 cycles_late) { --reschedules; REQUIRE(reschedules >= 0); REQUIRE(lateness == cycles_late); if (reschedules > 0) - timing.ScheduleEvent(1000, reinterpret_cast(userdata), userdata); + timing.ScheduleEvent(1000, reinterpret_cast(user_data), user_data); } } // namespace ChainSchedulingTest @@ -161,9 +161,9 @@ TEST_CASE("CoreTiming[ChainScheduling]", "[core]") { Core::TimingEventType* cb_a = timing.RegisterEvent("callbackA", CallbackTemplate<0>); Core::TimingEventType* cb_b = timing.RegisterEvent("callbackB", CallbackTemplate<1>); Core::TimingEventType* cb_c = timing.RegisterEvent("callbackC", CallbackTemplate<2>); - Core::TimingEventType* cb_rs = - timing.RegisterEvent("callbackReschedule", [&timing](u64 userdata, s64 cycles_late) { - RescheduleCallback(timing, userdata, cycles_late); + Core::TimingEventType* cb_rs = timing.RegisterEvent( + "callbackReschedule", [&timing](std::uintptr_t user_data, s64 cycles_late) { + RescheduleCallback(timing, user_data, cycles_late); }); // Enter slice 0