1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
/*
* Copyright (c) 2006-2025, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2025-10-10 foxglove RT-Thread Thread implementation
*/
use crate::alloc::boxed::Box;
use crate::api::*;
use crate::{RTResult, RTTError};
use alloc::string::String;
use core::mem;
use core::ffi::c_void;
#[derive(Debug)]
pub struct Thread(APIRawThread);
impl Thread {
pub fn delay(tick: usize) {
let _ = thread_delay(tick);
}
pub fn ms_delay(ms: usize) {
let _ = thread_m_delay(ms as i32);
}
pub fn new() -> ThreadBuilder {
ThreadBuilder {
th_name: "Unnamed".into(),
th_stack_size: 4096,
th_priority: 10,
th_ticks: 10,
}
}
pub fn r#yield() {
thread_yield();
}
pub fn delete_thread(th: Self) {
let _ = thread_delete(th.0);
}
pub fn delete(&self) {
let _ = thread_delete(self.0);
}
unsafe fn spawn_inner(
name: String,
stack_size: u32,
priority: u8,
ticks: u32,
func: Box<dyn FnOnce()>,
) -> Result<Self, RTTError> {
let func = Box::new(func);
let param = &*func as *const _ as *mut _;
extern "C" fn thread_func(param: *mut c_void) {
unsafe {
let run = Box::from_raw(param as *mut Box<dyn FnOnce()>);
run();
}
}
let th_handle = thread_create(
name.as_ref(),
thread_func,
param,
stack_size,
priority,
ticks,
)
.ok_or(RTTError::OutOfMemory)?;
let ret = match Self::_startup(th_handle) {
Ok(_) => {
mem::forget(func);
Ok(Thread(th_handle))
}
Err(e) => Err(e),
};
return ret;
}
fn _startup(th: APIRawThread) -> Result<(), RTTError> {
let ret = thread_startup(th);
return if is_eok(ret) {
Ok(())
} else {
Err(RTTError::ThreadStartupErr)
};
}
pub fn spawn<F>(
name: String,
stack_size: u32,
priority: u8,
ticks: u32,
func: F,
) -> RTResult<Thread>
where
F: FnOnce() -> () + Send + 'static,
{
unsafe { Self::spawn_inner(name, stack_size, priority, ticks, Box::new(func)) }
}
}
unsafe impl Send for Thread {}
pub struct ThreadBuilder {
th_name: String,
th_stack_size: u32,
th_priority: u8,
th_ticks: u32,
}
impl ThreadBuilder {
pub fn name(&mut self, name: &str) -> &mut Self {
self.th_name = name.into();
self
}
pub fn stack_size(&mut self, stack_size: u32) -> &mut Self {
self.th_stack_size = stack_size;
self
}
pub fn priority(&mut self, priority: u8) -> &mut Self {
self.th_priority = priority;
self
}
pub fn ticks(&mut self, ticks: u32) -> &mut Self {
self.th_ticks = ticks;
self
}
pub fn start<F>(&self, func: F) -> RTResult<Thread>
where
F: FnOnce() -> (),
F: Send + 'static,
{
Thread::spawn(
self.th_name.clone(),
self.th_stack_size,
self.th_priority,
self.th_ticks,
func,
)
}
}
|