mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-23 12:23:22 +00:00
Take sys/vxworks/thread from sys/unix instead.
This commit is contained in:
parent
c8628f43bf
commit
5d526f6eee
@ -6,10 +6,12 @@ use crate::ptr;
|
||||
use crate::sys::{os, stack_overflow};
|
||||
use crate::time::Duration;
|
||||
|
||||
#[cfg(not(target_os = "l4re"))]
|
||||
#[cfg(not(any(target_os = "l4re", target_os = "vxworks")))]
|
||||
pub const DEFAULT_MIN_STACK_SIZE: usize = 2 * 1024 * 1024;
|
||||
#[cfg(target_os = "l4re")]
|
||||
pub const DEFAULT_MIN_STACK_SIZE: usize = 1024 * 1024;
|
||||
#[cfg(target_os = "vxworks")]
|
||||
pub const DEFAULT_MIN_STACK_SIZE: usize = 256 * 1024;
|
||||
|
||||
pub struct Thread {
|
||||
id: libc::pthread_t,
|
||||
@ -152,10 +154,11 @@ impl Thread {
|
||||
target_os = "haiku",
|
||||
target_os = "l4re",
|
||||
target_os = "emscripten",
|
||||
target_os = "redox"
|
||||
target_os = "redox",
|
||||
target_os = "vxworks"
|
||||
))]
|
||||
pub fn set_name(_name: &CStr) {
|
||||
// Newlib, Haiku, and Emscripten have no way to set a thread name.
|
||||
// Newlib, Haiku, Emscripten, and VxWorks have no way to set a thread name.
|
||||
}
|
||||
#[cfg(target_os = "fuchsia")]
|
||||
pub fn set_name(_name: &CStr) {
|
||||
|
@ -31,6 +31,7 @@ pub mod rwlock;
|
||||
#[path = "../unix/stack_overflow.rs"]
|
||||
pub mod stack_overflow;
|
||||
pub mod stdio;
|
||||
#[path = "../unix/thread.rs"]
|
||||
pub mod thread;
|
||||
pub mod thread_local_dtor;
|
||||
pub mod thread_local_key;
|
||||
|
@ -1,155 +0,0 @@
|
||||
use crate::cmp;
|
||||
use crate::ffi::CStr;
|
||||
use crate::io;
|
||||
use crate::mem;
|
||||
use crate::ptr;
|
||||
use crate::sys::{os, stack_overflow};
|
||||
use crate::time::Duration;
|
||||
|
||||
pub const DEFAULT_MIN_STACK_SIZE: usize = 0x40000; // 256K
|
||||
|
||||
pub struct Thread {
|
||||
id: libc::pthread_t,
|
||||
}
|
||||
|
||||
// Some platforms may have pthread_t as a pointer in which case we still want
|
||||
// a thread to be Send/Sync
|
||||
unsafe impl Send for Thread {}
|
||||
unsafe impl Sync for Thread {}
|
||||
|
||||
// The pthread_attr_setstacksize symbol doesn't exist in the emscripten libc,
|
||||
// so we have to not link to it to satisfy emcc's ERROR_ON_UNDEFINED_SYMBOLS.
|
||||
unsafe fn pthread_attr_setstacksize(
|
||||
attr: *mut libc::pthread_attr_t,
|
||||
stack_size: libc::size_t,
|
||||
) -> libc::c_int {
|
||||
libc::pthread_attr_setstacksize(attr, stack_size)
|
||||
}
|
||||
|
||||
impl Thread {
|
||||
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
|
||||
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
|
||||
let p = Box::into_raw(box p);
|
||||
let mut native: libc::pthread_t = mem::zeroed();
|
||||
let mut attr: libc::pthread_attr_t = mem::zeroed();
|
||||
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
|
||||
|
||||
let stack_size = cmp::max(stack, min_stack_size(&attr));
|
||||
|
||||
match pthread_attr_setstacksize(&mut attr, stack_size) {
|
||||
0 => {}
|
||||
n => {
|
||||
assert_eq!(n, libc::EINVAL);
|
||||
// EINVAL means |stack_size| is either too small or not a
|
||||
// multiple of the system page size. Because it's definitely
|
||||
// >= PTHREAD_STACK_MIN, it must be an alignment issue.
|
||||
// Round up to the nearest page and try again.
|
||||
let page_size = os::page_size();
|
||||
let stack_size =
|
||||
(stack_size + page_size - 1) & (-(page_size as isize - 1) as usize - 1);
|
||||
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
|
||||
}
|
||||
};
|
||||
|
||||
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
|
||||
// Note: if the thread creation fails and this assert fails, then p will
|
||||
// be leaked. However, an alternative design could cause double-free
|
||||
// which is clearly worse.
|
||||
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
|
||||
|
||||
return if ret != 0 {
|
||||
// The thread failed to start and as a result p was not consumed. Therefore, it is
|
||||
// safe to reconstruct the box so that it gets deallocated.
|
||||
drop(Box::from_raw(p));
|
||||
Err(io::Error::from_raw_os_error(ret))
|
||||
} else {
|
||||
Ok(Thread { id: native })
|
||||
};
|
||||
|
||||
extern "C" fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
|
||||
unsafe {
|
||||
// Next, set up our stack overflow handler which may get triggered if we run
|
||||
// out of stack.
|
||||
let _handler = stack_overflow::Handler::new();
|
||||
// Finally, let's run some code.
|
||||
Box::from_raw(main as *mut Box<dyn FnOnce()>)();
|
||||
}
|
||||
ptr::null_mut()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn yield_now() {
|
||||
let ret = unsafe { libc::sched_yield() };
|
||||
debug_assert_eq!(ret, 0);
|
||||
}
|
||||
|
||||
pub fn set_name(_name: &CStr) {
|
||||
// VxWorks does not provide a way to set the task name except at creation time
|
||||
}
|
||||
|
||||
pub fn sleep(dur: Duration) {
|
||||
let mut secs = dur.as_secs();
|
||||
let mut nsecs = dur.subsec_nanos() as _;
|
||||
|
||||
// If we're awoken with a signal then the return value will be -1 and
|
||||
// nanosleep will fill in `ts` with the remaining time.
|
||||
unsafe {
|
||||
while secs > 0 || nsecs > 0 {
|
||||
let mut ts = libc::timespec {
|
||||
tv_sec: cmp::min(libc::time_t::MAX as u64, secs) as libc::time_t,
|
||||
tv_nsec: nsecs,
|
||||
};
|
||||
secs -= ts.tv_sec as u64;
|
||||
if libc::nanosleep(&ts, &mut ts) == -1 {
|
||||
assert_eq!(os::errno(), libc::EINTR);
|
||||
secs += ts.tv_sec as u64;
|
||||
nsecs = ts.tv_nsec;
|
||||
} else {
|
||||
nsecs = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn join(self) {
|
||||
unsafe {
|
||||
let ret = libc::pthread_join(self.id, ptr::null_mut());
|
||||
mem::forget(self);
|
||||
assert!(ret == 0, "failed to join thread: {}", io::Error::from_raw_os_error(ret));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn id(&self) -> libc::pthread_t {
|
||||
self.id
|
||||
}
|
||||
|
||||
pub fn into_id(self) -> libc::pthread_t {
|
||||
let id = self.id;
|
||||
mem::forget(self);
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Thread {
|
||||
fn drop(&mut self) {
|
||||
let ret = unsafe { libc::pthread_detach(self.id) };
|
||||
debug_assert_eq!(ret, 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg_attr(test, allow(dead_code))]
|
||||
pub mod guard {
|
||||
use crate::ops::Range;
|
||||
pub type Guard = Range<usize>;
|
||||
pub unsafe fn current() -> Option<Guard> {
|
||||
None
|
||||
}
|
||||
pub unsafe fn init() -> Option<Guard> {
|
||||
None
|
||||
}
|
||||
pub unsafe fn deinit() {}
|
||||
}
|
||||
|
||||
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
|
||||
libc::PTHREAD_STACK_MIN
|
||||
}
|
Loading…
Reference in New Issue
Block a user