From 3c85e376676f179d5fd3bf9f009207c543558838 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 12 Aug 2020 20:47:14 -0700 Subject: [PATCH] kern: use std::atomic_ref instead of reinterpret_cast to std::atomic --- libraries/libmesosphere/source/kern_k_scheduler.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/libraries/libmesosphere/source/kern_k_scheduler.cpp b/libraries/libmesosphere/source/kern_k_scheduler.cpp index 87da6e765..320abdcd8 100644 --- a/libraries/libmesosphere/source/kern_k_scheduler.cpp +++ b/libraries/libmesosphere/source/kern_k_scheduler.cpp @@ -276,10 +276,12 @@ namespace ams::kern { void KScheduler::ClearPreviousThread(KThread *thread) { MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread()); for (size_t i = 0; i < cpu::NumCores; ++i) { - std::atomic *prev_thread_ptr = reinterpret_cast *>(std::addressof(Kernel::GetScheduler(static_cast(i)).prev_thread)); - static_assert(sizeof(*prev_thread_ptr) == sizeof(KThread *)); + /* Get an atomic reference to the core scheduler's previous thread. */ + std::atomic_ref prev_thread(Kernel::GetScheduler(static_cast(i)).prev_thread); + static_assert(std::atomic_ref::is_always_lock_free); - prev_thread_ptr->compare_exchange_weak(thread, nullptr); + /* Atomically clear the previous thread if it's our target. */ + prev_thread.compare_exchange_weak(thread, nullptr); } }