kernel: More thread code clean-up. (#3599)

This commit is contained in:
squidbus
2025-09-14 02:22:13 -07:00
committed by GitHub
parent c42d0cff2e
commit fb090dc90f
15 changed files with 84 additions and 89 deletions

View File

@@ -13,16 +13,16 @@ namespace Libraries::Kernel {
static std::mutex CondStaticLock; static std::mutex CondStaticLock;
#define THR_COND_INITIALIZER ((PthreadCond*)NULL) #define THR_COND_INITIALIZER ((PthreadCond*)nullptr)
#define THR_COND_DESTROYED ((PthreadCond*)1) #define THR_COND_DESTROYED ((PthreadCond*)1)
static constexpr PthreadCondAttr PhreadCondattrDefault = { static constexpr PthreadCondAttr PthreadCondattrDefault = {
.c_pshared = 0, .c_pshared = 0,
.c_clockid = ClockId::Realtime, .c_clockid = ClockId::Realtime,
}; };
static int CondInit(PthreadCondT* cond, const PthreadCondAttrT* cond_attr, const char* name) { 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) { if (cvp == nullptr) {
return POSIX_ENOMEM; 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) { int PthreadCond::Wait(PthreadMutexT* mutex, const OrbisKernelTimespec* abstime, u64 usec) {
PthreadMutex* mp = *mutex; PthreadMutex* mp = *mutex;
if (int error = mp->IsOwned(g_curthread); error != 0) { if (const int error = mp->IsOwned(g_curthread); error != 0) {
return error; return error;
} }
@@ -107,7 +107,7 @@ int PthreadCond::Wait(PthreadMutexT* mutex, const OrbisKernelTimespec* abstime,
* set __has_user_waiters before unlocking mutex, this allows * set __has_user_waiters before unlocking mutex, this allows
* us to check it without locking in pthread_cond_signal(). * us to check it without locking in pthread_cond_signal().
*/ */
has_user_waiters = 1; has_user_waiters = true;
curthread->will_sleep = true; curthread->will_sleep = true;
int recurse; int recurse;
@@ -145,7 +145,7 @@ int PthreadCond::Wait(PthreadMutexT* mutex, const OrbisKernelTimespec* abstime,
} }
SleepqUnlock(this); SleepqUnlock(this);
curthread->mutex_obj = nullptr; curthread->mutex_obj = nullptr;
int error2 = mp->CvLock(recurse); const int error2 = mp->CvLock(recurse);
if (error == 0) { if (error == 0) {
error = error2; error = error2;
} }
@@ -222,8 +222,8 @@ int PthreadCond::Broadcast() {
ba.count = 0; ba.count = 0;
const auto drop_cb = [](Pthread* td, void* arg) { const auto drop_cb = [](Pthread* td, void* arg) {
BroadcastArg* ba = reinterpret_cast<BroadcastArg*>(arg); auto* ba2 = static_cast<BroadcastArg*>(arg);
Pthread* curthread = ba->curthread; Pthread* curthread = ba2->curthread;
PthreadMutex* mp = td->mutex_obj; PthreadMutex* mp = td->mutex_obj;
if (mp->m_owner == curthread) { if (mp->m_owner == curthread) {
@@ -233,13 +233,13 @@ int PthreadCond::Broadcast() {
curthread->defer_waiters[curthread->nwaiter_defer++] = &td->wake_sema; curthread->defer_waiters[curthread->nwaiter_defer++] = &td->wake_sema;
mp->m_flags |= PthreadMutexFlags::Deferred; mp->m_flags |= PthreadMutexFlags::Deferred;
} else { } else {
if (ba->count >= Pthread::MaxDeferWaiters) { if (ba2->count >= Pthread::MaxDeferWaiters) {
for (int i = 0; i < ba->count; i++) { for (int i = 0; i < ba2->count; i++) {
ba->waddrs[i]->release(); 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); SleepqDrop(sq, drop_cb, &ba);
has_user_waiters = 0; has_user_waiters = false;
SleepqUnlock(this); SleepqUnlock(this);
for (int i = 0; i < ba.count; i++) { 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) { int PS4_SYSV_ABI posix_pthread_condattr_init(PthreadCondAttrT* attr) {
PthreadCondAttr* pattr = new PthreadCondAttr{}; auto* pattr = new (std::nothrow) PthreadCondAttr{};
if (pattr == nullptr) { if (pattr == nullptr) {
return POSIX_ENOMEM; return POSIX_ENOMEM;
} }
memcpy(pattr, &PhreadCondattrDefault, sizeof(PthreadCondAttr)); memcpy(pattr, &PthreadCondattrDefault, sizeof(PthreadCondAttr));
*attr = pattr; *attr = pattr;
return 0; return 0;
} }
@@ -302,7 +302,7 @@ int PS4_SYSV_ABI posix_pthread_condattr_getclock(const PthreadCondAttrT* attr, C
if (attr == nullptr || *attr == nullptr) { if (attr == nullptr || *attr == nullptr) {
return POSIX_EINVAL; return POSIX_EINVAL;
} }
*clock_id = static_cast<ClockId>((*attr)->c_clockid); *clock_id = (*attr)->c_clockid;
return 0; return 0;
} }

View File

@@ -187,8 +187,7 @@ int PS4_SYSV_ABI sceKernelCreateEventFlag(OrbisKernelEventFlag* ef, const char*
if (ef == nullptr || pName == nullptr) { if (ef == nullptr || pName == nullptr) {
return ORBIS_KERNEL_ERROR_EINVAL; return ORBIS_KERNEL_ERROR_EINVAL;
} }
if (pOptParam || !pName || if (pOptParam || attr > (ORBIS_KERNEL_EVF_ATTR_MULTI | ORBIS_KERNEL_EVF_ATTR_TH_PRIO)) {
attr > (ORBIS_KERNEL_EVF_ATTR_MULTI | ORBIS_KERNEL_EVF_ATTR_TH_PRIO)) {
return ORBIS_KERNEL_ERROR_EINVAL; return ORBIS_KERNEL_ERROR_EINVAL;
} }
@@ -364,8 +363,7 @@ int PS4_SYSV_ABI sceKernelWaitEventFlag(OrbisKernelEventFlag ef, u64 bitPattern,
UNREACHABLE(); 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) { if (result != ORBIS_OK && result != ORBIS_KERNEL_ERROR_ETIMEDOUT) {
LOG_DEBUG(Kernel_Event, "returned {:#x}", result); LOG_DEBUG(Kernel_Event, "returned {:#x}", result);
} }

View File

@@ -10,7 +10,7 @@
#ifdef _WIN64 #ifdef _WIN64
#include "common/ntapi.h" #include "common/ntapi.h"
#else #else
#include <signal.h> #include <csignal>
#endif #endif
namespace Libraries::Kernel { namespace Libraries::Kernel {

View File

@@ -41,7 +41,7 @@ using CallocFun = void* (*)(size_t, size_t);
static int MutexInit(PthreadMutexT* mutex, const PthreadMutexAttr* mutex_attr, const char* name) { static int MutexInit(PthreadMutexT* mutex, const PthreadMutexAttr* mutex_attr, const char* name) {
const PthreadMutexAttr* attr; const PthreadMutexAttr* attr;
if (mutex_attr == NULL) { if (mutex_attr == nullptr) {
attr = &PthreadMutexattrDefault; attr = &PthreadMutexattrDefault;
} else { } else {
attr = mutex_attr; attr = mutex_attr;
@@ -52,7 +52,7 @@ static int MutexInit(PthreadMutexT* mutex, const PthreadMutexAttr* mutex_attr, c
return POSIX_EINVAL; return POSIX_EINVAL;
} }
} }
auto* pmutex = new PthreadMutex{}; auto* pmutex = new (std::nothrow) PthreadMutex{};
if (pmutex == nullptr) { if (pmutex == nullptr) {
return POSIX_ENOMEM; return POSIX_ENOMEM;
} }
@@ -350,7 +350,7 @@ int PthreadMutex::IsOwned(Pthread* curthread) const {
} }
int PS4_SYSV_ABI posix_pthread_mutexattr_init(PthreadMutexAttrT* attr) { int PS4_SYSV_ABI posix_pthread_mutexattr_init(PthreadMutexAttrT* attr) {
PthreadMutexAttrT pattr = new PthreadMutexAttr{}; auto pattr = new (std::nothrow) PthreadMutexAttr{};
if (pattr == nullptr) { if (pattr == nullptr) {
return POSIX_ENOMEM; return POSIX_ENOMEM;
} }

View File

@@ -74,10 +74,10 @@ void PS4_SYSV_ABI posix_pthread_exit(void* status) {
fmt::ptr(curthread)); fmt::ptr(curthread));
/* Flag this thread as exiting. */ /* Flag this thread as exiting. */
curthread->cancelling = 1; curthread->cancelling = true;
curthread->no_cancel = 1; curthread->no_cancel = true;
curthread->cancel_async = 0; curthread->cancel_async = false;
curthread->cancel_point = 0; curthread->cancel_point = false;
/* Save the return value: */ /* Save the return value: */
curthread->ret = status; curthread->ret = status;
@@ -107,7 +107,7 @@ static int JoinThread(PthreadT pthread, void** thread_return, const OrbisKernelT
} }
auto* thread_state = ThrState::Instance(); 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; return POSIX_ESRCH;
} }
@@ -127,9 +127,9 @@ static int JoinThread(PthreadT pthread, void** thread_return, const OrbisKernelT
pthread->lock.unlock(); pthread->lock.unlock();
const auto backout_join = [](void* arg) PS4_SYSV_ABI { const auto backout_join = [](void* arg) PS4_SYSV_ABI {
Pthread* pthread = (Pthread*)arg; auto* pthread2 = static_cast<Pthread*>(arg);
std::scoped_lock lk{pthread->lock}; std::scoped_lock lk{pthread2->lock};
pthread->joiner = nullptr; pthread2->joiner = nullptr;
}; };
PthreadCleanup cup{backout_join, pthread, 0}; PthreadCleanup cup{backout_join, pthread, 0};
@@ -137,7 +137,7 @@ static int JoinThread(PthreadT pthread, void** thread_return, const OrbisKernelT
//_thr_cancel_enter(curthread); //_thr_cancel_enter(curthread);
const int tid = pthread->tid; const s32 tid = pthread->tid;
while (pthread->tid.load() != TidTerminated) { while (pthread->tid.load() != TidTerminated) {
//_thr_testcancel(curthread); //_thr_testcancel(curthread);
ASSERT(abstime == nullptr); 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) { 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, 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(); 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; return ret;
} }
/* Check if the thread is already detached or has a joiner. */ /* 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(); pthread->lock.unlock();
return POSIX_EINVAL; return POSIX_EINVAL;
} }
@@ -201,7 +201,7 @@ int PS4_SYSV_ABI posix_pthread_detach(PthreadT pthread) {
} }
static void RunThread(void* arg) { static void RunThread(void* arg) {
Pthread* curthread = (Pthread*)arg; auto* curthread = static_cast<Pthread*>(arg);
g_curthread = curthread; g_curthread = curthread;
Common::SetCurrentThreadName(curthread->name.c_str()); Common::SetCurrentThreadName(curthread->name.c_str());
DebugState.AddCurrentThreadToGuestList(); DebugState.AddCurrentThreadToGuestList();
@@ -244,7 +244,7 @@ int PS4_SYSV_ABI posix_pthread_create_name_np(PthreadT* thread, const PthreadAtt
static int TidCounter = 1; static int TidCounter = 1;
new_thread->tid = ++TidCounter; new_thread->tid = ++TidCounter;
if (new_thread->attr.stackaddr_attr == 0) { if (new_thread->attr.stackaddr_attr == nullptr) {
/* Add additional stack space for HLE */ /* Add additional stack space for HLE */
static constexpr size_t AdditionalStack = 128_KB; static constexpr size_t AdditionalStack = 128_KB;
new_thread->attr.stacksize_attr += AdditionalStack; 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->magic = Pthread::ThrMagic;
new_thread->start_routine = start_routine; new_thread->start_routine = start_routine;
new_thread->arg = arg; new_thread->arg = arg;
new_thread->cancel_enable = 1; new_thread->cancel_enable = true;
new_thread->cancel_async = 0; new_thread->cancel_async = false;
auto* memory = Core::Memory::Instance(); auto* memory = Core::Memory::Instance();
if (name && memory->IsValidAddress(name)) { 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 { const auto once_cancel_handler = [](void* arg) PS4_SYSV_ABI {
PthreadOnce* once_control = (PthreadOnce*)arg; auto* once_control2 = static_cast<PthreadOnce*>(arg);
auto state = PthreadOnceState::InProgress; auto state = PthreadOnceState::InProgress;
if (once_control->state.compare_exchange_strong(state, PthreadOnceState::NeverDone, if (once_control2->state.compare_exchange_strong(state, PthreadOnceState::NeverDone,
std::memory_order_release)) { std::memory_order_release)) {
return; return;
} }
once_control->state.store(PthreadOnceState::NeverDone, std::memory_order_release); once_control2->state.store(PthreadOnceState::NeverDone, std::memory_order_release);
once_control->state.notify_all(); once_control2->state.notify_all();
}; };
PthreadCleanup cup{once_cancel_handler, once_control, 0}; 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(); auto* thread_state = ThrState::Instance();
/* Find the thread in the list of active threads. */ /* 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; return ret;
} }
pthread->lock.lock(); pthread->lock.lock();
@@ -450,7 +450,7 @@ int PS4_SYSV_ABI posix_pthread_setschedparam(PthreadT pthread, SchedPolicy polic
auto* thread_state = ThrState::Instance(); auto* thread_state = ThrState::Instance();
if (pthread == g_curthread) { if (pthread == g_curthread) {
g_curthread->lock.lock(); 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; 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) { int PS4_SYSV_ABI posix_pthread_setprio(PthreadT thread, int prio) {
SchedParam param; SchedParam param;
param.sched_priority = prio; param.sched_priority = prio;
auto* thread_state = ThrState::Instance(); auto* thread_state = ThrState::Instance();
if (thread == g_curthread) { if (thread == g_curthread) {
g_curthread->lock.lock(); 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; return ret;
} }
@@ -507,7 +506,7 @@ enum class PthreadCancelState : u32 {
#define POSIX_PTHREAD_CANCELED ((void*)1) #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]] { if (curthread->ShouldCancel() && !curthread->InCritical()) [[unlikely]] {
posix_pthread_exit(POSIX_PTHREAD_CANCELED); posix_pthread_exit(POSIX_PTHREAD_CANCELED);
} }
@@ -519,10 +518,10 @@ int PS4_SYSV_ABI posix_pthread_setcancelstate(PthreadCancelState state,
int oldval = curthread->cancel_enable; int oldval = curthread->cancel_enable;
switch (state) { switch (state) {
case PthreadCancelState::Disable: case PthreadCancelState::Disable:
curthread->cancel_enable = 0; curthread->cancel_enable = false;
break; break;
case PthreadCancelState::Enable: case PthreadCancelState::Enable:
curthread->cancel_enable = 1; curthread->cancel_enable = true;
TestCancel(curthread); TestCancel(curthread);
break; break;
default: default:
@@ -588,7 +587,8 @@ int PS4_SYSV_ABI posix_pthread_getaffinity_np(PthreadT thread, size_t cpusetsize
auto* thread_state = ThrState::Instance(); auto* thread_state = ThrState::Instance();
if (thread == g_curthread) { if (thread == g_curthread) {
g_curthread->lock.lock(); 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; return ret;
} }
@@ -608,7 +608,8 @@ int PS4_SYSV_ABI posix_pthread_setaffinity_np(PthreadT thread, size_t cpusetsize
auto* thread_state = ThrState::Instance(); auto* thread_state = ThrState::Instance();
if (thread == g_curthread) { if (thread == g_curthread) {
g_curthread->lock.lock(); 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; return ret;
} }

View File

@@ -7,7 +7,6 @@
#include <forward_list> #include <forward_list>
#include <list> #include <list>
#include <mutex> #include <mutex>
#include <semaphore>
#include <shared_mutex> #include <shared_mutex>
#include "common/enum.h" #include "common/enum.h"
@@ -55,7 +54,7 @@ struct PthreadMutex {
PthreadMutexProt m_protocol; PthreadMutexProt m_protocol;
std::string name; std::string name;
PthreadMutexType Type() const noexcept { [[nodiscard]] PthreadMutexType Type() const noexcept {
return static_cast<PthreadMutexType>(m_flags & PthreadMutexFlags::TypeMask); return static_cast<PthreadMutexType>(m_flags & PthreadMutexFlags::TypeMask);
} }
@@ -115,8 +114,8 @@ enum class ClockId : u32 {
}; };
struct PthreadCond { struct PthreadCond {
u32 has_user_waiters; bool has_user_waiters;
u32 has_kern_waiters; bool has_kern_waiters;
u32 flags; u32 flags;
ClockId clock_id; ClockId clock_id;
std::string name; std::string name;
@@ -239,7 +238,7 @@ enum class ThreadListFlags : u32 {
using PthreadEntryFunc = void* PS4_SYSV_ABI (*)(void*); using PthreadEntryFunc = void* PS4_SYSV_ABI (*)(void*);
constexpr u32 TidTerminated = 1; constexpr s32 TidTerminated = 1;
struct SleepQueue; struct SleepQueue;
@@ -253,7 +252,7 @@ struct Pthread {
static constexpr u32 ThrMagic = 0xd09ba115U; static constexpr u32 ThrMagic = 0xd09ba115U;
static constexpr u32 MaxDeferWaiters = 50; static constexpr u32 MaxDeferWaiters = 50;
std::atomic<long> tid; std::atomic<s32> tid;
std::mutex lock; std::mutex lock;
u32 cycle; u32 cycle;
int locklevel; int locklevel;

View File

@@ -29,7 +29,7 @@ PthreadAttr PthreadAttrDefault = {
.prio = 0, .prio = 0,
.suspend = false, .suspend = false,
.flags = PthreadAttrFlags::ScopeSystem, .flags = PthreadAttrFlags::ScopeSystem,
.stackaddr_attr = NULL, .stackaddr_attr = nullptr,
.stacksize_attr = ThrStackDefault, .stacksize_attr = ThrStackDefault,
.guardsize_attr = 0, .guardsize_attr = 0,
.cpusetsize = 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) { int PS4_SYSV_ABI posix_pthread_attr_init(PthreadAttrT* attr) {
PthreadAttrT pattr = new PthreadAttr{}; auto pattr = new (std::nothrow) PthreadAttr{};
if (pattr == nullptr) { if (pattr == nullptr) {
return POSIX_ENOMEM; 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) { 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; return POSIX_EINVAL;
} else if ((policy < SchedPolicy::Fifo) || (policy > SchedPolicy::RoundRobin)) { } else if ((policy < SchedPolicy::Fifo) || (policy > SchedPolicy::RoundRobin)) {
return POSIX_ENOTSUP; return POSIX_ENOTSUP;
@@ -216,7 +216,7 @@ int PS4_SYSV_ABI posix_pthread_attr_get_np(PthreadT pthread, PthreadAttrT* dstat
return POSIX_EINVAL; return POSIX_EINVAL;
} }
auto* thread_state = ThrState::Instance(); 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) { if (ret != 0) {
return ret; return ret;
} }
@@ -225,9 +225,7 @@ int PS4_SYSV_ABI posix_pthread_attr_get_np(PthreadT pthread, PthreadAttrT* dstat
attr.flags |= PthreadAttrFlags::Detached; attr.flags |= PthreadAttrFlags::Detached;
} }
pthread->lock.unlock(); pthread->lock.unlock();
if (ret == 0) {
memcpy(dst, &attr, sizeof(PthreadAttr)); memcpy(dst, &attr, sizeof(PthreadAttr));
}
return ret; return ret;
} }

View File

@@ -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) { void PS4_SYSV_ABI posix_pthread_cleanup_push(PthreadCleanupFunc routine, void* arg) {
Pthread* curthread = g_curthread; Pthread* curthread = g_curthread;
PthreadCleanup* newbuf = new PthreadCleanup{}; auto* newbuf = new (std::nothrow) PthreadCleanup{};
if (newbuf == nullptr) { if (newbuf == nullptr) {
return; return;
} }

View File

@@ -21,7 +21,7 @@ int PS4_SYSV_ABI posix_pthread_key_create(PthreadKeyT* key, PthreadKeyDestructor
it->allocated = 1; it->allocated = 1;
it->destructor = destructor; it->destructor = destructor;
it->seqno++; it->seqno++;
*key = std::distance(ThreadKeytable.begin(), it); *key = static_cast<PthreadKeyT>(std::distance(ThreadKeytable.begin(), it));
return 0; return 0;
} }
return POSIX_EAGAIN; return POSIX_EAGAIN;
@@ -44,7 +44,7 @@ int PS4_SYSV_ABI posix_pthread_key_delete(PthreadKeyT key) {
void _thread_cleanupspecific() { void _thread_cleanupspecific() {
Pthread* curthread = g_curthread; Pthread* curthread = g_curthread;
PthreadKeyDestructor destructor; PthreadKeyDestructor destructor;
const void* data = NULL; const void* data = nullptr;
if (curthread->specific == nullptr) { if (curthread->specific == nullptr) {
return; return;
@@ -63,7 +63,7 @@ void _thread_cleanupspecific() {
} }
curthread->specific[key].data = nullptr; curthread->specific[key].data = nullptr;
curthread->specific_data_count--; 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 * This can happen if the key is deleted via
* pthread_key_delete without first setting the value * 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 PS4_SYSV_ABI posix_pthread_setspecific(PthreadKeyT key, const void* value) {
int ret = 0;
Pthread* pthread = g_curthread; Pthread* pthread = g_curthread;
if (!pthread->specific) { if (!pthread->specific) {
pthread->specific = new PthreadSpecificElem[PthreadKeysMax]{}; pthread->specific = new (std::nothrow) PthreadSpecificElem[PthreadKeysMax]{};
if (!pthread->specific) { if (pthread->specific == nullptr) {
return POSIX_ENOMEM; return POSIX_ENOMEM;
} }
} }

View File

@@ -27,7 +27,7 @@ static std::mutex RwlockStaticLock;
} }
static int RwlockInit(PthreadRwlockT* rwlock, const PthreadRwlockAttrT* attr) { static int RwlockInit(PthreadRwlockT* rwlock, const PthreadRwlockAttrT* attr) {
PthreadRwlock* prwlock = new PthreadRwlock{}; auto* prwlock = new (std::nothrow) PthreadRwlock{};
if (prwlock == nullptr) { if (prwlock == nullptr) {
return POSIX_ENOMEM; return POSIX_ENOMEM;
} }
@@ -213,7 +213,7 @@ int PS4_SYSV_ABI posix_pthread_rwlockattr_init(PthreadRwlockAttrT* rwlockattr) {
return POSIX_EINVAL; return POSIX_EINVAL;
} }
PthreadRwlockAttrT prwlockattr = new PthreadRwlockAttr{}; auto prwlockattr = new (std::nothrow) PthreadRwlockAttr{};
if (prwlockattr == nullptr) { if (prwlockattr == nullptr) {
return POSIX_ENOMEM; return POSIX_ENOMEM;
} }

View File

@@ -127,7 +127,7 @@ public:
thr_name = g_curthread->name; thr_name = g_curthread->name;
} }
s32 GetResult() const { [[nodiscard]] s32 GetResult() const {
if (was_signaled) { if (was_signaled) {
return ORBIS_OK; return ORBIS_OK;
} }

View File

@@ -58,18 +58,18 @@ void SleepqAdd(void* wchan, Pthread* td) {
sq->sq_blocked.push_front(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); std::erase(sq->sq_blocked, td);
if (sq->sq_blocked.empty()) { if (sq->sq_blocked.empty()) {
td->sleepqueue = sq; td->sleepqueue = sq;
sq->unlink(); sq->unlink();
td->wchan = nullptr; td->wchan = nullptr;
return 0; return false;
} else { } else {
td->sleepqueue = std::addressof(sq->sq_freeq.front()); td->sleepqueue = std::addressof(sq->sq_freeq.front());
sq->sq_freeq.pop_front(); sq->sq_freeq.pop_front();
td->wchan = nullptr; td->wchan = nullptr;
return 1; return true;
} }
} }

View File

@@ -30,7 +30,7 @@ SleepQueue* SleepqLookup(void* wchan);
void SleepqAdd(void* wchan, Pthread* td); 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); void SleepqDrop(SleepQueue* sq, void (*callback)(Pthread*, void*), void* arg);

View File

@@ -16,7 +16,7 @@ static constexpr size_t RoundUp(size_t size) {
} }
int ThreadState::CreateStack(PthreadAttr* attr) { int ThreadState::CreateStack(PthreadAttr* attr) {
if ((attr->stackaddr_attr) != NULL) { if ((attr->stackaddr_attr) != nullptr) {
attr->guardsize_attr = 0; attr->guardsize_attr = 0;
attr->flags |= PthreadAttrFlags::StackUser; attr->flags |= PthreadAttrFlags::StackUser;
return 0; return 0;
@@ -32,7 +32,7 @@ int ThreadState::CreateStack(PthreadAttr* attr) {
size_t stacksize = RoundUp(attr->stacksize_attr); size_t stacksize = RoundUp(attr->stacksize_attr);
size_t guardsize = RoundUp(attr->guardsize_attr); size_t guardsize = RoundUp(attr->guardsize_attr);
attr->stackaddr_attr = NULL; attr->stackaddr_attr = nullptr;
attr->flags &= ~PthreadAttrFlags::StackUser; attr->flags &= ~PthreadAttrFlags::StackUser;
/* /*
@@ -69,7 +69,7 @@ int ThreadState::CreateStack(PthreadAttr* attr) {
} }
/* A cached stack was found. Release the lock. */ /* A cached stack was found. Release the lock. */
if (attr->stackaddr_attr != NULL) { if (attr->stackaddr_attr != nullptr) {
thread_list_lock.unlock(); thread_list_lock.unlock();
return 0; return 0;
} }
@@ -122,8 +122,8 @@ void ThreadState::FreeStack(PthreadAttr* attr) {
return; return;
} }
char* stack_base = (char*)attr->stackaddr_attr; auto* stack_base = static_cast<char*>(attr->stackaddr_attr);
Stack* spare_stack = (Stack*)(stack_base + attr->stacksize_attr - sizeof(Stack)); auto* spare_stack = reinterpret_cast<Stack*>(stack_base + attr->stacksize_attr - sizeof(Stack));
spare_stack->stacksize = RoundUp(attr->stacksize_attr); spare_stack->stacksize = RoundUp(attr->stacksize_attr);
spare_stack->guardsize = RoundUp(attr->guardsize_attr); spare_stack->guardsize = RoundUp(attr->guardsize_attr);
spare_stack->stackaddr = attr->stackaddr_attr; spare_stack->stackaddr = attr->stackaddr_attr;

View File

@@ -37,7 +37,7 @@ void ThreadState::Collect(Pthread* curthread) {
for (auto it = gc_list.begin(); it != gc_list.end();) { for (auto it = gc_list.begin(); it != gc_list.end();) {
Pthread* td = *it; Pthread* td = *it;
if (td->tid != TidTerminated) { if (td->tid != TidTerminated) {
it++; ++it;
continue; continue;
} }
FreeStack(&td->attr); 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) { if (thread == nullptr) {
return POSIX_EINVAL; return POSIX_EINVAL;
} }