]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/cloudabi/thread.rs
Rollup merge of #61120 - spastorino:eval-place-iterate, r=oli-obk
[rust.git] / src / libstd / sys / cloudabi / thread.rs
1 use crate::cmp;
2 use crate::ffi::CStr;
3 use crate::io;
4 use crate::mem;
5 use crate::ptr;
6 use crate::sys::cloudabi::abi;
7 use crate::sys::time::checked_dur2intervals;
8 use crate::sys_common::thread::*;
9 use crate::time::Duration;
10
11 pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
12
13 pub struct Thread {
14     id: libc::pthread_t,
15 }
16
17 // CloudABI has pthread_t as a pointer in which case we still want
18 // a thread to be Send/Sync
19 unsafe impl Send for Thread {}
20 unsafe impl Sync for Thread {}
21
22 impl Thread {
23     // unsafe: see thread::Builder::spawn_unchecked for safety requirements
24     pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
25         let p = box p;
26         let mut native: libc::pthread_t = mem::zeroed();
27         let mut attr: libc::pthread_attr_t = mem::zeroed();
28         assert_eq!(libc::pthread_attr_init(&mut attr), 0);
29
30         let stack_size = cmp::max(stack, min_stack_size(&attr));
31         assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
32
33         let ret = libc::pthread_create(&mut native, &attr, thread_start, &*p as *const _ as *mut _);
34         assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
35
36         return if ret != 0 {
37             Err(io::Error::from_raw_os_error(ret))
38         } else {
39             mem::forget(p); // ownership passed to pthread_create
40             Ok(Thread { id: native })
41         };
42
43         extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
44             unsafe {
45                 start_thread(main as *mut u8);
46             }
47             ptr::null_mut()
48         }
49     }
50
51     pub fn yield_now() {
52         let ret = unsafe { abi::thread_yield() };
53         debug_assert_eq!(ret, abi::errno::SUCCESS);
54     }
55
56     pub fn set_name(_name: &CStr) {
57         // CloudABI has no way to set a thread name.
58     }
59
60     pub fn sleep(dur: Duration) {
61         let timeout = checked_dur2intervals(&dur)
62             .expect("overflow converting duration to nanoseconds");
63         unsafe {
64             let subscription = abi::subscription {
65                 type_: abi::eventtype::CLOCK,
66                 union: abi::subscription_union {
67                     clock: abi::subscription_clock {
68                         clock_id: abi::clockid::MONOTONIC,
69                         timeout,
70                         ..mem::zeroed()
71                     },
72                 },
73                 ..mem::zeroed()
74             };
75             let mut event: abi::event = mem::uninitialized();
76             let mut nevents: usize = mem::uninitialized();
77             let ret = abi::poll(&subscription, &mut event, 1, &mut nevents);
78             assert_eq!(ret, abi::errno::SUCCESS);
79             assert_eq!(event.error, abi::errno::SUCCESS);
80         }
81     }
82
83     pub fn join(self) {
84         unsafe {
85             let ret = libc::pthread_join(self.id, ptr::null_mut());
86             mem::forget(self);
87             assert!(
88                 ret == 0,
89                 "failed to join thread: {}",
90                 io::Error::from_raw_os_error(ret)
91             );
92         }
93     }
94 }
95
96 impl Drop for Thread {
97     fn drop(&mut self) {
98         let ret = unsafe { libc::pthread_detach(self.id) };
99         debug_assert_eq!(ret, 0);
100     }
101 }
102
103 #[cfg_attr(test, allow(dead_code))]
104 pub mod guard {
105     pub type Guard = !;
106     pub unsafe fn current() -> Option<Guard> {
107         None
108     }
109     pub unsafe fn init() -> Option<Guard> {
110         None
111     }
112 }
113
114 fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
115     libc::PTHREAD_STACK_MIN
116 }