summaryrefslogtreecommitdiffstats
path: root/src/core/hle/kernel/scheduler.h
blob: 50fa7376b4dc3cb59f7ff1afb9ade64b2326ed76 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <mutex>
#include <vector>
#include "common/common_types.h"
#include "common/multi_level_queue.h"
#include "core/hle/kernel/object.h"
#include "core/hle/kernel/thread.h"

namespace Core {
class ARM_Interface;
class System;
} // namespace Core

namespace Kernel {

class Process;

class GlobalScheduler final {
public:
    static constexpr u32 NUM_CPU_CORES = 4;

    GlobalScheduler() {
        reselection_pending = false;
    }
    ~GlobalScheduler();
    /// Adds a new thread to the scheduler
    void AddThread(SharedPtr<Thread> thread);

    /// Removes a thread from the scheduler
    void RemoveThread(Thread* thread);

    /// Returns a list of all threads managed by the scheduler
    const std::vector<SharedPtr<Thread>>& GetThreadList() const {
        return thread_list;
    }

    void Suggest(u32 priority, u32 core, Thread* thread) {
        suggested_queue[core].add(thread, priority);
    }

    void Unsuggest(u32 priority, u32 core, Thread* thread) {
        suggested_queue[core].remove(thread, priority);
    }

    void Schedule(u32 priority, u32 core, Thread* thread) {
        ASSERT_MSG(thread->GetProcessorID() == core,
                   "Thread must be assigned to this core.");
        scheduled_queue[core].add(thread, priority);
    }

    void SchedulePrepend(u32 priority, u32 core, Thread* thread) {
        ASSERT_MSG(thread->GetProcessorID() == core,
                   "Thread must be assigned to this core.");
        scheduled_queue[core].add(thread, priority, false);
    }

    void Reschedule(u32 priority, u32 core, Thread* thread) {
        scheduled_queue[core].remove(thread, priority);
        scheduled_queue[core].add(thread, priority);
    }

    void Unschedule(u32 priority, u32 core, Thread* thread) {
        scheduled_queue[core].remove(thread, priority);
    }

    void TransferToCore(u32 priority, s32 destination_core, Thread* thread) {
        bool schedulable = thread->GetPriority() < THREADPRIO_COUNT;
        s32 source_core = thread->GetProcessorID();
        if (source_core == destination_core || !schedulable)
            return;
        thread->SetProcessorID(destination_core);
        if (source_core >= 0)
            Unschedule(priority, source_core, thread);
        if (destination_core >= 0) {
            Unsuggest(priority, destination_core, thread);
            Schedule(priority, destination_core, thread);
        }
        if (source_core >= 0)
            Suggest(priority, source_core, thread);
    }

    void UnloadThread(s32 core);

    void SelectThreads();
    void SelectThread(u32 core);

    bool HaveReadyThreads(u32 core_id) {
        return !scheduled_queue[core_id].empty();
    }

    void YieldThread(Thread* thread);
    void YieldThreadAndBalanceLoad(Thread* thread);
    void YieldThreadAndWaitForLoadBalancing(Thread* thread);

    u32 CpuCoresCount() const {
        return NUM_CPU_CORES;
    }

    void SetReselectionPending() {
        reselection_pending.store(true, std::memory_order_release);
    }

    bool IsReselectionPending() {
        return reselection_pending.load(std::memory_order_acquire);
    }

private:
    void AskForReselectionOrMarkRedundant(Thread* current_thread, Thread* winner);

    static constexpr u32 min_regular_priority = 2;
    std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> scheduled_queue;
    std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> suggested_queue;
    std::atomic<bool> reselection_pending;

    /// Lists all thread ids that aren't deleted/etc.
    std::vector<SharedPtr<Thread>> thread_list;
};

class Scheduler final {
public:
    explicit Scheduler(Core::System& system, Core::ARM_Interface& cpu_core, const u32 id);
    ~Scheduler();

    /// Returns whether there are any threads that are ready to run.
    bool HaveReadyThreads() const;

    /// Reschedules to the next available thread (call after current thread is suspended)
    void TryDoContextSwitch();

    void UnloadThread();

    void SelectThreads();

    /// Gets the current running thread
    Thread* GetCurrentThread() const;

    Thread* GetSelectedThread() const;

    /// Gets the timestamp for the last context switch in ticks.
    u64 GetLastContextSwitchTicks() const;

    bool ContextSwitchPending() const {
        return context_switch_pending;
    }

private:
    friend class GlobalScheduler;
    /**
     * Switches the CPU's active thread context to that of the specified thread
     * @param new_thread The thread to switch to
     */
    void SwitchContext();

    /**
     * Called on every context switch to update the internal timestamp
     * This also updates the running time ticks for the given thread and
     * process using the following difference:
     *
     * ticks += most_recent_ticks - last_context_switch_ticks
     *
     * The internal tick timestamp for the scheduler is simply the
     * most recent tick count retrieved. No special arithmetic is
     * applied to it.
     */
    void UpdateLastContextSwitchTime(Thread* thread, Process* process);

    SharedPtr<Thread> current_thread = nullptr;
    SharedPtr<Thread> selected_thread = nullptr;

    Core::System& system;
    Core::ARM_Interface& cpu_core;
    u64 last_context_switch_time = 0;
    u64 idle_selection_count = 0;
    const u32 id;

    bool context_switch_pending = false;
};

} // namespace Kernel