From fb090dc90feb340efeea87430d253c110d547f53 Mon Sep 17 00:00:00 2001 From: squidbus <175574877+squidbus@users.noreply.github.com> Date: Sun, 14 Sep 2025 02:22:13 -0700 Subject: [PATCH] kernel: More thread code clean-up. (#3599) --- src/core/libraries/kernel/threads/condvar.cpp | 34 +++++------ .../libraries/kernel/threads/event_flag.cpp | 6 +- .../libraries/kernel/threads/exception.cpp | 2 +- src/core/libraries/kernel/threads/mutex.cpp | 6 +- src/core/libraries/kernel/threads/pthread.cpp | 61 ++++++++++--------- src/core/libraries/kernel/threads/pthread.h | 11 ++-- .../libraries/kernel/threads/pthread_attr.cpp | 12 ++-- .../kernel/threads/pthread_clean.cpp | 2 +- .../libraries/kernel/threads/pthread_spec.cpp | 11 ++-- src/core/libraries/kernel/threads/rwlock.cpp | 4 +- .../libraries/kernel/threads/semaphore.cpp | 2 +- src/core/libraries/kernel/threads/sleepq.cpp | 6 +- src/core/libraries/kernel/threads/sleepq.h | 2 +- src/core/libraries/kernel/threads/stack.cpp | 10 +-- .../libraries/kernel/threads/thread_state.cpp | 4 +- 15 files changed, 84 insertions(+), 89 deletions(-) diff --git a/src/core/libraries/kernel/threads/condvar.cpp b/src/core/libraries/kernel/threads/condvar.cpp index bd3822f09..d0b95133c 100644 --- a/src/core/libraries/kernel/threads/condvar.cpp +++ b/src/core/libraries/kernel/threads/condvar.cpp @@ -13,16 +13,16 @@ namespace Libraries::Kernel { static std::mutex CondStaticLock; -#define THR_COND_INITIALIZER ((PthreadCond*)NULL) +#define THR_COND_INITIALIZER ((PthreadCond*)nullptr) #define THR_COND_DESTROYED ((PthreadCond*)1) -static constexpr PthreadCondAttr PhreadCondattrDefault = { +static constexpr PthreadCondAttr PthreadCondattrDefault = { .c_pshared = 0, .c_clockid = ClockId::Realtime, }; static int CondInit(PthreadCondT* cond, const PthreadCondAttrT* cond_attr, const char* name) { - auto* cvp = new PthreadCond{}; + auto* cvp = new (std::nothrow) PthreadCond{}; if (cvp == nullptr) { return POSIX_ENOMEM; } @@ -94,7 +94,7 @@ int PS4_SYSV_ABI posix_pthread_cond_destroy(PthreadCondT* cond) { int PthreadCond::Wait(PthreadMutexT* mutex, const OrbisKernelTimespec* abstime, u64 usec) { PthreadMutex* mp = *mutex; - if (int error = mp->IsOwned(g_curthread); error != 0) { + if (const int error = mp->IsOwned(g_curthread); error != 0) { return error; } @@ -107,7 +107,7 @@ int PthreadCond::Wait(PthreadMutexT* mutex, const OrbisKernelTimespec* abstime, * set __has_user_waiters before unlocking mutex, this allows * us to check it without locking in pthread_cond_signal(). */ - has_user_waiters = 1; + has_user_waiters = true; curthread->will_sleep = true; int recurse; @@ -145,7 +145,7 @@ int PthreadCond::Wait(PthreadMutexT* mutex, const OrbisKernelTimespec* abstime, } SleepqUnlock(this); curthread->mutex_obj = nullptr; - int error2 = mp->CvLock(recurse); + const int error2 = mp->CvLock(recurse); if (error == 0) { error = error2; } @@ -222,8 +222,8 @@ int PthreadCond::Broadcast() { ba.count = 0; const auto drop_cb = [](Pthread* td, void* arg) { - BroadcastArg* ba = reinterpret_cast(arg); - Pthread* curthread = ba->curthread; + auto* ba2 = static_cast(arg); + Pthread* curthread = ba2->curthread; PthreadMutex* mp = td->mutex_obj; if (mp->m_owner == curthread) { @@ -233,13 +233,13 @@ int PthreadCond::Broadcast() { curthread->defer_waiters[curthread->nwaiter_defer++] = &td->wake_sema; mp->m_flags |= PthreadMutexFlags::Deferred; } else { - if (ba->count >= Pthread::MaxDeferWaiters) { - for (int i = 0; i < ba->count; i++) { - ba->waddrs[i]->release(); + if (ba2->count >= Pthread::MaxDeferWaiters) { + for (int i = 0; i < ba2->count; i++) { + ba2->waddrs[i]->release(); } - ba->count = 0; + ba2->count = 0; } - ba->waddrs[ba->count++] = &td->wake_sema; + ba2->waddrs[ba2->count++] = &td->wake_sema; } }; @@ -251,7 +251,7 @@ int PthreadCond::Broadcast() { } SleepqDrop(sq, drop_cb, &ba); - has_user_waiters = 0; + has_user_waiters = false; SleepqUnlock(this); for (int i = 0; i < ba.count; i++) { @@ -280,11 +280,11 @@ int PS4_SYSV_ABI posix_pthread_cond_broadcast(PthreadCondT* cond) { } int PS4_SYSV_ABI posix_pthread_condattr_init(PthreadCondAttrT* attr) { - PthreadCondAttr* pattr = new PthreadCondAttr{}; + auto* pattr = new (std::nothrow) PthreadCondAttr{}; if (pattr == nullptr) { return POSIX_ENOMEM; } - memcpy(pattr, &PhreadCondattrDefault, sizeof(PthreadCondAttr)); + memcpy(pattr, &PthreadCondattrDefault, sizeof(PthreadCondAttr)); *attr = pattr; return 0; } @@ -302,7 +302,7 @@ int PS4_SYSV_ABI posix_pthread_condattr_getclock(const PthreadCondAttrT* attr, C if (attr == nullptr || *attr == nullptr) { return POSIX_EINVAL; } - *clock_id = static_cast((*attr)->c_clockid); + *clock_id = (*attr)->c_clockid; return 0; } diff --git a/src/core/libraries/kernel/threads/event_flag.cpp b/src/core/libraries/kernel/threads/event_flag.cpp index d9d7d8b1d..7f2fdb163 100644 --- a/src/core/libraries/kernel/threads/event_flag.cpp +++ b/src/core/libraries/kernel/threads/event_flag.cpp @@ -187,8 +187,7 @@ int PS4_SYSV_ABI sceKernelCreateEventFlag(OrbisKernelEventFlag* ef, const char* if (ef == nullptr || pName == nullptr) { return ORBIS_KERNEL_ERROR_EINVAL; } - if (pOptParam || !pName || - attr > (ORBIS_KERNEL_EVF_ATTR_MULTI | ORBIS_KERNEL_EVF_ATTR_TH_PRIO)) { + if (pOptParam || attr > (ORBIS_KERNEL_EVF_ATTR_MULTI | ORBIS_KERNEL_EVF_ATTR_TH_PRIO)) { return ORBIS_KERNEL_ERROR_EINVAL; } @@ -364,8 +363,7 @@ int PS4_SYSV_ABI sceKernelWaitEventFlag(OrbisKernelEventFlag ef, u64 bitPattern, UNREACHABLE(); } - u32 result = ef->Wait(bitPattern, wait, clear, pResultPat, pTimeout); - + const int result = ef->Wait(bitPattern, wait, clear, pResultPat, pTimeout); if (result != ORBIS_OK && result != ORBIS_KERNEL_ERROR_ETIMEDOUT) { LOG_DEBUG(Kernel_Event, "returned {:#x}", result); } diff --git a/src/core/libraries/kernel/threads/exception.cpp b/src/core/libraries/kernel/threads/exception.cpp index 6c0add539..5455d425e 100644 --- a/src/core/libraries/kernel/threads/exception.cpp +++ b/src/core/libraries/kernel/threads/exception.cpp @@ -10,7 +10,7 @@ #ifdef _WIN64 #include "common/ntapi.h" #else -#include +#include #endif namespace Libraries::Kernel { diff --git a/src/core/libraries/kernel/threads/mutex.cpp b/src/core/libraries/kernel/threads/mutex.cpp index af5f43cb3..89c4b6a50 100644 --- a/src/core/libraries/kernel/threads/mutex.cpp +++ b/src/core/libraries/kernel/threads/mutex.cpp @@ -41,7 +41,7 @@ using CallocFun = void* (*)(size_t, size_t); static int MutexInit(PthreadMutexT* mutex, const PthreadMutexAttr* mutex_attr, const char* name) { const PthreadMutexAttr* attr; - if (mutex_attr == NULL) { + if (mutex_attr == nullptr) { attr = &PthreadMutexattrDefault; } else { attr = mutex_attr; @@ -52,7 +52,7 @@ static int MutexInit(PthreadMutexT* mutex, const PthreadMutexAttr* mutex_attr, c return POSIX_EINVAL; } } - auto* pmutex = new PthreadMutex{}; + auto* pmutex = new (std::nothrow) PthreadMutex{}; if (pmutex == nullptr) { return POSIX_ENOMEM; } @@ -350,7 +350,7 @@ int PthreadMutex::IsOwned(Pthread* curthread) const { } int PS4_SYSV_ABI posix_pthread_mutexattr_init(PthreadMutexAttrT* attr) { - PthreadMutexAttrT pattr = new PthreadMutexAttr{}; + auto pattr = new (std::nothrow) PthreadMutexAttr{}; if (pattr == nullptr) { return POSIX_ENOMEM; } diff --git a/src/core/libraries/kernel/threads/pthread.cpp b/src/core/libraries/kernel/threads/pthread.cpp index 1b75c8087..8e39a5bf3 100644 --- a/src/core/libraries/kernel/threads/pthread.cpp +++ b/src/core/libraries/kernel/threads/pthread.cpp @@ -74,10 +74,10 @@ void PS4_SYSV_ABI posix_pthread_exit(void* status) { fmt::ptr(curthread)); /* Flag this thread as exiting. */ - curthread->cancelling = 1; - curthread->no_cancel = 1; - curthread->cancel_async = 0; - curthread->cancel_point = 0; + curthread->cancelling = true; + curthread->no_cancel = true; + curthread->cancel_async = false; + curthread->cancel_point = false; /* Save the return value: */ curthread->ret = status; @@ -107,7 +107,7 @@ static int JoinThread(PthreadT pthread, void** thread_return, const OrbisKernelT } auto* thread_state = ThrState::Instance(); - if (int ret = thread_state->FindThread(pthread, 1); ret != 0) { + if (int ret = thread_state->FindThread(pthread, true); ret != 0) { return POSIX_ESRCH; } @@ -127,9 +127,9 @@ static int JoinThread(PthreadT pthread, void** thread_return, const OrbisKernelT pthread->lock.unlock(); const auto backout_join = [](void* arg) PS4_SYSV_ABI { - Pthread* pthread = (Pthread*)arg; - std::scoped_lock lk{pthread->lock}; - pthread->joiner = nullptr; + auto* pthread2 = static_cast(arg); + std::scoped_lock lk{pthread2->lock}; + pthread2->joiner = nullptr; }; PthreadCleanup cup{backout_join, pthread, 0}; @@ -137,7 +137,7 @@ static int JoinThread(PthreadT pthread, void** thread_return, const OrbisKernelT //_thr_cancel_enter(curthread); - const int tid = pthread->tid; + const s32 tid = pthread->tid; while (pthread->tid.load() != TidTerminated) { //_thr_testcancel(curthread); ASSERT(abstime == nullptr); @@ -165,7 +165,7 @@ static int JoinThread(PthreadT pthread, void** thread_return, const OrbisKernelT } int PS4_SYSV_ABI posix_pthread_join(PthreadT pthread, void** thread_return) { - return JoinThread(pthread, thread_return, NULL); + return JoinThread(pthread, thread_return, nullptr); } int PS4_SYSV_ABI posix_pthread_timedjoin_np(PthreadT pthread, void** thread_return, @@ -184,12 +184,12 @@ int PS4_SYSV_ABI posix_pthread_detach(PthreadT pthread) { } auto* thread_state = ThrState::Instance(); - if (int ret = thread_state->FindThread(pthread, 1); ret != 0) { + if (int ret = thread_state->FindThread(pthread, true); ret != 0) { return ret; } /* Check if the thread is already detached or has a joiner. */ - if (True(pthread->flags & ThreadFlags::Detached) || (pthread->joiner != NULL)) { + if (True(pthread->flags & ThreadFlags::Detached) || pthread->joiner != nullptr) { pthread->lock.unlock(); return POSIX_EINVAL; } @@ -201,7 +201,7 @@ int PS4_SYSV_ABI posix_pthread_detach(PthreadT pthread) { } static void RunThread(void* arg) { - Pthread* curthread = (Pthread*)arg; + auto* curthread = static_cast(arg); g_curthread = curthread; Common::SetCurrentThreadName(curthread->name.c_str()); DebugState.AddCurrentThreadToGuestList(); @@ -244,7 +244,7 @@ int PS4_SYSV_ABI posix_pthread_create_name_np(PthreadT* thread, const PthreadAtt static int TidCounter = 1; new_thread->tid = ++TidCounter; - if (new_thread->attr.stackaddr_attr == 0) { + if (new_thread->attr.stackaddr_attr == nullptr) { /* Add additional stack space for HLE */ static constexpr size_t AdditionalStack = 128_KB; new_thread->attr.stacksize_attr += AdditionalStack; @@ -263,8 +263,8 @@ int PS4_SYSV_ABI posix_pthread_create_name_np(PthreadT* thread, const PthreadAtt new_thread->magic = Pthread::ThrMagic; new_thread->start_routine = start_routine; new_thread->arg = arg; - new_thread->cancel_enable = 1; - new_thread->cancel_async = 0; + new_thread->cancel_enable = true; + new_thread->cancel_async = false; auto* memory = Core::Memory::Instance(); if (name && memory->IsValidAddress(name)) { @@ -365,15 +365,15 @@ int PS4_SYSV_ABI posix_pthread_once(PthreadOnce* once_control, } const auto once_cancel_handler = [](void* arg) PS4_SYSV_ABI { - PthreadOnce* once_control = (PthreadOnce*)arg; + auto* once_control2 = static_cast(arg); auto state = PthreadOnceState::InProgress; - if (once_control->state.compare_exchange_strong(state, PthreadOnceState::NeverDone, - std::memory_order_release)) { + if (once_control2->state.compare_exchange_strong(state, PthreadOnceState::NeverDone, + std::memory_order_release)) { return; } - once_control->state.store(PthreadOnceState::NeverDone, std::memory_order_release); - once_control->state.notify_all(); + once_control2->state.store(PthreadOnceState::NeverDone, std::memory_order_release); + once_control2->state.notify_all(); }; PthreadCleanup cup{once_cancel_handler, once_control, 0}; @@ -430,7 +430,7 @@ int PS4_SYSV_ABI posix_pthread_getschedparam(PthreadT pthread, SchedPolicy* poli } auto* thread_state = ThrState::Instance(); /* Find the thread in the list of active threads. */ - if (int ret = thread_state->RefAdd(pthread, /*include dead*/ 0); ret != 0) { + if (int ret = thread_state->RefAdd(pthread, /*include dead*/ false); ret != 0) { return ret; } pthread->lock.lock(); @@ -450,7 +450,7 @@ int PS4_SYSV_ABI posix_pthread_setschedparam(PthreadT pthread, SchedPolicy polic auto* thread_state = ThrState::Instance(); if (pthread == g_curthread) { g_curthread->lock.lock(); - } else if (int ret = thread_state->FindThread(pthread, /*include dead*/ 0); ret != 0) { + } else if (int ret = thread_state->FindThread(pthread, /*include dead*/ false); ret != 0) { return ret; } @@ -479,13 +479,12 @@ int PS4_SYSV_ABI scePthreadGetprio(PthreadT thread, int* priority) { int PS4_SYSV_ABI posix_pthread_setprio(PthreadT thread, int prio) { SchedParam param; - param.sched_priority = prio; auto* thread_state = ThrState::Instance(); if (thread == g_curthread) { g_curthread->lock.lock(); - } else if (int ret = thread_state->FindThread(thread, /*include dead*/ 0); ret != 0) { + } else if (const int ret = thread_state->FindThread(thread, /*include dead*/ false); ret != 0) { return ret; } @@ -507,7 +506,7 @@ enum class PthreadCancelState : u32 { #define POSIX_PTHREAD_CANCELED ((void*)1) -static inline void TestCancel(Pthread* curthread) { +static inline void TestCancel(const Pthread* curthread) { if (curthread->ShouldCancel() && !curthread->InCritical()) [[unlikely]] { posix_pthread_exit(POSIX_PTHREAD_CANCELED); } @@ -519,10 +518,10 @@ int PS4_SYSV_ABI posix_pthread_setcancelstate(PthreadCancelState state, int oldval = curthread->cancel_enable; switch (state) { case PthreadCancelState::Disable: - curthread->cancel_enable = 0; + curthread->cancel_enable = false; break; case PthreadCancelState::Enable: - curthread->cancel_enable = 1; + curthread->cancel_enable = true; TestCancel(curthread); break; default: @@ -588,7 +587,8 @@ int PS4_SYSV_ABI posix_pthread_getaffinity_np(PthreadT thread, size_t cpusetsize auto* thread_state = ThrState::Instance(); if (thread == g_curthread) { g_curthread->lock.lock(); - } else if (auto ret = thread_state->FindThread(thread, /*include dead*/ 0); ret != 0) { + } else if (const auto ret = thread_state->FindThread(thread, /*include dead*/ false); + ret != 0) { return ret; } @@ -608,7 +608,8 @@ int PS4_SYSV_ABI posix_pthread_setaffinity_np(PthreadT thread, size_t cpusetsize auto* thread_state = ThrState::Instance(); if (thread == g_curthread) { g_curthread->lock.lock(); - } else if (auto ret = thread_state->FindThread(thread, /*include dead*/ 0); ret != 0) { + } else if (const auto ret = thread_state->FindThread(thread, /*include dead*/ false); + ret != 0) { return ret; } diff --git a/src/core/libraries/kernel/threads/pthread.h b/src/core/libraries/kernel/threads/pthread.h index f002598fd..a285c6caf 100644 --- a/src/core/libraries/kernel/threads/pthread.h +++ b/src/core/libraries/kernel/threads/pthread.h @@ -7,7 +7,6 @@ #include #include #include -#include #include #include "common/enum.h" @@ -55,7 +54,7 @@ struct PthreadMutex { PthreadMutexProt m_protocol; std::string name; - PthreadMutexType Type() const noexcept { + [[nodiscard]] PthreadMutexType Type() const noexcept { return static_cast(m_flags & PthreadMutexFlags::TypeMask); } @@ -115,8 +114,8 @@ enum class ClockId : u32 { }; struct PthreadCond { - u32 has_user_waiters; - u32 has_kern_waiters; + bool has_user_waiters; + bool has_kern_waiters; u32 flags; ClockId clock_id; std::string name; @@ -239,7 +238,7 @@ enum class ThreadListFlags : u32 { using PthreadEntryFunc = void* PS4_SYSV_ABI (*)(void*); -constexpr u32 TidTerminated = 1; +constexpr s32 TidTerminated = 1; struct SleepQueue; @@ -253,7 +252,7 @@ struct Pthread { static constexpr u32 ThrMagic = 0xd09ba115U; static constexpr u32 MaxDeferWaiters = 50; - std::atomic tid; + std::atomic tid; std::mutex lock; u32 cycle; int locklevel; diff --git a/src/core/libraries/kernel/threads/pthread_attr.cpp b/src/core/libraries/kernel/threads/pthread_attr.cpp index 85a6a353f..28e3c91c3 100644 --- a/src/core/libraries/kernel/threads/pthread_attr.cpp +++ b/src/core/libraries/kernel/threads/pthread_attr.cpp @@ -29,7 +29,7 @@ PthreadAttr PthreadAttrDefault = { .prio = 0, .suspend = false, .flags = PthreadAttrFlags::ScopeSystem, - .stackaddr_attr = NULL, + .stackaddr_attr = nullptr, .stacksize_attr = ThrStackDefault, .guardsize_attr = 0, .cpusetsize = 0, @@ -112,7 +112,7 @@ int PS4_SYSV_ABI posix_pthread_attr_getstacksize(const PthreadAttrT* attr, size_ } int PS4_SYSV_ABI posix_pthread_attr_init(PthreadAttrT* attr) { - PthreadAttrT pattr = new PthreadAttr{}; + auto pattr = new (std::nothrow) PthreadAttr{}; if (pattr == nullptr) { return POSIX_ENOMEM; } @@ -122,7 +122,7 @@ int PS4_SYSV_ABI posix_pthread_attr_init(PthreadAttrT* attr) { } int PS4_SYSV_ABI posix_pthread_attr_setschedpolicy(PthreadAttrT* attr, SchedPolicy policy) { - if (attr == NULL || *attr == NULL) { + if (attr == nullptr || *attr == nullptr) { return POSIX_EINVAL; } else if ((policy < SchedPolicy::Fifo) || (policy > SchedPolicy::RoundRobin)) { return POSIX_ENOTSUP; @@ -216,7 +216,7 @@ int PS4_SYSV_ABI posix_pthread_attr_get_np(PthreadT pthread, PthreadAttrT* dstat return POSIX_EINVAL; } auto* thread_state = ThrState::Instance(); - int ret = thread_state->FindThread(pthread, /*include dead*/ 0); + const int ret = thread_state->FindThread(pthread, /*include dead*/ false); if (ret != 0) { return ret; } @@ -225,9 +225,7 @@ int PS4_SYSV_ABI posix_pthread_attr_get_np(PthreadT pthread, PthreadAttrT* dstat attr.flags |= PthreadAttrFlags::Detached; } pthread->lock.unlock(); - if (ret == 0) { - memcpy(dst, &attr, sizeof(PthreadAttr)); - } + memcpy(dst, &attr, sizeof(PthreadAttr)); return ret; } diff --git a/src/core/libraries/kernel/threads/pthread_clean.cpp b/src/core/libraries/kernel/threads/pthread_clean.cpp index ce319b524..45a134b70 100644 --- a/src/core/libraries/kernel/threads/pthread_clean.cpp +++ b/src/core/libraries/kernel/threads/pthread_clean.cpp @@ -16,7 +16,7 @@ void PS4_SYSV_ABI __pthread_cleanup_push_imp(PthreadCleanupFunc routine, void* a void PS4_SYSV_ABI posix_pthread_cleanup_push(PthreadCleanupFunc routine, void* arg) { Pthread* curthread = g_curthread; - PthreadCleanup* newbuf = new PthreadCleanup{}; + auto* newbuf = new (std::nothrow) PthreadCleanup{}; if (newbuf == nullptr) { return; } diff --git a/src/core/libraries/kernel/threads/pthread_spec.cpp b/src/core/libraries/kernel/threads/pthread_spec.cpp index cb2da6b90..094866a5a 100644 --- a/src/core/libraries/kernel/threads/pthread_spec.cpp +++ b/src/core/libraries/kernel/threads/pthread_spec.cpp @@ -21,7 +21,7 @@ int PS4_SYSV_ABI posix_pthread_key_create(PthreadKeyT* key, PthreadKeyDestructor it->allocated = 1; it->destructor = destructor; it->seqno++; - *key = std::distance(ThreadKeytable.begin(), it); + *key = static_cast(std::distance(ThreadKeytable.begin(), it)); return 0; } return POSIX_EAGAIN; @@ -44,7 +44,7 @@ int PS4_SYSV_ABI posix_pthread_key_delete(PthreadKeyT key) { void _thread_cleanupspecific() { Pthread* curthread = g_curthread; PthreadKeyDestructor destructor; - const void* data = NULL; + const void* data = nullptr; if (curthread->specific == nullptr) { return; @@ -63,7 +63,7 @@ void _thread_cleanupspecific() { } curthread->specific[key].data = nullptr; curthread->specific_data_count--; - } else if (curthread->specific[key].data != NULL) { + } else if (curthread->specific[key].data != nullptr) { /* * This can happen if the key is deleted via * pthread_key_delete without first setting the value @@ -97,12 +97,11 @@ void _thread_cleanupspecific() { } int PS4_SYSV_ABI posix_pthread_setspecific(PthreadKeyT key, const void* value) { - int ret = 0; Pthread* pthread = g_curthread; if (!pthread->specific) { - pthread->specific = new PthreadSpecificElem[PthreadKeysMax]{}; - if (!pthread->specific) { + pthread->specific = new (std::nothrow) PthreadSpecificElem[PthreadKeysMax]{}; + if (pthread->specific == nullptr) { return POSIX_ENOMEM; } } diff --git a/src/core/libraries/kernel/threads/rwlock.cpp b/src/core/libraries/kernel/threads/rwlock.cpp index 73997ea19..52109b58d 100644 --- a/src/core/libraries/kernel/threads/rwlock.cpp +++ b/src/core/libraries/kernel/threads/rwlock.cpp @@ -27,7 +27,7 @@ static std::mutex RwlockStaticLock; } static int RwlockInit(PthreadRwlockT* rwlock, const PthreadRwlockAttrT* attr) { - PthreadRwlock* prwlock = new PthreadRwlock{}; + auto* prwlock = new (std::nothrow) PthreadRwlock{}; if (prwlock == nullptr) { return POSIX_ENOMEM; } @@ -213,7 +213,7 @@ int PS4_SYSV_ABI posix_pthread_rwlockattr_init(PthreadRwlockAttrT* rwlockattr) { return POSIX_EINVAL; } - PthreadRwlockAttrT prwlockattr = new PthreadRwlockAttr{}; + auto prwlockattr = new (std::nothrow) PthreadRwlockAttr{}; if (prwlockattr == nullptr) { return POSIX_ENOMEM; } diff --git a/src/core/libraries/kernel/threads/semaphore.cpp b/src/core/libraries/kernel/threads/semaphore.cpp index e612e63b8..721b2c21a 100644 --- a/src/core/libraries/kernel/threads/semaphore.cpp +++ b/src/core/libraries/kernel/threads/semaphore.cpp @@ -127,7 +127,7 @@ public: thr_name = g_curthread->name; } - s32 GetResult() const { + [[nodiscard]] s32 GetResult() const { if (was_signaled) { return ORBIS_OK; } diff --git a/src/core/libraries/kernel/threads/sleepq.cpp b/src/core/libraries/kernel/threads/sleepq.cpp index 7d21c91d4..cebbf3f01 100644 --- a/src/core/libraries/kernel/threads/sleepq.cpp +++ b/src/core/libraries/kernel/threads/sleepq.cpp @@ -58,18 +58,18 @@ void SleepqAdd(void* wchan, Pthread* td) { sq->sq_blocked.push_front(td); } -int SleepqRemove(SleepQueue* sq, Pthread* td) { +bool SleepqRemove(SleepQueue* sq, Pthread* td) { std::erase(sq->sq_blocked, td); if (sq->sq_blocked.empty()) { td->sleepqueue = sq; sq->unlink(); td->wchan = nullptr; - return 0; + return false; } else { td->sleepqueue = std::addressof(sq->sq_freeq.front()); sq->sq_freeq.pop_front(); td->wchan = nullptr; - return 1; + return true; } } diff --git a/src/core/libraries/kernel/threads/sleepq.h b/src/core/libraries/kernel/threads/sleepq.h index 5dc37645d..a85aeca51 100644 --- a/src/core/libraries/kernel/threads/sleepq.h +++ b/src/core/libraries/kernel/threads/sleepq.h @@ -30,7 +30,7 @@ SleepQueue* SleepqLookup(void* wchan); void SleepqAdd(void* wchan, Pthread* td); -int SleepqRemove(SleepQueue* sq, Pthread* td); +bool SleepqRemove(SleepQueue* sq, Pthread* td); void SleepqDrop(SleepQueue* sq, void (*callback)(Pthread*, void*), void* arg); diff --git a/src/core/libraries/kernel/threads/stack.cpp b/src/core/libraries/kernel/threads/stack.cpp index 45715482a..0f3a4d59d 100644 --- a/src/core/libraries/kernel/threads/stack.cpp +++ b/src/core/libraries/kernel/threads/stack.cpp @@ -16,7 +16,7 @@ static constexpr size_t RoundUp(size_t size) { } int ThreadState::CreateStack(PthreadAttr* attr) { - if ((attr->stackaddr_attr) != NULL) { + if ((attr->stackaddr_attr) != nullptr) { attr->guardsize_attr = 0; attr->flags |= PthreadAttrFlags::StackUser; return 0; @@ -32,7 +32,7 @@ int ThreadState::CreateStack(PthreadAttr* attr) { size_t stacksize = RoundUp(attr->stacksize_attr); size_t guardsize = RoundUp(attr->guardsize_attr); - attr->stackaddr_attr = NULL; + attr->stackaddr_attr = nullptr; attr->flags &= ~PthreadAttrFlags::StackUser; /* @@ -69,7 +69,7 @@ int ThreadState::CreateStack(PthreadAttr* attr) { } /* A cached stack was found. Release the lock. */ - if (attr->stackaddr_attr != NULL) { + if (attr->stackaddr_attr != nullptr) { thread_list_lock.unlock(); return 0; } @@ -122,8 +122,8 @@ void ThreadState::FreeStack(PthreadAttr* attr) { return; } - char* stack_base = (char*)attr->stackaddr_attr; - Stack* spare_stack = (Stack*)(stack_base + attr->stacksize_attr - sizeof(Stack)); + auto* stack_base = static_cast(attr->stackaddr_attr); + auto* spare_stack = reinterpret_cast(stack_base + attr->stacksize_attr - sizeof(Stack)); spare_stack->stacksize = RoundUp(attr->stacksize_attr); spare_stack->guardsize = RoundUp(attr->guardsize_attr); spare_stack->stackaddr = attr->stackaddr_attr; diff --git a/src/core/libraries/kernel/threads/thread_state.cpp b/src/core/libraries/kernel/threads/thread_state.cpp index 6c2d4d7ef..d5ca475fe 100644 --- a/src/core/libraries/kernel/threads/thread_state.cpp +++ b/src/core/libraries/kernel/threads/thread_state.cpp @@ -37,7 +37,7 @@ void ThreadState::Collect(Pthread* curthread) { for (auto it = gc_list.begin(); it != gc_list.end();) { Pthread* td = *it; if (td->tid != TidTerminated) { - it++; + ++it; continue; } FreeStack(&td->attr); @@ -131,7 +131,7 @@ void ThreadState::Free(Pthread* curthread, Pthread* thread) { } } -int ThreadState::FindThread(Pthread* thread, bool include_dead) { +int ThreadState::FindThread(Pthread* thread, const bool include_dead) { if (thread == nullptr) { return POSIX_EINVAL; }