]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/cloudabi/thread.rs
Change sys::Thread::new to take the thread entry as Box<dyn FnBox() + 'static>̣
[rust.git] / src / libstd / sys / cloudabi / thread.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use boxed::FnBox;
12 use cmp;
13 use ffi::CStr;
14 use io;
15 use libc;
16 use mem;
17 use ptr;
18 use sys::cloudabi::abi;
19 use sys::time::dur2intervals;
20 use sys_common::thread::*;
21 use time::Duration;
22
23 pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
24
25 pub struct Thread {
26     id: libc::pthread_t,
27 }
28
29 // CloudABI has pthread_t as a pointer in which case we still want
30 // a thread to be Send/Sync
31 unsafe impl Send for Thread {}
32 unsafe impl Sync for Thread {}
33
34 impl Thread {
35     // unsafe: see thread::Builder::spawn_unchecked for safety requirements
36     pub unsafe fn new(stack: usize, p: Box<dyn FnBox()>) -> io::Result<Thread> {
37         let p = box p;
38         let mut native: libc::pthread_t = mem::zeroed();
39         let mut attr: libc::pthread_attr_t = mem::zeroed();
40         assert_eq!(libc::pthread_attr_init(&mut attr), 0);
41
42         let stack_size = cmp::max(stack, min_stack_size(&attr));
43         assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
44
45         let ret = libc::pthread_create(&mut native, &attr, thread_start, &*p as *const _ as *mut _);
46         assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
47
48         return if ret != 0 {
49             Err(io::Error::from_raw_os_error(ret))
50         } else {
51             mem::forget(p); // ownership passed to pthread_create
52             Ok(Thread { id: native })
53         };
54
55         extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
56             unsafe {
57                 start_thread(main as *mut u8);
58             }
59             ptr::null_mut()
60         }
61     }
62
63     pub fn yield_now() {
64         let ret = unsafe { abi::thread_yield() };
65         debug_assert_eq!(ret, abi::errno::SUCCESS);
66     }
67
68     pub fn set_name(_name: &CStr) {
69         // CloudABI has no way to set a thread name.
70     }
71
72     pub fn sleep(dur: Duration) {
73         unsafe {
74             let subscription = abi::subscription {
75                 type_: abi::eventtype::CLOCK,
76                 union: abi::subscription_union {
77                     clock: abi::subscription_clock {
78                         clock_id: abi::clockid::MONOTONIC,
79                         timeout: dur2intervals(&dur),
80                         ..mem::zeroed()
81                     },
82                 },
83                 ..mem::zeroed()
84             };
85             let mut event: abi::event = mem::uninitialized();
86             let mut nevents: usize = mem::uninitialized();
87             let ret = abi::poll(&subscription, &mut event, 1, &mut nevents);
88             assert_eq!(ret, abi::errno::SUCCESS);
89             assert_eq!(event.error, abi::errno::SUCCESS);
90         }
91     }
92
93     pub fn join(self) {
94         unsafe {
95             let ret = libc::pthread_join(self.id, ptr::null_mut());
96             mem::forget(self);
97             assert!(
98                 ret == 0,
99                 "failed to join thread: {}",
100                 io::Error::from_raw_os_error(ret)
101             );
102         }
103     }
104 }
105
106 impl Drop for Thread {
107     fn drop(&mut self) {
108         let ret = unsafe { libc::pthread_detach(self.id) };
109         debug_assert_eq!(ret, 0);
110     }
111 }
112
113 #[cfg_attr(test, allow(dead_code))]
114 pub mod guard {
115     pub type Guard = !;
116     pub unsafe fn current() -> Option<Guard> {
117         None
118     }
119     pub unsafe fn init() -> Option<Guard> {
120         None
121     }
122     pub unsafe fn deinit() {}
123 }
124
125 fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
126     libc::PTHREAD_STACK_MIN
127 }