pub struct Thread { /* private fields */ }
Expand description
Main thread handle with RAII resource management.
This represents a thread and automatically manages its resources through reference counting. When the last reference is dropped, the thread’s stack and other resources are automatically cleaned up.
Implementations§
Source§impl Thread
impl Thread
Sourcepub fn new(
id: ThreadId,
stack: Stack,
entry_point: fn(),
priority: u8,
) -> (Self, JoinHandle)
pub fn new( id: ThreadId, stack: Stack, entry_point: fn(), priority: u8, ) -> (Self, JoinHandle)
Create a new thread with the given parameters.
§Arguments
id
- Unique identifier for this threadstack
- Stack allocated for this threadentry_point
- Function to execute in this threadpriority
- Thread priority (0-255, higher = more important)
§Returns
A new Thread instance and corresponding JoinHandle.
Sourcepub fn state(&self) -> ThreadState
pub fn state(&self) -> ThreadState
Get the thread’s current state.
Sourcepub fn set_state(&self, new_state: ThreadState)
pub fn set_state(&self, new_state: ThreadState)
Sourcepub fn set_priority(&self, new_priority: u8)
pub fn set_priority(&self, new_priority: u8)
Set the thread’s priority.
§Arguments
new_priority
- The new priority (0-255, higher = more important)
Sourcepub fn is_runnable(&self) -> bool
pub fn is_runnable(&self) -> bool
Check if this thread is runnable (ready or running).
Sourcepub fn context_ptr(&self) -> *mut <DefaultArch as Arch>::SavedContext
pub fn context_ptr(&self) -> *mut <DefaultArch as Arch>::SavedContext
Get a pointer to the thread’s saved context.
§Returns
A pointer to the saved context, or null if not initialized.
Sourcepub fn stack_bottom(&self) -> Option<*mut u8>
pub fn stack_bottom(&self) -> Option<*mut u8>
Get the thread’s stack bottom (initial stack pointer).
Sourcepub fn check_stack_integrity(&self) -> bool
pub fn check_stack_integrity(&self) -> bool
Check if the thread’s stack canary is intact (stack overflow detection).
Sourcepub fn start_time_slice(&self)
pub fn start_time_slice(&self)
Start a new time slice for this thread.
This should be called when the thread is scheduled to run.
Sourcepub fn should_preempt(&self) -> bool
pub fn should_preempt(&self) -> bool
Update the thread’s virtual runtime and check if preemption is needed.
§Returns
true
if the thread’s time slice has expired and it should be preempted.
Sourcepub fn vruntime(&self) -> u64
pub fn vruntime(&self) -> u64
Get the thread’s current virtual runtime.
This is used by the scheduler for fair scheduling decisions.
Sourcepub fn set_cpu_affinity(&self, affinity: u64)
pub fn set_cpu_affinity(&self, affinity: u64)
Set CPU affinity mask.
Sourcepub fn cpu_affinity(&self) -> u64
pub fn cpu_affinity(&self) -> u64
Get CPU affinity mask.
Sourcepub fn set_group_id(&self, group_id: u32)
pub fn set_group_id(&self, group_id: u32)
Set thread group ID.
Sourcepub fn set_time_slice(&self, duration: Duration)
pub fn set_time_slice(&self, duration: Duration)
Set custom time slice duration.
Sourcepub fn set_critical(&self, critical: bool)
pub fn set_critical(&self, critical: bool)
Set whether this thread is critical.
Sourcepub fn is_critical(&self) -> bool
pub fn is_critical(&self) -> bool
Check if this thread is critical.
Sourcepub fn set_preemptible(&self, preemptible: bool)
pub fn set_preemptible(&self, preemptible: bool)
Set whether this thread can be preempted.
Sourcepub fn is_preemptible(&self) -> bool
pub fn is_preemptible(&self) -> bool
Check if this thread can be preempted.
Sourcepub fn reserve_tls(&self, size: usize)
pub fn reserve_tls(&self, size: usize)
Reserve thread-local storage space.
Sourcepub fn set_debug_info(&self, enabled: bool)
pub fn set_debug_info(&self, enabled: bool)
Enable or disable debug information.
Sourcepub fn debug_info_enabled(&self) -> bool
pub fn debug_info_enabled(&self) -> bool
Check if debug information is enabled.
Sourcepub fn set_realtime_priority(&self, rt_priority: u8)
pub fn set_realtime_priority(&self, rt_priority: u8)
Set real-time priority.
Sourcepub fn realtime_priority(&self) -> u8
pub fn realtime_priority(&self) -> u8
Get real-time priority.
Sourcepub fn set_nice_value(&self, nice: i8)
pub fn set_nice_value(&self, nice: i8)
Set nice value for process priority.
Sourcepub fn nice_value(&self) -> i8
pub fn nice_value(&self) -> i8
Get nice value.
Sourcepub fn set_inherit_signal_mask(&self, inherit: bool)
pub fn set_inherit_signal_mask(&self, inherit: bool)
Set whether to inherit parent’s signal mask.
Sourcepub fn inherits_signal_mask(&self) -> bool
pub fn inherits_signal_mask(&self) -> bool
Check if inheriting parent’s signal mask.
Sourcepub fn set_environment(&self, env: BTreeMap<String, String>)
pub fn set_environment(&self, env: BTreeMap<String, String>)
Set custom environment variables.
Sourcepub fn set_max_cpu_time(&self, max_time: u64)
pub fn set_max_cpu_time(&self, max_time: u64)
Set maximum CPU time limit.
Sourcepub fn max_cpu_time(&self) -> u64
pub fn max_cpu_time(&self) -> u64
Get maximum CPU time limit.
Sourcepub fn set_max_memory(&self, max_memory: usize)
pub fn set_max_memory(&self, max_memory: usize)
Set maximum memory usage limit.
Sourcepub fn max_memory(&self) -> usize
pub fn max_memory(&self) -> usize
Get maximum memory usage limit.
Sourcepub fn set_max_files(&self, max_files: u32)
pub fn set_max_files(&self, max_files: u32)
Set maximum file descriptors limit.
Sourcepub fn set_max_children(&self, max_children: u32)
pub fn set_max_children(&self, max_children: u32)
Set maximum child threads limit.
Sourcepub fn max_children(&self) -> u32
pub fn max_children(&self) -> u32
Get maximum child threads limit.
Sourcepub fn record_memory_allocation(&self, size: u64)
pub fn record_memory_allocation(&self, size: u64)
Record memory allocation for this thread.
Sourcepub fn record_memory_deallocation(&self, size: u64)
pub fn record_memory_deallocation(&self, size: u64)
Record memory deallocation for this thread.
Sourcepub fn record_cpu_time(&self, duration: Duration, user_mode: bool)
pub fn record_cpu_time(&self, duration: Duration, user_mode: bool)
Record CPU time usage for this thread.
Sourcepub fn update_stack_usage_metrics(&self, current_usage: usize)
pub fn update_stack_usage_metrics(&self, current_usage: usize)
Update stack usage for this thread.