Overview
TheXThread class represents an Xbox 360 guest thread, managing thread state, execution, APCs (Asynchronous Procedure Calls), priority, and affinity.
Thread stacks are allocated in the guest address range
0x70000000 to 0x7F000000.Class Definition
Construction
XThread()
Constructs a new guest thread.kernel_state- Pointer to the kernel statestack_size- Size of thread stack in bytesxapi_thread_startup- Guest address of XAPI startup routinestart_address- Guest address of thread entry pointstart_context- Context parameter passed to threadcreation_flags- Thread creation flags (seeX_CREATE_SUSPENDED)guest_thread-trueif created by guest codemain_thread-trueif this is the main entry point thread
Thread Lifecycle
Create()
Creates and initializes the thread.X_STATUS_SUCCESS on success)
Execute()
Begins thread execution.Exit()
Exits the thread with an exit code.exit_code- Thread exit code
Terminate()
Forcibly terminates the thread.exit_code- Thread exit code
Reenter()
Re-enters thread execution at a specific address.address- Guest address to resume execution
Thread Control
Suspend()
Suspends the thread.out_suspend_count- Optional pointer to receive previous suspend count
Resume()
Resumes the thread.out_suspend_count- Optional pointer to receive previous suspend count
Delay()
Delays thread execution for a specified interval.processor_mode- Processor mode (user/kernel)alertable- Whether the delay is alertable for APCsinterval- Delay interval in 100-nanosecond units
Priority and Affinity
SetPriority()
Sets the thread priority.increment- Priority increment value
QueryPriority()
Queries the current thread priority.priority()
Gets the thread priority.SetAffinity()
Sets the processor affinity mask.affinity- Processor affinity bitmask
Xbox 360 has 3 cores with 2 hardware threads each:
- Thread 0-1: Core 0
- Thread 2-3: Core 1 (sometimes XContent)
- Thread 4-5: Core 2 (often XAudio)
SetActiveCpu()
Sets the active CPU for the thread.cpu_index- CPU index (0-5)
active_cpu()
Gets the active CPU index.Critical Regions and IRQL
EnterCriticalRegion()
Enters a critical region (disables APCs).LeaveCriticalRegion()
Leaves a critical region (re-enables APCs).RaiseIrql()
Raises the thread’s IRQL.new_irql- New IRQL level
LowerIrql()
Lowers the thread’s IRQL.new_irql- IRQL level to lower to
Asynchronous Procedure Calls (APCs)
EnqueueApc()
Enqueues an APC for execution on this thread.normal_routine- Guest address of APC routinenormal_context- Context parameterarg1- First argumentarg2- Second argument
CheckApcs()
Checks and delivers pending APCs.LockApc()
Locks the APC queue.UnlockApc()
Unlocks the APC queue.queue_delivery- Whether to queue APC delivery after unlocking
apc_list()
Gets the APC list.Thread Local Storage (TLS)
GetTLSValue()
Gets a TLS value.slot- TLS slot indexvalue_out- Pointer to receive the value
true on success
SetTLSValue()
Sets a TLS value.slot- TLS slot indexvalue- Value to set
true on success
tls_ptr()
Gets the TLS base address.Current Thread Access
GetCurrentThread()
Gets the currently executing XThread.nullptr
GetCurrentThreadHandle()
Gets the current thread’s handle.GetCurrentThreadId()
Gets the current thread’s ID.IsInThread()
Checks if code is executing in a specific thread.other- Thread to check against
true if in the specified thread
Error Handling
GetLastError()
Gets the last error code for the current thread.SetLastError()
Sets the last error code for the current thread.error_code- Error code to set
last_error()
Gets the thread’s last error code.set_last_error()
Sets the thread’s last error code.error_code- Error code to set
Properties
thread_id()
Gets the thread ID.pcr_ptr()
Gets the Processor Control Region (PCR) address.is_guest_thread()
Checks if the thread was created by guest code.true if created by guest
main_thread()
Checks if this is the main entry point thread.true if main thread
is_running()
Checks if the thread is running.true if running
creation_params()
Gets the thread creation parameters.suspend_count()
Gets the thread suspend count.set_name()
Sets the thread name.name- Thread name
thread()
Gets the underlying host thread.thread_state()
Gets the PowerPC thread state.State Management
Save()
Saves thread state to a byte stream.stream- Byte stream to write to
true on success
Restore()
Restores thread state from a byte stream.kernel_state- Kernel state pointerstream- Byte stream to read from