Fix deadlocks by enabling reuse of exited threads (#855)
Some checks are pending
Reuse / reuse (push) Waiting to run
Clang Format / clang-format (push) Waiting to run
Linux-Qt / build (push) Waiting to run
Linux / build (push) Waiting to run
macOS-Qt / build (push) Waiting to run
macOS / build (push) Waiting to run
Windows-Qt / build (push) Waiting to run
Windows / build (push) Waiting to run

Simplify loop

const correctness

Simplify setting is_free

Co-authored-by: Adam Jones <a.c.jones@outlook.com>
This commit is contained in:
adjonesey 2024-09-09 21:13:28 +01:00 committed by GitHub
parent f23c6dc852
commit dcab06ff2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 9 additions and 6 deletions

View file

@ -54,11 +54,12 @@ void init_pthreads() {
} }
void pthreadInitSelfMainThread() { void pthreadInitSelfMainThread() {
const char* name = "Main_Thread";
auto* pthread_pool = g_pthread_cxt->GetPthreadPool(); auto* pthread_pool = g_pthread_cxt->GetPthreadPool();
g_pthread_self = pthread_pool->Create(); g_pthread_self = pthread_pool->Create(name);
scePthreadAttrInit(&g_pthread_self->attr); scePthreadAttrInit(&g_pthread_self->attr);
g_pthread_self->pth = pthread_self(); g_pthread_self->pth = pthread_self();
g_pthread_self->name = "Main_Thread"; g_pthread_self->name = name;
} }
int PS4_SYSV_ABI scePthreadAttrInit(ScePthreadAttr* attr) { int PS4_SYSV_ABI scePthreadAttrInit(ScePthreadAttr* attr) {
@ -1016,7 +1017,7 @@ int PS4_SYSV_ABI scePthreadCreate(ScePthread* thread, const ScePthreadAttr* attr
attr = g_pthread_cxt->GetDefaultAttr(); attr = g_pthread_cxt->GetDefaultAttr();
} }
*thread = pthread_pool->Create(); *thread = pthread_pool->Create(name);
if ((*thread)->attr != nullptr) { if ((*thread)->attr != nullptr) {
scePthreadAttrDestroy(&(*thread)->attr); scePthreadAttrDestroy(&(*thread)->attr);
@ -1058,11 +1059,11 @@ int PS4_SYSV_ABI scePthreadCreate(ScePthread* thread, const ScePthreadAttr* attr
} }
} }
ScePthread PThreadPool::Create() { ScePthread PThreadPool::Create(const char* name) {
std::scoped_lock lock{m_mutex}; std::scoped_lock lock{m_mutex};
for (auto* p : m_threads) { for (auto* p : m_threads) {
if (p->is_free) { if (p->is_free && p->name == name) {
p->is_free = false; p->is_free = false;
return p; return p;
} }
@ -1491,6 +1492,8 @@ int PS4_SYSV_ABI scePthreadOnce(int* once_control, void (*init_routine)(void)) {
} }
[[noreturn]] void PS4_SYSV_ABI scePthreadExit(void* value_ptr) { [[noreturn]] void PS4_SYSV_ABI scePthreadExit(void* value_ptr) {
g_pthread_self->is_free = true;
pthread_exit(value_ptr); pthread_exit(value_ptr);
UNREACHABLE(); UNREACHABLE();
} }

View file

@ -119,7 +119,7 @@ struct PthreadSemInternal {
class PThreadPool { class PThreadPool {
public: public:
ScePthread Create(); ScePthread Create(const char* name);
private: private:
std::vector<ScePthread> m_threads; std::vector<ScePthread> m_threads;