-
Notifications
You must be signed in to change notification settings - Fork 0
/
thread.rs
200 lines (162 loc) · 5.09 KB
/
thread.rs
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
use crate::early_debug;
use crate::os::{AsHandle, OwnedHandle};
use crate::result::Result;
use crate::svc::{self, Timeout};
use alloc::boxed::Box;
use alloc::{self, alloc::Layout};
use log::debug;
unsafe extern "C" fn _ctru_rt_thread_start(argument: usize) {
early_debug!("We are in _ctru_rt_thread_start(0x{:08x})!", argument);
let packet = ThreadPacket::from_argument(argument);
early_debug!("Got a packet: entry_point={:p}", packet.entry_point);
(packet.entry_point)();
svc::exit_thread();
}
struct ThreadPacket {
entry_point: Box<dyn FnOnce()>,
}
impl ThreadPacket {
pub(crate) fn new(entry_point: impl FnOnce() + Send + 'static) -> Box<Self> {
Box::new(Self {
entry_point: Box::new(entry_point),
})
}
unsafe fn from_argument(argument: usize) -> Box<ThreadPacket> {
Box::from_raw(argument as *mut ThreadPacket)
}
fn into_argument(packet: Box<ThreadPacket>) -> usize {
Box::into_raw(packet) as usize
}
}
#[derive(Debug)]
struct ReturnValue<T>(*mut T);
impl<T> ReturnValue<T> {
fn new(ptr: *mut T) -> Self {
Self(ptr)
}
unsafe fn store(self, return_value: T) {
core::ptr::write(self.0, return_value)
}
}
unsafe impl<T> Send for ReturnValue<T> where T: Send + 'static {}
#[derive(Debug)]
struct ThreadMemory<T> {
allocated: *mut u8,
stack_top: *mut u8,
return_value: *mut T,
layout: Layout,
}
impl<T> ThreadMemory<T> {
fn allocate(stack_size: usize) -> Self {
let layout = Layout::array::<u8>(align_to(stack_size, 8))
.unwrap()
.align_to(8)
.unwrap();
let stack_size = layout.size();
let (layout, rv_offset) = layout.extend(Layout::new::<T>()).unwrap();
let allocated = unsafe { alloc::alloc::alloc(layout) };
let stack_top = unsafe { allocated.add(stack_size) };
let return_value = unsafe { allocated.add(rv_offset) as *mut T };
Self {
allocated,
stack_top,
return_value,
layout,
}
}
unsafe fn dealloc(self) {
alloc::alloc::dealloc(self.allocated, self.layout)
}
}
#[derive(Debug)]
#[must_use = "Dropping a JoinHandle leaks the associated thread and its resources"]
pub struct JoinHandle<T> {
handle: OwnedHandle,
memory: ThreadMemory<T>,
}
impl<T> JoinHandle<T>
where
T: Send + 'static,
{
pub fn join(self) -> Result<T> {
let Self { handle, memory } = self;
svc::wait_synchronization(handle.as_handle(), Timeout::forever())?;
// SAFETY: The thread using this memory exited.
// We own the only pointer to the location of the return value.
let return_value = unsafe { memory.return_value.read() };
// SAFETY: The thread using this memory exited, so we have exclusive access and are free to
// deallocate it.
unsafe { memory.dealloc() };
Ok(return_value)
}
pub fn is_running(&self) -> bool {
svc::wait_synchronization(self.handle.as_handle(), Timeout::none()).is_err()
}
}
#[derive(Debug)]
pub struct ThreadBuilder {
priority: i32,
stack_size: usize,
processor_id: i32,
}
const fn align_to(value: usize, aligment: usize) -> usize {
let mask = aligment - 1;
(value + mask) & !mask
}
impl Default for ThreadBuilder {
fn default() -> Self {
Self {
priority: 0x30,
stack_size: 0x1000,
processor_id: -2,
}
}
}
impl ThreadBuilder {
pub fn with_priority(self, priority: i32) -> Self {
Self { priority, ..self }
}
pub fn spawn<F, T>(self, f: F) -> Result<JoinHandle<T>>
where
F: FnOnce() -> T,
F: Send + 'static,
T: Send + 'static,
{
let thread_memory = ThreadMemory::allocate(self.stack_size);
let return_value = ReturnValue::new(thread_memory.return_value);
let wrapper = move || unsafe {
let rv: T = f();
return_value.store(rv)
};
let packet = ThreadPacket::new(wrapper);
let argument = ThreadPacket::into_argument(packet);
debug!(
"Launching thread: priority={}, argument={:p}, mem_start={:p}, stack_top={:p}, return_value={:p}, processor_id={}",
self.priority, argument as *const (), thread_memory.allocated, thread_memory.stack_top, thread_memory.return_value, self.processor_id
);
let handle = unsafe {
svc::create_thread(
self.priority,
_ctru_rt_thread_start,
argument,
thread_memory.stack_top,
self.processor_id,
)?
};
Ok(JoinHandle {
handle,
memory: thread_memory,
})
}
}
pub fn spawn<F, T>(f: F) -> Result<JoinHandle<T>>
where
F: FnOnce() -> T,
F: Send + 'static,
T: Send + 'static,
{
ThreadBuilder::default().spawn(f)
}