osal_rs/traits/
thread.rs

1/***************************************************************************
2 *
3 * osal-rs
4 * Copyright (C) 2023/2026 Antonio Salsi <passy.linux@zresa.it>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 *
18 ***************************************************************************/
19
20use core::any::Any;
21use alloc::boxed::Box;
22use alloc::sync::Arc;
23
24use crate::os::{ThreadMetadata};
25use crate::os::types::{BaseType, StackType, TickType, UBaseType};
26use crate::utils::{Result, ConstPtr, DoublePtr};
27
28pub type ThreadParam = Arc<dyn Any + Send + Sync>;
29pub type ThreadFnPtr = dyn Fn(Box<dyn Thread>, Option<ThreadParam>) -> Result<ThreadParam> + Send + Sync + 'static;
30pub type ThreadSimpleFnPtr = dyn Fn() + Send + Sync + 'static;
31
32#[derive(Debug, Copy, Clone)]
33pub enum ThreadNotification {
34    NoAction,
35    SetBits(u32),
36    Increment,
37    SetValueWithOverwrite(u32),
38    SetValueWithoutOverwrite(u32),
39}
40
41impl Into<(u32, u32)> for ThreadNotification {
42    fn into(self) -> (u32, u32) {
43        use ThreadNotification::*;
44        match self {
45            NoAction => (0, 0),
46            SetBits(bits) => (1, bits),
47            Increment => (2, 0),
48            SetValueWithOverwrite(value) => (3, value),
49            SetValueWithoutOverwrite(value) => (4, value),
50        }
51    }
52}
53
54pub trait Thread {
55    fn new(name: &str, stack_depth: StackType, priority: UBaseType) -> Self 
56    where
57        Self: Sized;
58
59    fn new_with_handle(handle: ConstPtr, name: &str, stack_depth: StackType, priority: UBaseType) -> Result<Self>  
60    where 
61        Self: Sized;
62
63    fn spawn<F>(&mut self, param: Option<ThreadParam>, callback: F) -> Result<Self>
64    where 
65        F: Fn(Box<dyn Thread>, Option<ThreadParam>) -> Result<ThreadParam>,
66        F: Send + Sync + 'static,
67        Self: Sized;
68
69    fn spawn_simple<F>(&mut self, callback: F) -> Result<Self>
70    where
71        F: Fn() + Send + Sync + 'static,
72        Self: Sized;
73
74    fn delete(&self);
75
76    fn suspend(&self);
77
78    fn resume(&self);
79
80    fn join(&self, retval: DoublePtr) -> Result<i32>;
81
82    fn get_metadata(&self) -> ThreadMetadata;
83
84    fn get_current() -> Self
85    where 
86        Self: Sized;
87
88    fn notify(&self, notification: ThreadNotification) -> Result<()>;
89
90    fn notify_from_isr(&self, notification: ThreadNotification, higher_priority_task_woken: &mut BaseType) -> Result<()>;
91
92    fn wait_notification(&self, bits_to_clear_on_entry: u32, bits_to_clear_on_exit: u32 , timeout_ticks: TickType) -> Result<u32>; //no ToTick here to maintain dynamic dispatch
93
94
95}
96
97pub trait ToPriority {
98    fn to_priority(&self) -> UBaseType;
99}