summaryrefslogtreecommitdiffstats
path: root/src/core/cpu_manager.h
blob: 8143424af232c3afbde0fa86b3b11d417a3a5a0f (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
// SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <array>
#include <atomic>
#include <csetjmp>
#include <functional>
#include <memory>
#include <thread>

#include "common/fiber.h"
#include "common/thread.h"
#include "core/hardware_properties.h"

namespace Common {
class Event;
class Fiber;
} // namespace Common

namespace Core {

class System;

class CpuManager {
public:
    explicit CpuManager(System& system_);
    CpuManager(const CpuManager&) = delete;
    CpuManager(CpuManager&&) = delete;

    ~CpuManager();

    CpuManager& operator=(const CpuManager&) = delete;
    CpuManager& operator=(CpuManager&&) = delete;

    /// Sets if emulation is multicore or single core, must be set before Initialize
    void SetMulticore(bool is_multi) {
        is_multicore = is_multi;
    }

    /// Sets if emulation is using an asynchronous GPU.
    void SetAsyncGpu(bool is_async) {
        is_async_gpu = is_async;
    }

    void OnGpuReady() {
        gpu_barrier->Sync();
    }

    void WaitForAndHandleInterrupt();
    void Initialize();
    void Shutdown();

    std::function<void()> GetGuestActivateFunc() {
        return [this] { GuestActivateFunction(); };
    }
    std::function<void()> GetGuestThreadFunc() {
        return [this] { GuestThreadFunction(); };
    }
    std::function<void()> GetIdleThreadStartFunc() {
        return [this] { IdleThreadFunction(); };
    }
    std::function<void()> GetShutdownThreadStartFunc() {
        return [this] { ShutdownThreadFunction(); };
    }

    void PreemptSingleCore(bool from_running_enviroment = true);

    std::size_t CurrentCore() const {
        return current_core.load();
    }

private:
    void GuestActivateFunction();
    void GuestThreadFunction();
    void IdleThreadFunction();
    void ShutdownThreadFunction();

    void MultiCoreGuestActivate();
    void MultiCoreRunGuestThread();
    void MultiCoreRunGuestLoop();

    void SingleCoreGuestActivate();
    void SingleCoreRunGuestThread();
    void SingleCoreRunGuestLoop();

    static void ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager, std::size_t core);

    void HandleInterrupt();
    void ShutdownThread();
    void RunThread(std::size_t core);

    struct CoreData {
        std::shared_ptr<Common::Fiber> host_context;
        std::jthread host_thread;
    };

    std::unique_ptr<Common::Barrier> gpu_barrier{};
    std::array<CoreData, Core::Hardware::NUM_CPU_CORES> core_data{};

    bool is_async_gpu{};
    bool is_multicore{};
    std::atomic<std::size_t> current_core{};
    std::size_t idle_count{};
    std::size_t num_cores{};
    static constexpr std::size_t max_cycle_runs = 5;

    System& system;
};

} // namespace Core