Implement thread names

This commit is contained in:
Ivan Chikish 2023-07-17 07:33:25 +03:00
parent a1a91bb557
commit 9462e12735
4 changed files with 44 additions and 8 deletions

View file

@ -5,6 +5,7 @@
#include "types.hpp"
#include "../utils/SharedMutex.hpp"
#include <thread>
namespace orbis {
struct Process;
@ -17,12 +18,13 @@ struct Thread {
ptr<void> 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() {}

View file

@ -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<IdT>(index + chunk * ChunkSize + MinId);
cb(id, chunks[chunk].get(id));
index = chunks[chunk].mask.countr_zero(index + 1);
}
}
}
};
} // namespace utils
} // namespace orbis

View file

@ -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 */) {