29#include <rex/kernel/thread_state.h>
49 ThreadRef(u32 thread_id_in, ThreadRegistry* registry_in)
63 virtual u32 GetLastError() = 0;
65 virtual void SetLastError(u32 error_code) = 0;
100 u32 start_context, u32 creation_flags,
bool guest_thread,
bool main_thread =
false,
101 u32 title_process = 0);
115 static bool IsInThread();
119 static u32 GetCurrentThreadHandle();
135 static void SleepFor(std::chrono::microseconds duration);
151 u32 GetGuestObject()
const {
return guest_object_ptr_; }
154 template <
typename T>
156 return GetAddressSpace()->TranslateVirtual<T*>(guest_object_ptr_);
159 const std::string& GetName()
const {
return thread_name_; }
166 return {
crash_name_.data(), crash_name_length_.load(std::memory_order_acquire)};
169 const CreationParams* GetCreationParams()
const {
return &creation_params_; }
171 u32 GetTLSPtr()
const {
return tls_static_address_; }
173 u32 GetPCRPtr()
const {
return pcr_address_; }
175 bool IsGuestThread()
const {
return guest_thread_; }
179 bool IsRunning()
const {
return running_; }
181 u32 GetThreadID()
const {
return thread_id_; }
183 u32 GetLastError()
override;
185 void SetLastError(u32 error_code)
override;
195 [[noreturn]]
void Exit(
int exit_code);
226 void EnqueueAPC(u32 normal_routine, u32 normal_context, u32 arg1, u32 arg2);
228 i32 GetPriority()
const {
return priority_; }
230 i32 GetHostPriority();
232 void SetPriority(i32 increment);
243 u8 GetActiveCPU()
const;
245 void SetActiveCPU(u8 cpu_index);
258 u32 GetSuspendCount();
289 void InitializeGuestObject();
292 void CallThreadNotifyRoutines(
bool create);
300 u32 tls_static_address_ = 0;
301 u32 tls_dynamic_address_ = 0;
304 u32 pcr_address_ = 0;
305 u32 stack_alloc_base_ = 0;
308 u32 stack_limit_ = 0;
309 bool guest_thread_ =
false;
310 bool main_thread_ =
false;
311 std::atomic<bool> running_{
false};
321 std::string thread_name_;
324 std::atomic<u8> crash_name_length_{0};
325 std::unique_ptr<kernel::ThreadState> thread_state_;
340 std::unique_ptr<rex::platform::Thread> thread_ =
nullptr;
344 friend class ThreadRegistry;
358 void SetHandle(X_HANDLE handle) { handle_ = handle; }
359 void SetGuestObject(u32 address) { guest_object_ptr_ = address; }
361 void Finish(
int exit_code);
362 [[noreturn]]
static void ThrowStop();
364 bool ShouldStop()
const;
366 void WaitForResume(std::unique_lock<std::mutex>& lock);
370 void ReleaseReference();
380 void TakeDeferredSuspend();
386 u32 GetProcessAddress()
const;
388 void WriteExitState(u32 exit_code);
390 TitleRuntime* runtime_;
391 ThreadRegistry* registry_ =
nullptr;
392 X_HANDLE handle_ = 0;
393 u32 guest_object_ptr_ = 0;
395 std::atomic<bool> released_{
false};
396 std::atomic<bool> abandoned_{
false};
397 bool exited_ =
false;
398 bool handles_closed_ =
false;
Definition kernel_thread.h:59
virtual bool IsMainThread() const =0
KernelThread(TitleRuntime &runtime, u32 stack_size, u32 xapi_thread_startup, u32 start_address, u32 start_context, u32 creation_flags, bool guest_thread, bool main_thread=false, u32 title_process=0)
T * GetGuestObject()
Definition kernel_thread.h:155
bool SetTLSValue(u32 slot, u32 value)
static void WriteKernelTime()
static void SleepFor(std::chrono::microseconds duration)
static constexpr u32 kStackAddressRangeEnd
Definition kernel_thread.h:77
std::string_view GetCrashName() const
Definition kernel_thread.h:165
X_STATUS Delay(u32 processor_mode, u32 alertable, u64 interval) const
bool GetTLSValue(u32 slot, u32 *value_out) const
PinCount pins
Definition kernel_thread.h:285
std::atomic< bool > suspend_deferred_
Definition kernel_thread.h:336
void RequestStop(i32 exit_code=0)
u32 scratch_size_
Definition kernel_thread.h:299
X_STATUS Suspend(u32 *out_suspend_count=nullptr)
X_HANDLE GetHandle() const
Definition kernel_thread.h:149
bool IsMainThread() const override
Definition kernel_thread.h:177
void LeaveCriticalRegion()
void SetName(const std::string_view name)
X_STATUS Resume(u32 *out_suspend_count=nullptr)
KernelThread(TitleRuntime &runtime, u32 stack_size, u32 creation_flags, rex::MoveOnlyFunction< int()> host_fn)
std::atomic< bool > exiting_
Definition kernel_thread.h:313
std::atomic< i32 > stop_exit_code_
Definition kernel_thread.h:315
bool stop_requested_
Definition kernel_thread.h:319
std::array< char, 64 > crash_name_
Definition kernel_thread.h:323
void EnterCriticalRegion()
std::unique_ptr< SuspendWait > suspend_wait_
Definition kernel_thread.h:334
bool SetAffinity(u32 affinity)
std::mutex stop_mutex_
Definition kernel_thread.h:317
std::atomic< u32 > tls_slot_count_
Definition kernel_thread.h:303
u32 stack_base_
Definition kernel_thread.h:307
static bool IsStopPending()
std::mutex thread_lock_
Definition kernel_thread.h:338
static constexpr u32 kStackAddressRangeBegin
Definition kernel_thread.h:75
u32 scratch_address_
Definition kernel_thread.h:298
Definition thread_state.h:40
Definition title_runtime.h:85
Virtual memory flags and typed guest pointers.
Guest list and spin lock structures.
u32 X_STATUS
Definition status.h:23
typename detail::MoveOnlyFunctionFor< Signature >::type MoveOnlyFunction
Definition utility.h:254
Definition kernel_thread.h:79
u32 title_process
Definition kernel_thread.h:85
u32 xapi_thread_startup
Definition kernel_thread.h:81
u32 start_context
Definition kernel_thread.h:83
u32 stack_size
Definition kernel_thread.h:80
u32 start_address
Definition kernel_thread.h:82
u32 creation_flags
Definition kernel_thread.h:84
u32 thread_id
Definition kernel_thread.h:54
ThreadRegistry * registry
Definition kernel_thread.h:55
Integer types, byte order wrappers and size literals.
Move only function, const propagating pointer and scope guard.