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.
15 use sys_common::thread::start_thread;
16 use sys::{cvt, syscall};
19 pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
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 {}
31 pub unsafe fn new<'a>(_stack: usize, p: Box<dyn FnBox() + 'a>) -> io::Result<Thread> {
34 let id = cvt(syscall::clone(syscall::CLONE_VM | syscall::CLONE_FS | syscall::CLONE_FILES))?;
36 start_thread(&*p as *const _ as *mut _);
37 let _ = syscall::exit(0);
38 panic!("thread failed to exit");
46 let ret = syscall::sched_yield().expect("failed to sched_yield");
47 debug_assert_eq!(ret, 0);
50 pub fn set_name(_name: &CStr) {
54 pub fn sleep(dur: Duration) {
55 let mut secs = dur.as_secs();
56 let mut nsecs = dur.subsec_nanos() as i32;
58 // If we're awoken with a signal then the return value will be -1 and
59 // nanosleep will fill in `ts` with the remaining time.
60 while secs > 0 || nsecs > 0 {
61 let req = syscall::TimeSpec {
65 secs -= req.tv_sec as u64;
66 let mut rem = syscall::TimeSpec::default();
67 if syscall::nanosleep(&req, &mut rem).is_err() {
68 secs += rem.tv_sec as u64;
78 syscall::waitpid(self.id, &mut status, 0).unwrap();
81 pub fn id(&self) -> usize { self.id }
83 pub fn into_id(self) -> usize {
92 pub unsafe fn current() -> Option<Guard> { None }
93 pub unsafe fn init() -> Option<Guard> { None }
94 pub unsafe fn deinit() {}