]>
Commit | Line | Data |
---|---|---|
532ac7d7 XL |
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; | |
532ac7d7 | 8 | use crate::time::Duration; |
2c00a5a8 XL |
9 | |
10 | pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024; | |
11 | ||
12 | pub struct Thread { | |
13 | id: libc::pthread_t, | |
14 | } | |
15 | ||
16 | // CloudABI has pthread_t as a pointer in which case we still want | |
17 | // a thread to be Send/Sync | |
18 | unsafe impl Send for Thread {} | |
19 | unsafe impl Sync for Thread {} | |
20 | ||
21 | impl Thread { | |
0731742a | 22 | // unsafe: see thread::Builder::spawn_unchecked for safety requirements |
48663c56 | 23 | pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> { |
ba9703b0 | 24 | let p = Box::into_raw(box p); |
2c00a5a8 XL |
25 | let mut native: libc::pthread_t = mem::zeroed(); |
26 | let mut attr: libc::pthread_attr_t = mem::zeroed(); | |
27 | assert_eq!(libc::pthread_attr_init(&mut attr), 0); | |
28 | ||
29 | let stack_size = cmp::max(stack, min_stack_size(&attr)); | |
30 | assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0); | |
31 | ||
ba9703b0 XL |
32 | let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _); |
33 | // Note: if the thread creation fails and this assert fails, then p will | |
34 | // be leaked. However, an alternative design could cause double-free | |
35 | // which is clearly worse. | |
2c00a5a8 XL |
36 | assert_eq!(libc::pthread_attr_destroy(&mut attr), 0); |
37 | ||
38 | return if ret != 0 { | |
ba9703b0 XL |
39 | // The thread failed to start and as a result p was not consumed. Therefore, it is |
40 | // safe to reconstruct the box so that it gets deallocated. | |
41 | drop(Box::from_raw(p)); | |
2c00a5a8 XL |
42 | Err(io::Error::from_raw_os_error(ret)) |
43 | } else { | |
2c00a5a8 XL |
44 | Ok(Thread { id: native }) |
45 | }; | |
46 | ||
47 | extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void { | |
48 | unsafe { | |
ba9703b0 XL |
49 | // Let's run some code. |
50 | Box::from_raw(main as *mut Box<dyn FnOnce()>)(); | |
2c00a5a8 XL |
51 | } |
52 | ptr::null_mut() | |
53 | } | |
54 | } | |
55 | ||
56 | pub fn yield_now() { | |
57 | let ret = unsafe { abi::thread_yield() }; | |
58 | debug_assert_eq!(ret, abi::errno::SUCCESS); | |
59 | } | |
60 | ||
61 | pub fn set_name(_name: &CStr) { | |
62 | // CloudABI has no way to set a thread name. | |
63 | } | |
64 | ||
65 | pub fn sleep(dur: Duration) { | |
60c5eb7d XL |
66 | let timeout = |
67 | checked_dur2intervals(&dur).expect("overflow converting duration to nanoseconds"); | |
2c00a5a8 XL |
68 | unsafe { |
69 | let subscription = abi::subscription { | |
70 | type_: abi::eventtype::CLOCK, | |
71 | union: abi::subscription_union { | |
72 | clock: abi::subscription_clock { | |
73 | clock_id: abi::clockid::MONOTONIC, | |
0731742a | 74 | timeout, |
2c00a5a8 XL |
75 | ..mem::zeroed() |
76 | }, | |
77 | }, | |
78 | ..mem::zeroed() | |
79 | }; | |
e1599b0c XL |
80 | let mut event = mem::MaybeUninit::<abi::event>::uninit(); |
81 | let mut nevents = mem::MaybeUninit::<usize>::uninit(); | |
82 | let ret = abi::poll(&subscription, event.as_mut_ptr(), 1, nevents.as_mut_ptr()); | |
2c00a5a8 | 83 | assert_eq!(ret, abi::errno::SUCCESS); |
e1599b0c | 84 | let event = event.assume_init(); |
2c00a5a8 XL |
85 | assert_eq!(event.error, abi::errno::SUCCESS); |
86 | } | |
87 | } | |
88 | ||
89 | pub fn join(self) { | |
90 | unsafe { | |
91 | let ret = libc::pthread_join(self.id, ptr::null_mut()); | |
92 | mem::forget(self); | |
60c5eb7d | 93 | assert!(ret == 0, "failed to join thread: {}", io::Error::from_raw_os_error(ret)); |
2c00a5a8 XL |
94 | } |
95 | } | |
96 | } | |
97 | ||
98 | impl Drop for Thread { | |
99 | fn drop(&mut self) { | |
100 | let ret = unsafe { libc::pthread_detach(self.id) }; | |
101 | debug_assert_eq!(ret, 0); | |
102 | } | |
103 | } | |
104 | ||
105 | #[cfg_attr(test, allow(dead_code))] | |
106 | pub mod guard { | |
107 | pub type Guard = !; | |
108 | pub unsafe fn current() -> Option<Guard> { | |
109 | None | |
110 | } | |
111 | pub unsafe fn init() -> Option<Guard> { | |
112 | None | |
113 | } | |
114 | } | |
115 | ||
116 | fn min_stack_size(_: *const libc::pthread_attr_t) -> usize { | |
117 | libc::PTHREAD_STACK_MIN | |
118 | } |