pub struct Thread { /* private fields */ }Expand description
Thread/task management and notification types. A FreeRTOS task/thread wrapper.
Provides a safe Rust interface for creating and managing FreeRTOS tasks. Threads can be created with closures or function pointers and support various synchronization primitives.
§Examples
§Creating a simple thread
use osal_rs::os::{Thread, ThreadPriority};
use core::time::Duration;
let thread = Thread::new(
"worker",
2048, // stack size in words
ThreadPriority::Normal,
|| {
loop {
println!("Working...");
Duration::from_secs(1).sleep();
}
}
).unwrap();
thread.start().unwrap();§Using thread notifications
use osal_rs::os::{Thread, ThreadNotification};
use core::time::Duration;
let thread = Thread::new("notified", 2048, 5, || {
loop {
if let Some(value) = Thread::current().wait_notification(Duration::from_secs(1)) {
println!("Received notification: {}", value);
}
}
}).unwrap();
thread.start().unwrap();
thread.notify(42).unwrap(); // Send notificationImplementations§
Source§impl Thread
impl Thread
Sourcepub fn new_with_handle(
handle: ThreadHandle,
name: &str,
stack_depth: StackType,
priority: UBaseType,
) -> Result<Self>
pub fn new_with_handle( handle: ThreadHandle, name: &str, stack_depth: StackType, priority: UBaseType, ) -> Result<Self>
Sourcepub fn new_with_to_priority(
name: &str,
stack_depth: StackType,
priority: impl ToPriority,
) -> Self
pub fn new_with_to_priority( name: &str, stack_depth: StackType, priority: impl ToPriority, ) -> Self
Creates a new thread with a priority that implements ToPriority.
This is a convenience constructor that allows using various priority types.
§Parameters
name- Thread name for debuggingstack_depth- Stack size in words (not bytes)priority- Thread priority (any type implementingToPriority)
§Examples
use osal_rs::os::Thread;
let thread = Thread::new_with_to_priority("worker", 2048, 5);Sourcepub fn new_with_handle_and_to_priority(
handle: ThreadHandle,
name: &str,
stack_depth: StackType,
priority: impl ToPriority,
) -> Result<Self>
pub fn new_with_handle_and_to_priority( handle: ThreadHandle, name: &str, stack_depth: StackType, priority: impl ToPriority, ) -> Result<Self>
Creates a thread from an existing FreeRTOS task handle.
§Parameters
handle- Valid FreeRTOS task handlename- Thread namestack_depth- Stack sizepriority- Thread priority
§Returns
Err(Error::NullPtr)if handle is null
§Examples
use osal_rs::os::Thread;
// Get current task handle from FreeRTOS
let handle = get_task_handle();
let thread = Thread::new_with_handle_and_to_priority(handle, "existing", 2048, 5).unwrap();Sourcepub fn get_metadata_from_handle(handle: ThreadHandle) -> ThreadMetadata
pub fn get_metadata_from_handle(handle: ThreadHandle) -> ThreadMetadata
Retrieves metadata for a thread from its handle.
§Parameters
handle- FreeRTOS task handle
§Returns
Thread metadata including state, priority, stack usage, etc.
§Examples
use osal_rs::os::Thread;
let handle = get_some_task_handle();
let metadata = Thread::get_metadata_from_handle(handle);
println!("Thread '{}' state: {:?}", metadata.name, metadata.state);Sourcepub fn get_metadata(thread: &Thread) -> ThreadMetadata
pub fn get_metadata(thread: &Thread) -> ThreadMetadata
Retrieves metadata for a thread object.
§Parameters
thread- Thread reference
§Returns
Thread metadata or default if handle is null
§Examples
use osal_rs::os::Thread;
let thread = Thread::new("worker", 2048, 5);
let metadata = Thread::get_metadata(&thread);
println!("Stack high water mark: {}", metadata.stack_high_water_mark);Sourcepub fn wait_notification_with_to_tick(
&self,
bits_to_clear_on_entry: u32,
bits_to_clear_on_exit: u32,
timeout_ticks: impl ToTick,
) -> Result<u32>
pub fn wait_notification_with_to_tick( &self, bits_to_clear_on_entry: u32, bits_to_clear_on_exit: u32, timeout_ticks: impl ToTick, ) -> Result<u32>
Waits for a thread notification with a timeout that implements ToTick.
Convenience method that accepts Duration or other tick-convertible types.
§Parameters
bits_to_clear_on_entry- Bits to clear before waitingbits_to_clear_on_exit- Bits to clear after receiving notificationtimeout_ticks- Maximum time to wait (convertible to ticks)
§Returns
Ok(u32)- Notification value receivedErr(Error::NullPtr)- Thread handle is nullErr(Error::Timeout)- No notification received within timeout
§Examples
use osal_rs::os::{Thread, ThreadFn};
use core::time::Duration;
let thread = Thread::current();
match thread.wait_notification_with_to_tick(0, 0xFF, Duration::from_secs(1)) {
Ok(value) => println!("Received: {}", value),
Err(_) => println!("Timeout"),
}Trait Implementations§
Source§impl Debug for Thread
Formats the thread for debugging purposes.
impl Debug for Thread
Formats the thread for debugging purposes.
Includes handle, name, stack depth, priority, and callback status.
Source§impl Deref for Thread
Allows dereferencing to the underlying FreeRTOS thread handle.
impl Deref for Thread
Allows dereferencing to the underlying FreeRTOS thread handle.
This enables direct access to the handle when needed for low-level operations.
Source§impl Display for Thread
Formats the thread for display purposes.
impl Display for Thread
Formats the thread for display purposes.
Shows a concise representation with handle, name, priority, and stack depth.
Source§impl Thread for Thread
impl Thread for Thread
Source§fn spawn<F>(&mut self, param: Option<ThreadParam>, callback: F) -> Result<Self>
fn spawn<F>(&mut self, param: Option<ThreadParam>, callback: F) -> Result<Self>
Spawns a new thread with a callback.
§Important
The callback must be 'static, which means it cannot borrow local variables.
Use move in the closure to transfer ownership of any captured values:
let data = Arc::new(Mutex::new(0));
let thread = Thread::new("my_thread", 4096, 3, move |_thread, _param| {
// Use 'move' to capture 'data' by value
let mut guard = data.lock().unwrap();
*guard += 1;
Ok(Arc::new(()))
});
``Source§fn spawn_simple<F>(&mut self, callback: F) -> Result<Self>
fn spawn_simple<F>(&mut self, callback: F) -> Result<Self>
Spawns a new thread with a simple closure, similar to std::thread::spawn.
This is the recommended way to create threads for most use cases.
§Example
let counter = Arc::new(Mutex::new(0));
let counter_clone = Arc::clone(&counter);
let handle = Thread::spawn_simple("worker", 4096, 3, move || {
let mut num = counter_clone.lock().unwrap();
*num += 1;
}).unwrap();
handle.join(core::ptr::null_mut());Source§fn get_metadata(&self) -> ThreadMetadata
fn get_metadata(&self) -> ThreadMetadata
Source§fn get_current() -> Self
fn get_current() -> Self
Source§fn notify(&self, notification: ThreadNotification) -> Result<()>
fn notify(&self, notification: ThreadNotification) -> Result<()>
Sends a notification to this thread.
§Parameters
notification- Type of notification action to perform
§Returns
Ok(())- Notification sent successfullyErr(Error::NullPtr)- Thread handle is nullErr(Error::QueueFull)- Notification failed
§Examples
use osal_rs::os::{Thread, ThreadFn, ThreadNotification};
let thread = get_worker_thread();
thread.notify(ThreadNotification::SetValueWithOverwrite(42)).unwrap();Source§fn notify_from_isr(
&self,
notification: ThreadNotification,
higher_priority_task_woken: &mut BaseType,
) -> Result<()>
fn notify_from_isr( &self, notification: ThreadNotification, higher_priority_task_woken: &mut BaseType, ) -> Result<()>
Sends a notification to this thread from an ISR.
§Parameters
notification- Type of notification actionhigher_priority_task_woken- Set to pdTRUE if a higher priority task was woken
§Returns
Ok(())- Notification sent successfullyErr(Error::NullPtr)- Thread handle is nullErr(Error::QueueFull)- Notification failed
§Examples
// In ISR context:
let mut woken = pdFALSE;
thread.notify_from_isr(ThreadNotification::Increment, &mut woken).ok();Source§fn wait_notification(
&self,
bits_to_clear_on_entry: u32,
bits_to_clear_on_exit: u32,
timeout_ticks: TickType,
) -> Result<u32>
fn wait_notification( &self, bits_to_clear_on_entry: u32, bits_to_clear_on_exit: u32, timeout_ticks: TickType, ) -> Result<u32>
Waits for a thread notification.
§Parameters
bits_to_clear_on_entry- Bits to clear in notification value before waitingbits_to_clear_on_exit- Bits to clear after receiving notificationtimeout_ticks- Maximum ticks to wait
§Returns
Ok(u32)- Notification value receivedErr(Error::NullPtr)- Thread handle is nullErr(Error::Timeout)- No notification within timeout
§Examples
use osal_rs::os::{Thread, ThreadFn};
let thread = Thread::current();
match thread.wait_notification(0, 0xFFFFFFFF, 1000) {
Ok(value) => println!("Received notification: {}", value),
Err(_) => println!("Timeout waiting for notification"),
}