Merge pull request #6878 from BreadFish64/optimize-GetHostThreadID

kernel: Optimize GetHostThreadID
master
Ameer J 2021-08-24 00:01:13 +07:00 committed by GitHub
commit bed0c3c92a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 10 deletions

@ -261,20 +261,23 @@ struct KernelCore::Impl {
current_process = process; current_process = process;
} }
/// Creates a new host thread ID, should only be called by GetHostThreadId static inline thread_local u32 host_thread_id = UINT32_MAX;
u32 AllocateHostThreadId(std::optional<std::size_t> core_id) {
if (core_id) { /// Gets the host thread ID for the caller, allocating a new one if this is the first time
// The first for slots are reserved for CPU core threads u32 GetHostThreadId(std::size_t core_id) {
ASSERT(*core_id < Core::Hardware::NUM_CPU_CORES); if (host_thread_id == UINT32_MAX) {
return static_cast<u32>(*core_id); // The first four slots are reserved for CPU core threads
} else { ASSERT(core_id < Core::Hardware::NUM_CPU_CORES);
return next_host_thread_id++; host_thread_id = static_cast<u32>(core_id);
} }
return host_thread_id;
} }
/// Gets the host thread ID for the caller, allocating a new one if this is the first time /// Gets the host thread ID for the caller, allocating a new one if this is the first time
u32 GetHostThreadId(std::optional<std::size_t> core_id = std::nullopt) { u32 GetHostThreadId() {
const thread_local auto host_thread_id{AllocateHostThreadId(core_id)}; if (host_thread_id == UINT32_MAX) {
host_thread_id = next_host_thread_id++;
}
return host_thread_id; return host_thread_id;
} }