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