From 9462e1273551b0e39ceb6ccb623eb4a8c2ab421f Mon Sep 17 00:00:00 2001 From: Ivan Chikish Date: Mon, 17 Jul 2023 07:33:25 +0300 Subject: [PATCH] Implement thread names --- orbis-kernel/include/orbis/thread/Thread.hpp | 4 +++- orbis-kernel/include/orbis/utils/IdMap.hpp | 13 +++++++++++++ orbis-kernel/src/sys/sys_sce.cpp | 15 +++++++++++++++ rpcsx-os/ops.cpp | 20 +++++++++++++------- 4 files changed, 44 insertions(+), 8 deletions(-) diff --git a/orbis-kernel/include/orbis/thread/Thread.hpp b/orbis-kernel/include/orbis/thread/Thread.hpp index a1f79c4..edc8b2a 100644 --- a/orbis-kernel/include/orbis/thread/Thread.hpp +++ b/orbis-kernel/include/orbis/thread/Thread.hpp @@ -5,6 +5,7 @@ #include "types.hpp" #include "../utils/SharedMutex.hpp" +#include namespace orbis { struct Process; @@ -17,12 +18,13 @@ struct Thread { ptr stackEnd; uint64_t fsBase{}; uint64_t gsBase{}; - char name[32]; + char name[32]{}; uint64_t sigMask[4] = {0x7fff'ffff, 0}; lwpid_t tid = -1; ThreadState state = ThreadState::INACTIVE; + std::thread handle; // FIXME: implement thread destruction void incRef() {} diff --git a/orbis-kernel/include/orbis/utils/IdMap.hpp b/orbis-kernel/include/orbis/utils/IdMap.hpp index ea23c19..416388f 100644 --- a/orbis-kernel/include/orbis/utils/IdMap.hpp +++ b/orbis-kernel/include/orbis/utils/IdMap.hpp @@ -338,6 +338,19 @@ struct OwningIdMap { fullChunks.clear(chunk); return true; } + + void walk(auto cb) { + for (std::size_t chunk = 0; chunk < ChunkCount; ++chunk) { + std::size_t index = chunks[chunk].mask.countr_zero(); + + while (index < ChunkSize) { + auto id = static_cast(index + chunk * ChunkSize + MinId); + cb(id, chunks[chunk].get(id)); + + index = chunks[chunk].mask.countr_zero(index + 1); + } + } + } }; } // namespace utils } // namespace orbis diff --git a/orbis-kernel/src/sys/sys_sce.cpp b/orbis-kernel/src/sys/sys_sce.cpp index f83421f..435fd7d 100644 --- a/orbis-kernel/src/sys/sys_sce.cpp +++ b/orbis-kernel/src/sys/sys_sce.cpp @@ -488,6 +488,21 @@ orbis::SysResult orbis::sys_mname(Thread *thread, uint64_t addr, uint64_t len, std::abort(); if (!thread->tproc->namedMem.count(addr)) std::abort(); + + std::lock_guard lock2(thread->tproc->mtx); + thread->tproc->threadsMap.walk([&](auto id, auto obj) { + if (obj->stackStart == (void *)addr) { + std::string name = std::to_string(obj->tid) + "_" + _name; + if (name.size() > 15) + name.resize(15); + ORBIS_LOG_NOTICE("sys_mname: setting thread name", obj->tid, name, + obj->handle.native_handle()); + if (!pthread_setname_np(obj->handle.native_handle(), name.c_str())) { + perror("pthread_setname_np"); + } + } + }); + return {}; } orbis::SysResult orbis::sys_dynlib_dlopen(Thread *thread /* TODO */) { diff --git a/rpcsx-os/ops.cpp b/rpcsx-os/ops.cpp index 7f316d0..3b881cb 100644 --- a/rpcsx-os/ops.cpp +++ b/rpcsx-os/ops.cpp @@ -477,6 +477,7 @@ SysResult thr_new(orbis::Thread *thread, orbis::ptr param, } auto proc = thread->tproc; + std::lock_guard lock(proc->mtx); auto [baseId, childThread] = proc->threadsMap.emplace(); childThread->tproc = proc; childThread->tid = proc->pid + baseId; @@ -493,11 +494,10 @@ SysResult thr_new(orbis::Thread *thread, orbis::ptr param, // FIXME: implement scheduler - std::printf("Starting child thread %lu\n", (long)(proc->pid + baseId)); + ORBIS_LOG_NOTICE("Starting child thread", childThread->tid, + childThread->stackStart); - std::thread{[=, childThread = Ref(childThread)] { - pthread_setname_np(pthread_self(), - std::to_string(childThread->tid).c_str()); + auto stdthr = std::thread{[=, childThread = Ref(childThread)] { stack_t ss; auto sigStackSize = std::max( @@ -506,7 +506,7 @@ SysResult thr_new(orbis::Thread *thread, orbis::ptr param, ss.ss_sp = malloc(sigStackSize); if (ss.ss_sp == NULL) { perror("malloc"); - exit(EXIT_FAILURE); + ::exit(EXIT_FAILURE); } ss.ss_size = sigStackSize; @@ -514,7 +514,7 @@ SysResult thr_new(orbis::Thread *thread, orbis::ptr param, if (sigaltstack(&ss, NULL) == -1) { perror("sigaltstack"); - exit(EXIT_FAILURE); + ::exit(EXIT_FAILURE); } static_cast( @@ -533,8 +533,14 @@ SysResult thr_new(orbis::Thread *thread, orbis::ptr param, childThread->context = context; childThread->state = orbis::ThreadState::RUNNING; rx::thread::invoke(childThread.get()); - }}.detach(); + }}; + if (pthread_setname_np(stdthr.native_handle(), + std::to_string(childThread->tid).c_str())) { + perror("pthread_setname_np"); + } + + childThread->handle = std::move(stdthr); return {}; } SysResult thr_exit(orbis::Thread *thread, orbis::ptr state) {