forked from suyu/suyu
hle: kernel: TimeManager: Simplify to not rely on previous EmuThreadHandle implementation.
This commit is contained in:
parent
bb966d3e33
commit
c0f5830323
6 changed files with 25 additions and 69 deletions
|
@ -232,10 +232,9 @@ ResultCode KAddressArbiter::SignalAndModifyByWaitingCountIfEqual(VAddr addr, s32
|
|||
ResultCode KAddressArbiter::WaitIfLessThan(VAddr addr, s32 value, bool decrement, s64 timeout) {
|
||||
// Prepare to wait.
|
||||
KThread* cur_thread = kernel.CurrentScheduler()->GetCurrentThread();
|
||||
Handle timer = InvalidHandle;
|
||||
|
||||
{
|
||||
KScopedSchedulerLockAndSleep slp(kernel, timer, cur_thread, timeout);
|
||||
KScopedSchedulerLockAndSleep slp{kernel, cur_thread, timeout};
|
||||
|
||||
// Check that the thread isn't terminating.
|
||||
if (cur_thread->IsTerminationRequested()) {
|
||||
|
@ -280,10 +279,7 @@ ResultCode KAddressArbiter::WaitIfLessThan(VAddr addr, s32 value, bool decrement
|
|||
}
|
||||
|
||||
// Cancel the timer wait.
|
||||
if (timer != InvalidHandle) {
|
||||
auto& time_manager = kernel.TimeManager();
|
||||
time_manager.UnscheduleTimeEvent(timer);
|
||||
}
|
||||
kernel.TimeManager().UnscheduleTimeEvent(cur_thread);
|
||||
|
||||
// Remove from the address arbiter.
|
||||
{
|
||||
|
@ -303,10 +299,9 @@ ResultCode KAddressArbiter::WaitIfLessThan(VAddr addr, s32 value, bool decrement
|
|||
ResultCode KAddressArbiter::WaitIfEqual(VAddr addr, s32 value, s64 timeout) {
|
||||
// Prepare to wait.
|
||||
KThread* cur_thread = kernel.CurrentScheduler()->GetCurrentThread();
|
||||
Handle timer = InvalidHandle;
|
||||
|
||||
{
|
||||
KScopedSchedulerLockAndSleep slp(kernel, timer, cur_thread, timeout);
|
||||
KScopedSchedulerLockAndSleep slp{kernel, cur_thread, timeout};
|
||||
|
||||
// Check that the thread isn't terminating.
|
||||
if (cur_thread->IsTerminationRequested()) {
|
||||
|
@ -344,10 +339,7 @@ ResultCode KAddressArbiter::WaitIfEqual(VAddr addr, s32 value, s64 timeout) {
|
|||
}
|
||||
|
||||
// Cancel the timer wait.
|
||||
if (timer != InvalidHandle) {
|
||||
auto& time_manager = kernel.TimeManager();
|
||||
time_manager.UnscheduleTimeEvent(timer);
|
||||
}
|
||||
kernel.TimeManager().UnscheduleTimeEvent(cur_thread);
|
||||
|
||||
// Remove from the address arbiter.
|
||||
{
|
||||
|
|
|
@ -258,10 +258,9 @@ void KConditionVariable::Signal(u64 cv_key, s32 count) {
|
|||
ResultCode KConditionVariable::Wait(VAddr addr, u64 key, u32 value, s64 timeout) {
|
||||
// Prepare to wait.
|
||||
KThread* cur_thread = kernel.CurrentScheduler()->GetCurrentThread();
|
||||
Handle timer = InvalidHandle;
|
||||
|
||||
{
|
||||
KScopedSchedulerLockAndSleep slp(kernel, timer, cur_thread, timeout);
|
||||
KScopedSchedulerLockAndSleep slp{kernel, cur_thread, timeout};
|
||||
|
||||
// Set the synced object.
|
||||
cur_thread->SetSyncedObject(nullptr, Svc::ResultTimedOut);
|
||||
|
@ -322,10 +321,7 @@ ResultCode KConditionVariable::Wait(VAddr addr, u64 key, u32 value, s64 timeout)
|
|||
}
|
||||
|
||||
// Cancel the timer wait.
|
||||
if (timer != InvalidHandle) {
|
||||
auto& time_manager = kernel.TimeManager();
|
||||
time_manager.UnscheduleTimeEvent(timer);
|
||||
}
|
||||
kernel.TimeManager().UnscheduleTimeEvent(cur_thread);
|
||||
|
||||
// Remove from the condition variable.
|
||||
{
|
||||
|
|
|
@ -17,19 +17,16 @@ namespace Kernel {
|
|||
|
||||
class KScopedSchedulerLockAndSleep {
|
||||
public:
|
||||
explicit KScopedSchedulerLockAndSleep(KernelCore& kernel, Handle& event_handle, KThread* t,
|
||||
s64 timeout)
|
||||
: kernel(kernel), event_handle(event_handle), thread(t), timeout_tick(timeout) {
|
||||
event_handle = InvalidHandle;
|
||||
|
||||
explicit KScopedSchedulerLockAndSleep(KernelCore& kernel, KThread* t, s64 timeout)
|
||||
: kernel(kernel), thread(t), timeout_tick(timeout) {
|
||||
// Lock the scheduler.
|
||||
kernel.GlobalSchedulerContext().scheduler_lock.Lock();
|
||||
}
|
||||
|
||||
~KScopedSchedulerLockAndSleep() {
|
||||
// Register the sleep.
|
||||
if (this->timeout_tick > 0) {
|
||||
kernel.TimeManager().ScheduleTimeEvent(event_handle, this->thread, this->timeout_tick);
|
||||
if (timeout_tick > 0) {
|
||||
kernel.TimeManager().ScheduleTimeEvent(thread, timeout_tick);
|
||||
}
|
||||
|
||||
// Unlock the scheduler.
|
||||
|
@ -37,12 +34,11 @@ public:
|
|||
}
|
||||
|
||||
void CancelSleep() {
|
||||
this->timeout_tick = 0;
|
||||
timeout_tick = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
KernelCore& kernel;
|
||||
Handle& event_handle;
|
||||
KThread* thread{};
|
||||
s64 timeout_tick{};
|
||||
};
|
||||
|
|
|
@ -21,11 +21,10 @@ ResultCode KSynchronizationObject::Wait(KernelCore& kernel, s32* out_index,
|
|||
|
||||
// Prepare for wait.
|
||||
KThread* thread = kernel.CurrentScheduler()->GetCurrentThread();
|
||||
Handle timer = InvalidHandle;
|
||||
|
||||
{
|
||||
// Setup the scheduling lock and sleep.
|
||||
KScopedSchedulerLockAndSleep slp(kernel, timer, thread, timeout);
|
||||
KScopedSchedulerLockAndSleep slp{kernel, thread, timeout};
|
||||
|
||||
// Check if any of the objects are already signaled.
|
||||
for (auto i = 0; i < num_objects; ++i) {
|
||||
|
@ -90,10 +89,7 @@ ResultCode KSynchronizationObject::Wait(KernelCore& kernel, s32* out_index,
|
|||
thread->SetWaitObjectsForDebugging({});
|
||||
|
||||
// Cancel the timer as needed.
|
||||
if (timer != InvalidHandle) {
|
||||
auto& time_manager = kernel.TimeManager();
|
||||
time_manager.UnscheduleTimeEvent(timer);
|
||||
}
|
||||
kernel.TimeManager().UnscheduleTimeEvent(thread);
|
||||
|
||||
// Get the wait result.
|
||||
ResultCode wait_result{RESULT_SUCCESS};
|
||||
|
|
|
@ -21,47 +21,27 @@ TimeManager::TimeManager(Core::System& system_) : system{system_} {
|
|||
std::shared_ptr<KThread> thread;
|
||||
{
|
||||
std::lock_guard lock{mutex};
|
||||
const auto proper_handle = static_cast<Handle>(thread_handle);
|
||||
if (cancelled_events[proper_handle]) {
|
||||
return;
|
||||
thread = SharedFrom<KThread>(reinterpret_cast<KThread*>(thread_handle));
|
||||
}
|
||||
thread = system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
|
||||
}
|
||||
|
||||
if (thread) {
|
||||
// Thread can be null if process has exited
|
||||
thread->Wakeup();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void TimeManager::ScheduleTimeEvent(Handle& event_handle, KThread* timetask, s64 nanoseconds) {
|
||||
void TimeManager::ScheduleTimeEvent(KThread* thread, s64 nanoseconds) {
|
||||
std::lock_guard lock{mutex};
|
||||
event_handle = timetask->GetGlobalHandle();
|
||||
if (nanoseconds > 0) {
|
||||
ASSERT(timetask);
|
||||
ASSERT(timetask->GetState() != ThreadState::Runnable);
|
||||
ASSERT(thread);
|
||||
ASSERT(thread->GetState() != ThreadState::Runnable);
|
||||
system.CoreTiming().ScheduleEvent(std::chrono::nanoseconds{nanoseconds},
|
||||
time_manager_event_type, event_handle);
|
||||
} else {
|
||||
event_handle = InvalidHandle;
|
||||
time_manager_event_type,
|
||||
reinterpret_cast<uintptr_t>(thread));
|
||||
}
|
||||
cancelled_events[event_handle] = false;
|
||||
}
|
||||
|
||||
void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
|
||||
void TimeManager::UnscheduleTimeEvent(KThread* thread) {
|
||||
std::lock_guard lock{mutex};
|
||||
if (event_handle == InvalidHandle) {
|
||||
return;
|
||||
}
|
||||
system.CoreTiming().UnscheduleEvent(time_manager_event_type, event_handle);
|
||||
cancelled_events[event_handle] = true;
|
||||
}
|
||||
|
||||
void TimeManager::CancelTimeEvent(KThread* time_task) {
|
||||
std::lock_guard lock{mutex};
|
||||
const Handle event_handle = time_task->GetGlobalHandle();
|
||||
UnscheduleTimeEvent(event_handle);
|
||||
system.CoreTiming().UnscheduleEvent(time_manager_event_type,
|
||||
reinterpret_cast<uintptr_t>(thread));
|
||||
}
|
||||
|
||||
} // namespace Kernel
|
||||
|
|
|
@ -31,18 +31,14 @@ public:
|
|||
explicit TimeManager(Core::System& system);
|
||||
|
||||
/// Schedule a time event on `timetask` thread that will expire in 'nanoseconds'
|
||||
/// returns a non-invalid handle in `event_handle` if correctly scheduled
|
||||
void ScheduleTimeEvent(Handle& event_handle, KThread* timetask, s64 nanoseconds);
|
||||
void ScheduleTimeEvent(KThread* time_task, s64 nanoseconds);
|
||||
|
||||
/// Unschedule an existing time event
|
||||
void UnscheduleTimeEvent(Handle event_handle);
|
||||
|
||||
void CancelTimeEvent(KThread* time_task);
|
||||
void UnscheduleTimeEvent(KThread* thread);
|
||||
|
||||
private:
|
||||
Core::System& system;
|
||||
std::shared_ptr<Core::Timing::EventType> time_manager_event_type;
|
||||
std::unordered_map<Handle, bool> cancelled_events;
|
||||
std::mutex mutex;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue