diff options
| author | bunnei <bunneidev@gmail.com> | 2018-05-02 21:26:14 -0400 |
|---|---|---|
| committer | bunnei <bunneidev@gmail.com> | 2018-05-10 19:34:46 -0400 |
| commit | 9776ff91797423a9cf19571faafe4648fb5a1d1d (patch) | |
| tree | 46a7c94c53faee26b805f6290a09c45d33f7bf11 /src/core/core_cpu.h | |
| parent | 559024593086d04e24a99a9f77490a3f97cf952d (diff) | |
core: Create a thread for each CPU core, keep in lock-step with a barrier.
Diffstat (limited to 'src/core/core_cpu.h')
| -rw-r--r-- | src/core/core_cpu.h | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/src/core/core_cpu.h b/src/core/core_cpu.h index 312db1655..e6ed698cc 100644 --- a/src/core/core_cpu.h +++ b/src/core/core_cpu.h @@ -4,7 +4,9 @@ #pragma once +#include <condition_variable> #include <memory> +#include <mutex> #include <string> #include "common/common_types.h" @@ -16,9 +18,32 @@ 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(); + Cpu(std::shared_ptr<CpuBarrier> cpu_barrier, size_t core_index); void RunLoop(bool tight_loop = true); @@ -34,13 +59,19 @@ public: 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 |
