From 0aa3eadca7ab7af90e95521b1dc0075a5689e388 Mon Sep 17 00:00:00 2001 From: MechaCat02 Date: Mon, 13 Jul 2026 20:42:54 +0200 Subject: [PATCH] [Kernel] Add null-safe XThread::TryGetCurrentThread() accessor The additive event_log extraction tracer needs a current-thread lookup that returns nullptr instead of asserting when called from boot code before any XThread exists (GetCurrentThread asserts on non-guest threads). New read-only static accessor over the thread-local; no behaviour change. Fixes the native Linux build of the phase-a args/file.read tracer. Co-Authored-By: Claude Opus 4.8 --- src/xenia/kernel/xthread.cc | 2 ++ src/xenia/kernel/xthread.h | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/xenia/kernel/xthread.cc b/src/xenia/kernel/xthread.cc index e2c4ecedc..657c0fb37 100644 --- a/src/xenia/kernel/xthread.cc +++ b/src/xenia/kernel/xthread.cc @@ -117,6 +117,8 @@ XThread* XThread::GetCurrentThread() { return thread; } +XThread* XThread::TryGetCurrentThread() { return current_xthread_tls_; } + uint32_t XThread::GetCurrentThreadHandle() { XThread* thread = XThread::GetCurrentThread(); return thread->handle(); diff --git a/src/xenia/kernel/xthread.h b/src/xenia/kernel/xthread.h index 2c1e75856..31ec31398 100644 --- a/src/xenia/kernel/xthread.h +++ b/src/xenia/kernel/xthread.h @@ -408,6 +408,11 @@ class XThread : public XObject, public cpu::Thread { static bool IsInThread(XThread* other); static bool IsInThread(); static XThread* GetCurrentThread(); + // Null-safe variant of GetCurrentThread: returns nullptr (instead of + // asserting) when the calling OS thread is not a guest XThread. Used by + // additive instrumentation that can fire from boot code before any XThread + // exists. Read-only accessor; no behaviour change. + static XThread* TryGetCurrentThread(); static uint32_t GetCurrentThreadHandle(); static uint32_t GetCurrentThreadId();