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
|
#![allow(dead_code)]
use crate::ffi::CStr;
use crate::fmt;
use crate::io;
use crate::mem;
use crate::sys::hermit::abi;
use crate::time::Duration;
use core::u32;
use crate::sys_common::thread::*;
pub type Tid = abi::Tid;
/// Priority of a task
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Clone, Copy)]
pub struct Priority(u8);
impl Priority {
pub const fn into(self) -> u8 {
self.0
}
pub const fn from(x: u8) -> Self {
Priority(x)
}
}
impl fmt::Display for Priority {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
pub const NORMAL_PRIO: Priority = Priority::from(2);
pub struct Thread {
tid: Tid,
}
unsafe impl Send for Thread {}
unsafe impl Sync for Thread {}
pub const DEFAULT_MIN_STACK_SIZE: usize = 262144;
impl Thread {
pub unsafe fn new_with_coreid(
_stack: usize,
p: Box<dyn FnOnce()>,
core_id: isize,
) -> io::Result<Thread> {
let p = box p;
let mut tid: Tid = u32::MAX;
let ret = abi::spawn(
&mut tid as *mut Tid,
thread_start,
&*p as *const _ as *const u8 as usize,
Priority::into(NORMAL_PRIO),
core_id,
);
return if ret == 0 {
mem::forget(p); // ownership passed to pthread_create
Ok(Thread { tid: tid })
} else {
Err(io::Error::new(io::ErrorKind::Other, "Unable to create thread!"))
};
extern "C" fn thread_start(main: usize) {
unsafe {
start_thread(main as *mut u8);
}
}
}
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
Thread::new_with_coreid(stack, p, -1 /* = no specific core */)
}
#[inline]
pub fn yield_now() {
unsafe {
abi::yield_now();
}
}
#[inline]
pub fn set_name(_name: &CStr) {
// nope
}
#[inline]
pub fn sleep(dur: Duration) {
unsafe {
abi::usleep(dur.as_micros() as u64);
}
}
pub fn join(self) {
unsafe {
let _ = abi::join(self.tid);
}
}
#[inline]
pub fn id(&self) -> Tid {
self.tid
}
#[inline]
pub fn into_id(self) -> Tid {
let id = self.tid;
mem::forget(self);
id
}
}
pub mod guard {
pub type Guard = !;
pub unsafe fn current() -> Option<Guard> {
None
}
pub unsafe fn init() -> Option<Guard> {
None
}
}
|