diff options
| author | 2021-09-06 03:49:14 -0700 | |
|---|---|---|
| committer | 2021-09-06 03:49:14 -0700 | |
| commit | 51ccc29cdd2e8ac7aa1a10dfdef12ad981c67056 (patch) | |
| tree | 50bf923ae390709e5e40e5479e0cb719c1464846 /src | |
| parent | Merge pull request #6968 from bunnei/nvflinger-event (diff) | |
| parent | core: cpu_manager: Use jthread. (diff) | |
| download | yuzu-51ccc29cdd2e8ac7aa1a10dfdef12ad981c67056.tar.gz yuzu-51ccc29cdd2e8ac7aa1a10dfdef12ad981c67056.tar.xz yuzu-51ccc29cdd2e8ac7aa1a10dfdef12ad981c67056.zip | |
Merge pull request #6965 from bunnei/cpu_manager_jthread
core: cpu_manager: Use jthread.
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/cpu_manager.cpp | 25 | ||||
| -rw-r--r-- | src/core/cpu_manager.h | 6 |
2 files changed, 13 insertions, 18 deletions
diff --git a/src/core/cpu_manager.cpp b/src/core/cpu_manager.cpp index 7e195346b..77efcabf0 100644 --- a/src/core/cpu_manager.cpp +++ b/src/core/cpu_manager.cpp | |||
| @@ -21,34 +21,25 @@ namespace Core { | |||
| 21 | CpuManager::CpuManager(System& system_) : system{system_} {} | 21 | CpuManager::CpuManager(System& system_) : system{system_} {} |
| 22 | CpuManager::~CpuManager() = default; | 22 | CpuManager::~CpuManager() = default; |
| 23 | 23 | ||
| 24 | void CpuManager::ThreadStart(CpuManager& cpu_manager, std::size_t core) { | 24 | void CpuManager::ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager, |
| 25 | cpu_manager.RunThread(core); | 25 | std::size_t core) { |
| 26 | cpu_manager.RunThread(stop_token, core); | ||
| 26 | } | 27 | } |
| 27 | 28 | ||
| 28 | void CpuManager::Initialize() { | 29 | void CpuManager::Initialize() { |
| 29 | running_mode = true; | 30 | running_mode = true; |
| 30 | if (is_multicore) { | 31 | if (is_multicore) { |
| 31 | for (std::size_t core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) { | 32 | for (std::size_t core = 0; core < Core::Hardware::NUM_CPU_CORES; core++) { |
| 32 | core_data[core].host_thread = | 33 | core_data[core].host_thread = std::jthread(ThreadStart, std::ref(*this), core); |
| 33 | std::make_unique<std::thread>(ThreadStart, std::ref(*this), core); | ||
| 34 | } | 34 | } |
| 35 | } else { | 35 | } else { |
| 36 | core_data[0].host_thread = std::make_unique<std::thread>(ThreadStart, std::ref(*this), 0); | 36 | core_data[0].host_thread = std::jthread(ThreadStart, std::ref(*this), 0); |
| 37 | } | 37 | } |
| 38 | } | 38 | } |
| 39 | 39 | ||
| 40 | void CpuManager::Shutdown() { | 40 | void CpuManager::Shutdown() { |
| 41 | running_mode = false; | 41 | running_mode = false; |
| 42 | Pause(false); | 42 | Pause(false); |
| 43 | if (is_multicore) { | ||
| 44 | for (auto& data : core_data) { | ||
| 45 | data.host_thread->join(); | ||
| 46 | data.host_thread.reset(); | ||
| 47 | } | ||
| 48 | } else { | ||
| 49 | core_data[0].host_thread->join(); | ||
| 50 | core_data[0].host_thread.reset(); | ||
| 51 | } | ||
| 52 | } | 43 | } |
| 53 | 44 | ||
| 54 | std::function<void(void*)> CpuManager::GetGuestThreadStartFunc() { | 45 | std::function<void(void*)> CpuManager::GetGuestThreadStartFunc() { |
| @@ -317,7 +308,7 @@ void CpuManager::Pause(bool paused) { | |||
| 317 | } | 308 | } |
| 318 | } | 309 | } |
| 319 | 310 | ||
| 320 | void CpuManager::RunThread(std::size_t core) { | 311 | void CpuManager::RunThread(std::stop_token stop_token, std::size_t core) { |
| 321 | /// Initialization | 312 | /// Initialization |
| 322 | system.RegisterCoreThread(core); | 313 | system.RegisterCoreThread(core); |
| 323 | std::string name; | 314 | std::string name; |
| @@ -361,6 +352,10 @@ void CpuManager::RunThread(std::size_t core) { | |||
| 361 | return; | 352 | return; |
| 362 | } | 353 | } |
| 363 | 354 | ||
| 355 | if (stop_token.stop_requested()) { | ||
| 356 | break; | ||
| 357 | } | ||
| 358 | |||
| 364 | auto current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread(); | 359 | auto current_thread = system.Kernel().CurrentScheduler()->GetCurrentThread(); |
| 365 | data.is_running = true; | 360 | data.is_running = true; |
| 366 | Common::Fiber::YieldTo(data.host_context, *current_thread->GetHostContext()); | 361 | Common::Fiber::YieldTo(data.host_context, *current_thread->GetHostContext()); |
diff --git a/src/core/cpu_manager.h b/src/core/cpu_manager.h index 140263b09..9d92d4af0 100644 --- a/src/core/cpu_manager.h +++ b/src/core/cpu_manager.h | |||
| @@ -78,9 +78,9 @@ private: | |||
| 78 | void SingleCoreRunSuspendThread(); | 78 | void SingleCoreRunSuspendThread(); |
| 79 | void SingleCorePause(bool paused); | 79 | void SingleCorePause(bool paused); |
| 80 | 80 | ||
| 81 | static void ThreadStart(CpuManager& cpu_manager, std::size_t core); | 81 | static void ThreadStart(std::stop_token stop_token, CpuManager& cpu_manager, std::size_t core); |
| 82 | 82 | ||
| 83 | void RunThread(std::size_t core); | 83 | void RunThread(std::stop_token stop_token, std::size_t core); |
| 84 | 84 | ||
| 85 | struct CoreData { | 85 | struct CoreData { |
| 86 | std::shared_ptr<Common::Fiber> host_context; | 86 | std::shared_ptr<Common::Fiber> host_context; |
| @@ -89,7 +89,7 @@ private: | |||
| 89 | std::atomic<bool> is_running; | 89 | std::atomic<bool> is_running; |
| 90 | std::atomic<bool> is_paused; | 90 | std::atomic<bool> is_paused; |
| 91 | std::atomic<bool> initialized; | 91 | std::atomic<bool> initialized; |
| 92 | std::unique_ptr<std::thread> host_thread; | 92 | std::jthread host_thread; |
| 93 | }; | 93 | }; |
| 94 | 94 | ||
| 95 | std::atomic<bool> running_mode{}; | 95 | std::atomic<bool> running_mode{}; |