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.
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.
11 use alloc::boxed::FnBox;
15 use sys_common::thread::start_thread;
16 use sys::{cvt, syscall};
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 {}
29 pub unsafe fn new<'a>(_stack: usize, p: Box<FnBox() + 'a>) -> io::Result<Thread> {
32 let id = cvt(syscall::clone(syscall::CLONE_VM | syscall::CLONE_FS | syscall::CLONE_FILES))?;
34 start_thread(&*p as *const _ as *mut _);
35 let _ = syscall::exit(0);
36 panic!("thread failed to exit");
44 let ret = syscall::sched_yield().expect("failed to sched_yield");
45 debug_assert_eq!(ret, 0);
48 pub fn set_name(_name: &CStr) {
52 pub fn sleep(dur: Duration) {
53 let mut secs = dur.as_secs();
54 let mut nsecs = dur.subsec_nanos() as i32;
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 {
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;
76 syscall::waitpid(self.id, &mut status, 0).unwrap();
79 pub fn id(&self) -> usize { self.id }
81 pub fn into_id(self) -> usize {
89 pub unsafe fn current() -> Option<usize> { None }
90 pub unsafe fn init() -> Option<usize> { None }