]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/redox/thread.rs
Change sys::Thread::new to take the thread entry as Box<dyn FnBox() + 'static>̣
[rust.git] / src / libstd / sys / redox / thread.rs
1 // Copyright 2016 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 ffi::CStr;
13 use io;
14 use mem;
15 use sys_common::thread::start_thread;
16 use sys::{cvt, syscall};
17 use time::Duration;
18
19 pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
20
21 pub struct Thread {
22     id: usize,
23 }
24
25 // Some platforms may have pthread_t as a pointer in which case we still want
26 // a thread to be Send/Sync
27 unsafe impl Send for Thread {}
28 unsafe impl Sync for Thread {}
29
30 impl Thread {
31     // unsafe: see thread::Builder::spawn_unchecked for safety requirements
32     pub unsafe fn new(_stack: usize, p: Box<dyn FnBox()>) -> io::Result<Thread> {
33         let p = box p;
34
35         let id = cvt(syscall::clone(syscall::CLONE_VM | syscall::CLONE_FS | syscall::CLONE_FILES))?;
36         if id == 0 {
37             start_thread(&*p as *const _ as *mut _);
38             let _ = syscall::exit(0);
39             panic!("thread failed to exit");
40         } else {
41             mem::forget(p);
42             Ok(Thread { id })
43         }
44     }
45
46     pub fn yield_now() {
47         let ret = syscall::sched_yield().expect("failed to sched_yield");
48         debug_assert_eq!(ret, 0);
49     }
50
51     pub fn set_name(_name: &CStr) {
52
53     }
54
55     pub fn sleep(dur: Duration) {
56         let mut secs = dur.as_secs();
57         let mut nsecs = dur.subsec_nanos() as i32;
58
59         // If we're awoken with a signal then the return value will be -1 and
60         // nanosleep will fill in `ts` with the remaining time.
61         while secs > 0 || nsecs > 0 {
62             let req = syscall::TimeSpec {
63                 tv_sec: secs as i64,
64                 tv_nsec: nsecs,
65             };
66             secs -= req.tv_sec as u64;
67             let mut rem = syscall::TimeSpec::default();
68             if syscall::nanosleep(&req, &mut rem).is_err() {
69                 secs += rem.tv_sec as u64;
70                 nsecs = rem.tv_nsec;
71             } else {
72                 nsecs = 0;
73             }
74         }
75     }
76
77     pub fn join(self) {
78         let mut status = 0;
79         syscall::waitpid(self.id, &mut status, 0).unwrap();
80     }
81
82     pub fn id(&self) -> usize { self.id }
83
84     pub fn into_id(self) -> usize {
85         let id = self.id;
86         mem::forget(self);
87         id
88     }
89 }
90
91 pub mod guard {
92     pub type Guard = !;
93     pub unsafe fn current() -> Option<Guard> { None }
94     pub unsafe fn init() -> Option<Guard> { None }
95     pub unsafe fn deinit() {}
96 }