diff options
| author | bors <bors@rust-lang.org> | 2019-10-26 19:35:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-10-26 19:35:59 +0000 |
| commit | fae75cd216c481de048e4951697c8f8525669c65 (patch) | |
| tree | 157e46f84638aa1fa3b0339ced417876e8ca209a /src/libstd/sys/hermit/thread.rs | |
| parent | 46e6c533d08a2c6d22083a2756a0b569e001c3c4 (diff) | |
| parent | 805a330ab4c7f33421acacbee0545f6991b2fc70 (diff) | |
Auto merge of #65167 - hermitcore:rusty-hermit, r=alexcrichton
Redesign the interface to the unikernel HermitCore We are developing the unikernel HermitCore, where the kernel is written in Rust and is already part of the Rust Standard Library. The interface between the standard library and the kernel based on a small C library. With this pull request, we remove completely the dependency to C and use lld as linker. Currently, the kernel will be linked to the application as static library, which is published at https://github.com/hermitcore/libhermit-rs. We don’t longer support the C interface to the kernel. Consequently, we remove this part from the Rust Standard Library.
Diffstat (limited to 'src/libstd/sys/hermit/thread.rs')
| -rw-r--r-- | src/libstd/sys/hermit/thread.rs | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/libstd/sys/hermit/thread.rs b/src/libstd/sys/hermit/thread.rs new file mode 100644 index 00000000000..99a9c830c9e --- /dev/null +++ b/src/libstd/sys/hermit/thread.rs @@ -0,0 +1,116 @@ +#![allow(dead_code)] + +use crate::ffi::CStr; +use crate::io; +use crate::sys::hermit::abi; +use crate::time::Duration; +use crate::mem; +use crate::fmt; +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 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 } +} |
