From ad8a2022ec80102d4b3d5138a5ea7b1b07dbac6b Mon Sep 17 00:00:00 2001 From: IndecisiveTurtle <47210458+raphaelthegreat@users.noreply.github.com> Date: Wed, 23 Oct 2024 23:38:40 +0300 Subject: [PATCH] File cleanup pt4 --- src/core/libraries/kernel/threads.h | 1 + src/core/libraries/kernel/threads/condvar.cpp | 135 +----------------- src/core/libraries/kernel/threads/mutex.cpp | 6 +- src/core/libraries/kernel/threads/pthread.h | 10 +- .../libraries/kernel/threads/pthread_attr.cpp | 2 +- .../libraries/kernel/threads/thread_state.cpp | 6 +- 6 files changed, 11 insertions(+), 149 deletions(-) diff --git a/src/core/libraries/kernel/threads.h b/src/core/libraries/kernel/threads.h index a5795989..4ad9644e 100644 --- a/src/core/libraries/kernel/threads.h +++ b/src/core/libraries/kernel/threads.h @@ -3,6 +3,7 @@ #pragma once +#include #include "core/libraries/kernel/threads/pthread.h" namespace Core::Loader { diff --git a/src/core/libraries/kernel/threads/condvar.cpp b/src/core/libraries/kernel/threads/condvar.cpp index 2f387a4d..800a0326 100644 --- a/src/core/libraries/kernel/threads/condvar.cpp +++ b/src/core/libraries/kernel/threads/condvar.cpp @@ -1,8 +1,7 @@ // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#pragma clang optimize off + #include -#include "common/assert.h" #include "core/libraries/error_codes.h" #include "core/libraries/kernel/kernel.h" #include "core/libraries/kernel/threads/pthread.h" @@ -21,10 +20,7 @@ static constexpr PthreadCondAttr PhreadCondattrDefault = { }; static int CondInit(PthreadCondT* cond, const PthreadCondAttrT* cond_attr, const char* name) { - auto* cvp = (PthreadCond*)malloc(sizeof(PthreadCond)); - std::memset(cvp, 0, sizeof(PthreadCond)); - std::construct_at(cvp); - + auto* cvp = new PthreadCond{}; if (cvp == nullptr) { return POSIX_ENOMEM; } @@ -90,135 +86,10 @@ int PS4_SYSV_ABI posix_pthread_cond_destroy(PthreadCondT* cond) { } cvp = *cond; *cond = THR_COND_DESTROYED; - free(cvp); + delete cvp; return 0; } -static std::mutex sc_lock; -static std::unordered_map sc_table; - -void _sleepq_lock(void* wchan) { - sc_lock.lock(); -} - -void _sleepq_unlock(void* wchan) { - sc_lock.unlock(); -} - -SleepQueue* _sleepq_lookup(void* wchan) { - const auto it = sc_table.find(wchan); - if (it != sc_table.end()) { - return it->second; - } - return nullptr; -} - -void _sleepq_add(void* wchan, Pthread* td) { - SleepQueue* sq = _sleepq_lookup(wchan); - if (sq != NULL) { - sq->sq_freeq.push_front(td->sleepqueue); - } else { - sc_table.emplace(wchan, td->sleepqueue); - td->sleepqueue->sq_wchan = wchan; - } - td->sleepqueue = nullptr; - td->wchan = wchan; - sq->sq_blocked.push_front(td); -} - -bool _sleepq_remove(SleepQueue* sq, Pthread* td) { - std::erase(sq->sq_blocked, td); - if (sq->sq_blocked.empty()) { - sc_table.erase(td->wchan); - td->sleepqueue = sq; - td->wchan = nullptr; - return false; - } else { - td->sleepqueue = sq->sq_freeq.front(); - sq->sq_freeq.pop_front(); - td->wchan = nullptr; - return true; - } -} - -void _sleepq_drop(SleepQueue* sq, void (*cb)(Pthread*, void* arg), void* arg) { - if (sq->sq_blocked.empty()) { - return; - } - Pthread* td = sq->sq_blocked.front(); - sc_table.erase(td->wchan); - std::erase(sq->sq_blocked, td); - - cb(td, arg); - td->sleepqueue = sq; - td->wchan = nullptr; - - auto sq2 = sq->sq_freeq.begin(); - for (Pthread* td : sq->sq_blocked) { - cb(td, arg); - td->sleepqueue = *sq2; - td->wchan = NULL; - sq2++; - } - - sq->sq_blocked.clear(); - sq->sq_freeq.clear(); -} - -/*static int cond_wait_user(PthreadCond *cvp, PthreadMutex* mp, - const OrbisKernelTimespec *abstime, int cancel) { - Pthread* curthread = g_curthread; - int recurse; - int error; - - ASSERT_MSG(curthread->wchan == nullptr, "Thread was already on queue"); - //if (cancel) - //_thr_testcancel(curthread); - - _sleepq_lock(cvp); - cvp->has_user_waiters = 1; - curthread->will_sleep = 1; - _mutex_cv_unlock(mp, &recurse); - curthread->mutex_obj = mp; - _sleepq_add(cvp, curthread); - for(;;) { - _thr_clear_wake(curthread); - _sleepq_unlock(cvp); - - if (cancel) { - //_thr_cancel_enter2(curthread, 0); - error = _thr_sleep(curthread, cvp->__clock_id, abstime); - //_thr_cancel_leave(curthread, 0); - } else { - error = _thr_sleep(curthread, cvp->__clock_id, abstime); - } - - _sleepq_lock(cvp); - if (curthread->wchan == nullptr) { - error = 0; - break; - } else if (cancel && curthread->ShouldCancel()) { - SleepQueue* sq = _sleepq_lookup(cvp); - cvp->has_user_waiters = _sleepq_remove(sq, curthread); - _sleepq_unlock(cvp); - curthread->mutex_obj = NULL; - _mutex_cv_lock(mp, recurse); - if (!THR_IN_CRITICAL(curthread)) - _pthread_exit(PTHREAD_CANCELED); - else - return (0); - } else if (error == POSIX_ETIMEDOUT) { - SleepQueue* sq = _sleepq_lookup(cvp); - cvp->has_user_waiters = _sleepq_remove(sq, curthread); - break; - } - } - _sleepq_unlock(cvp); - curthread->mutex_obj = NULL; - _mutex_cv_lock(mp, recurse); - return (error); -}*/ - int PthreadCond::Wait(PthreadMutexT* mutex, const OrbisKernelTimespec* abstime) { PthreadMutex* mp = *mutex; if (int error = mp->IsOwned(g_curthread); error != 0) { diff --git a/src/core/libraries/kernel/threads/mutex.cpp b/src/core/libraries/kernel/threads/mutex.cpp index 51e49db0..884e3910 100644 --- a/src/core/libraries/kernel/threads/mutex.cpp +++ b/src/core/libraries/kernel/threads/mutex.cpp @@ -53,9 +53,7 @@ static int MutexInit(PthreadMutexT* mutex, const PthreadMutexAttr* mutex_attr, c return POSIX_EINVAL; } } - auto* pmutex = (PthreadMutex*)malloc(sizeof(PthreadMutex)); - std::memset(pmutex, 0, sizeof(PthreadMutex)); - std::construct_at(pmutex); + auto* pmutex = new PthreadMutex{}; if (pmutex == nullptr) { return POSIX_ENOMEM; } @@ -115,7 +113,7 @@ int PS4_SYSV_ABI posix_pthread_mutex_destroy(PthreadMutexT* mutex) { return POSIX_EBUSY; } *mutex = THR_MUTEX_DESTROYED; - free(m); + delete m; return 0; } diff --git a/src/core/libraries/kernel/threads/pthread.h b/src/core/libraries/kernel/threads/pthread.h index 57d8d38e..9cb89dfe 100644 --- a/src/core/libraries/kernel/threads/pthread.h +++ b/src/core/libraries/kernel/threads/pthread.h @@ -5,10 +5,10 @@ #include #include +#include #include #include #include -#include #include "common/enum.h" #include "core/libraries/kernel/time.h" @@ -230,12 +230,6 @@ using PthreadEntryFunc = void* (*)(void*); constexpr u32 TidTerminated = 1; -struct WakeAddr { - WakeAddr* link; - u32 value; - char pad[12]; -}; - struct SleepQueue { std::list sq_blocked; std::forward_list sq_freeq; @@ -291,7 +285,7 @@ struct Pthread { int report_events; int event_mask; std::string name; - WakeAddr* wake_addr; + std::binary_semaphore wake_sema{0}; SleepQueue* sleepqueue; void* wchan; PthreadMutex* mutex_obj; diff --git a/src/core/libraries/kernel/threads/pthread_attr.cpp b/src/core/libraries/kernel/threads/pthread_attr.cpp index 8d96693b..24f66795 100644 --- a/src/core/libraries/kernel/threads/pthread_attr.cpp +++ b/src/core/libraries/kernel/threads/pthread_attr.cpp @@ -1,6 +1,6 @@ // SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project // SPDX-License-Identifier: GPL-2.0-or-later -#pragma clang optimize off + #include "core/libraries/error_codes.h" #include "core/libraries/kernel/kernel.h" #include "core/libraries/kernel/threads/pthread.h" diff --git a/src/core/libraries/kernel/threads/thread_state.cpp b/src/core/libraries/kernel/threads/thread_state.cpp index ee644552..ddc4d460 100644 --- a/src/core/libraries/kernel/threads/thread_state.cpp +++ b/src/core/libraries/kernel/threads/thread_state.cpp @@ -102,8 +102,7 @@ Pthread* ThreadState::Alloc(Pthread* curthread) { memset(thread, 0, sizeof(Pthread)); std::construct_at(thread); thread->tcb = tcb; - // thread->sleepqueue = _sleepq_alloc(); - // thread->wake_addr = _thr_alloc_wake_addr(); + thread->sleepqueue = new SleepQueue{}; } else { thread_heap.Free(thread); total_threads.fetch_sub(1); @@ -122,8 +121,7 @@ void ThreadState::Free(Pthread* curthread, Pthread* thread) { thread->tcb = nullptr; std::destroy_at(thread); if (free_threads.size() >= MaxCachedThreads) { - //_sleepq_free(thread->sleepqueue); - //_thr_release_wake_addr(thread->wake_addr); + delete thread->sleepqueue; thread_heap.Free(thread); total_threads.fetch_sub(1); } else {