summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/time_manager.cpp
blob: dab5fc4c6f49e4d2931b0efe9e27aa5f956adcc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2020 yuzu Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#include "common/assert.h"
#include "core/core.h"
#include "core/core_timing.h"
#include "core/core_timing_util.h"
#include "core/hle/kernel/handle_table.h"
#include "core/hle/kernel/kernel.h"
#include "core/hle/kernel/scheduler.h"
#include "core/hle/kernel/thread.h"
#include "core/hle/kernel/time_manager.h"

namespace Kernel {

TimeManager::TimeManager(Core::System& system_) : system{system_} {
    time_manager_event_type = Core::Timing::CreateEvent(
        "Kernel::TimeManagerCallback", [this](u64 thread_handle, [[maybe_unused]] s64 cycles_late) {
            SchedulerLock lock(system.Kernel());
            Handle proper_handle = static_cast<Handle>(thread_handle);
            if (cancelled_events[proper_handle]) {
                return;
            }
            event_fired[proper_handle] = true;
            std::shared_ptr<Thread> thread =
                this->system.Kernel().RetrieveThreadFromGlobalHandleTable(proper_handle);
            thread->OnWakeUp();
        });
}

void TimeManager::ScheduleTimeEvent(Handle& event_handle, Thread* timetask, s64 nanoseconds) {
    event_handle = timetask->GetGlobalHandle();
    if (nanoseconds > 0) {
        ASSERT(timetask);
        const s64 cycles = Core::Timing::nsToCycles(std::chrono::nanoseconds{nanoseconds});
        system.CoreTiming().ScheduleEvent(cycles, time_manager_event_type, event_handle);
    } else {
        event_handle = InvalidHandle;
    }
    cancelled_events[event_handle] = false;
    event_fired[event_handle] = false;
}

void TimeManager::UnscheduleTimeEvent(Handle event_handle) {
    if (event_handle == InvalidHandle) {
        return;
    }
    system.CoreTiming().UnscheduleEvent(time_manager_event_type, event_handle);
    cancelled_events[event_handle] = true;
}

void TimeManager::CancelTimeEvent(Thread* time_task) {
    Handle event_handle = time_task->GetGlobalHandle();
    UnscheduleTimeEvent(event_handle);
}

} // namespace Kernel