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 // unsafe: see thread::Builder::spawn_unchecked for safety requirements
32 pub unsafe fn new(_stack: usize, p: Box<dyn FnBox()>) -> io::Result<Thread> {
35 let id = cvt(syscall::clone(syscall::CLONE_VM | syscall::CLONE_FS | syscall::CLONE_FILES))?;
37 start_thread(&*p as *const _ as *mut _);
38 let _ = syscall::exit(0);
39 panic!("thread failed to exit");
47 let ret = syscall::sched_yield().expect("failed to sched_yield");
48 debug_assert_eq!(ret, 0);
51 pub fn set_name(_name: &CStr) {
55 pub fn sleep(dur: Duration) {
56 let mut secs = dur.as_secs();
57 let mut nsecs = dur.subsec_nanos() as i32;
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 {
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;
79 syscall::waitpid(self.id, &mut status, 0).unwrap();
82 pub fn id(&self) -> usize { self.id }
84 pub fn into_id(self) -> usize {
93 pub unsafe fn current() -> Option<Guard> { None }
94 pub unsafe fn init() -> Option<Guard> { None }
95 pub unsafe fn deinit() {}