summaryrefslogtreecommitdiffstats
path: root/src/core/core_cpu.h
blob: e6ed698ccfa32bbf6faa1077043cad55ed32aa6d (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
// Copyright 2018 yuzu emulator team
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.

#pragma once

#include <condition_variable>
#include <memory>
#include <mutex>
#include <string>
#include "common/common_types.h"

class ARM_Interface;

namespace Kernel {
class Scheduler;
}

namespace Core {

constexpr unsigned NUM_CPU_CORES{4};

class CpuBarrier {
public:
    void Rendezvous() {
        std::unique_lock<std::mutex> lock(mutex);

        --cores_waiting;
        if (!cores_waiting) {
            cores_waiting = NUM_CPU_CORES;
            condition.notify_all();
            return;
        }

        condition.wait(lock);
    }

private:
    unsigned cores_waiting{NUM_CPU_CORES};
    std::mutex mutex;
    std::condition_variable condition;
};

class Cpu {
public:
    Cpu(std::shared_ptr<CpuBarrier> cpu_barrier, size_t core_index);

    void RunLoop(bool tight_loop = true);

    void SingleStep();

    void PrepareReschedule();

    ARM_Interface& CPU() {
        return *arm_interface;
    }

    Kernel::Scheduler& Scheduler() {
        return *scheduler;
    }

    bool IsMainCore() const {
        return core_index == 0;
    }

private:
    void Reschedule();

    std::shared_ptr<ARM_Interface> arm_interface;
    std::shared_ptr<CpuBarrier> cpu_barrier;
    std::unique_ptr<Kernel::Scheduler> scheduler;

    bool reschedule_pending{};
    size_t core_index;
};

} // namespace Core