about summary refs log tree commit diff
path: root/src/libstd/sys/hermit/thread.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/libstd/sys/hermit/thread.rs')
-rw-r--r--src/libstd/sys/hermit/thread.rs58
1 files changed, 19 insertions, 39 deletions
diff --git a/src/libstd/sys/hermit/thread.rs b/src/libstd/sys/hermit/thread.rs
index c3c29c93826..e11afed6687 100644
--- a/src/libstd/sys/hermit/thread.rs
+++ b/src/libstd/sys/hermit/thread.rs
@@ -1,39 +1,14 @@
 #![allow(dead_code)]
 
 use crate::ffi::CStr;
-use crate::fmt;
 use crate::io;
 use crate::mem;
 use crate::sys::hermit::abi;
+use crate::sys::hermit::fast_thread_local::run_dtors;
 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,
 }
@@ -41,34 +16,39 @@ pub struct Thread {
 unsafe impl Send for Thread {}
 unsafe impl Sync for Thread {}
 
-pub const DEFAULT_MIN_STACK_SIZE: usize = 262144;
+pub const DEFAULT_MIN_STACK_SIZE: usize = 1 << 20;
 
 impl Thread {
     pub unsafe fn new_with_coreid(
-        _stack: usize,
+        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,
+        let p = Box::into_raw(box p);
+        let tid = abi::spawn2(
             thread_start,
-            &*p as *const _ as *const u8 as usize,
-            Priority::into(NORMAL_PRIO),
+            p as usize,
+            abi::Priority::into(abi::NORMAL_PRIO),
+            stack,
             core_id,
         );
 
-        return if ret == 0 {
-            mem::forget(p); // ownership passed to pthread_create
-            Ok(Thread { tid: tid })
-        } else {
+        return if tid == 0 {
+            // The thread failed to start and as a result p was not consumed. Therefore, it is
+            // safe to reconstruct the box so that it gets deallocated.
+            drop(Box::from_raw(p));
             Err(io::Error::new(io::ErrorKind::Other, "Unable to create thread!"))
+        } else {
+            Ok(Thread { tid: tid })
         };
 
         extern "C" fn thread_start(main: usize) {
             unsafe {
-                start_thread(main as *mut u8);
+                // Finally, let's run some code.
+                Box::from_raw(main as *mut Box<dyn FnOnce()>)();
+
+                // run all destructors
+                run_dtors();
             }
         }
     }