time_manager.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include "core/core.h"
  5. #include "core/core_timing.h"
  6. #include "core/core_timing_util.h"
  7. #include "core/hle/kernel/handle_table.h"
  8. #include "core/hle/kernel/kernel.h"
  9. #include "core/hle/kernel/thread.h"
  10. #include "core/hle/kernel/time_manager.h"
  11. namespace Kernel {
  12. TimeManager::TimeManager(Core::System& system) : system{system} {
  13. time_manager_event_type = Core::Timing::CreateEvent(
  14. "Kernel::TimeManagerCallback", [this](u64 thread_handle, [[maybe_unused]] s64 cycles_late) {
  15. Handle proper_handle = static_cast<Handle>(thread_handle);
  16. std::shared_ptr<Thread> thread =
  17. this->system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
  18. thread->ResumeFromWait();
  19. });
  20. }
  21. void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64 nanoseconds) {
  22. if (nanoseconds > 0) {
  23. ASSERT(timetask);
  24. event_handle = timetask->GetGlobalHandle();
  25. const s64 cycles = Core::Timing::nsToCycles(std::chrono::nanoseconds{nanoseconds});
  26. system.CoreTiming().ScheduleEvent(cycles, time_manager_event_type, event_handle);
  27. } else {
  28. event_handle = InvalidHandle;
  29. }
  30. }
  31. void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
  32. if (event_handle != InvalidHandle) {
  33. system.CoreTiming().UnscheduleEvent(time_manager_event_type, event_handle);
  34. }
  35. }
  36. } // namespace Kernel