Skip to content

Commit af428db

Browse files
committed
Implement sys/thread for UEFI
Since UEFI has no concept of threads, most of this module can be ignored. However, implementing parts that make sense. - Implement sleep - Implement available_parallelism Signed-off-by: Ayush Singh <[email protected]>
1 parent 899c895 commit af428db

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

library/std/src/sys/pal/uefi/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub mod pipe;
3131
#[path = "../unsupported/process.rs"]
3232
pub mod process;
3333
pub mod stdio;
34-
#[path = "../unsupported/thread.rs"]
3534
pub mod thread;
3635
#[path = "../unsupported/thread_local_key.rs"]
3736
pub mod thread_local_key;
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
use super::unsupported;
2+
use crate::ffi::CStr;
3+
use crate::io;
4+
use crate::num::NonZeroUsize;
5+
use crate::ptr::NonNull;
6+
use crate::time::Duration;
7+
8+
pub struct Thread(!);
9+
10+
pub const DEFAULT_MIN_STACK_SIZE: usize = 4096;
11+
12+
impl Thread {
13+
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
14+
pub unsafe fn new(_stack: usize, _p: Box<dyn FnOnce()>) -> io::Result<Thread> {
15+
unsupported()
16+
}
17+
18+
pub fn yield_now() {
19+
// do nothing
20+
}
21+
22+
pub fn set_name(_name: &CStr) {
23+
// nope
24+
}
25+
26+
pub fn sleep(dur: Duration) {
27+
let boot_services: NonNull<r_efi::efi::BootServices> =
28+
crate::os::uefi::env::boot_services().expect("can't sleep").cast();
29+
let mut dur_ms = dur.as_micros();
30+
// ceil up to the nearest microsecond
31+
if dur.subsec_nanos() % 1000 > 0 {
32+
dur_ms += 1;
33+
}
34+
35+
while dur_ms > 0 {
36+
let ms = crate::cmp::min(dur_ms, usize::MAX as u128);
37+
let _ = unsafe { ((*boot_services.as_ptr()).stall)(ms as usize) };
38+
dur_ms -= ms;
39+
}
40+
}
41+
42+
pub fn join(self) {
43+
self.0
44+
}
45+
}
46+
47+
pub fn available_parallelism() -> io::Result<NonZeroUsize> {
48+
// UEFI is single threaded
49+
Ok(NonZeroUsize::new(1).unwrap())
50+
}
51+
52+
pub mod guard {
53+
pub type Guard = !;
54+
pub unsafe fn current() -> Option<Guard> {
55+
None
56+
}
57+
pub unsafe fn init() -> Option<Guard> {
58+
None
59+
}
60+
}

0 commit comments

Comments
 (0)