2015-01-29 07:19:28 +00:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
2014-11-24 03:21:17 +00:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-04-15 05:13:57 +00:00
|
|
|
use prelude::v1::*;
|
2014-11-24 03:21:17 +00:00
|
|
|
|
2015-04-15 05:13:57 +00:00
|
|
|
use alloc::boxed::FnBox;
|
2014-11-24 03:21:17 +00:00
|
|
|
use cmp;
|
2016-01-28 11:02:31 +00:00
|
|
|
#[cfg(not(any(target_env = "newlib", target_os = "solaris")))]
|
2016-03-25 04:46:45 +00:00
|
|
|
use ffi::CStr;
|
2015-03-13 03:21:17 +00:00
|
|
|
use io;
|
|
|
|
use libc;
|
2014-11-24 03:21:17 +00:00
|
|
|
use mem;
|
|
|
|
use ptr;
|
2015-03-13 03:21:17 +00:00
|
|
|
use sys::os;
|
|
|
|
use time::Duration;
|
2014-11-24 03:21:17 +00:00
|
|
|
|
|
|
|
use sys_common::thread::*;
|
|
|
|
|
2015-04-15 05:13:57 +00:00
|
|
|
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 {}
|
|
|
|
|
|
|
|
impl Thread {
|
|
|
|
pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>)
|
|
|
|
-> io::Result<Thread> {
|
|
|
|
let p = box p;
|
|
|
|
let mut native: libc::pthread_t = mem::zeroed();
|
|
|
|
let mut attr: libc::pthread_attr_t = mem::zeroed();
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
|
2015-04-15 05:13:57 +00:00
|
|
|
|
2015-07-27 20:41:35 +00:00
|
|
|
let stack_size = cmp::max(stack, min_stack_size(&attr));
|
2015-11-03 00:23:22 +00:00
|
|
|
match libc::pthread_attr_setstacksize(&mut attr,
|
|
|
|
stack_size as libc::size_t) {
|
2015-04-15 05:13:57 +00:00
|
|
|
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);
|
|
|
|
let stack_size = stack_size as libc::size_t;
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_setstacksize(&mut attr,
|
|
|
|
stack_size), 0);
|
2015-04-15 05:13:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-11-03 00:23:22 +00:00
|
|
|
let ret = libc::pthread_create(&mut native, &attr, thread_start,
|
|
|
|
&*p as *const _ as *mut _);
|
|
|
|
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
|
2015-04-15 05:13:57 +00:00
|
|
|
|
|
|
|
return if ret != 0 {
|
|
|
|
Err(io::Error::from_raw_os_error(ret))
|
|
|
|
} else {
|
|
|
|
mem::forget(p); // ownership passed to pthread_create
|
|
|
|
Ok(Thread { id: native })
|
|
|
|
};
|
|
|
|
|
|
|
|
extern fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
|
|
|
|
unsafe { start_thread(main); }
|
2015-09-03 06:49:50 +00:00
|
|
|
ptr::null_mut()
|
2015-04-15 05:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn yield_now() {
|
2015-11-03 00:23:22 +00:00
|
|
|
let ret = unsafe { libc::sched_yield() };
|
2015-04-15 05:13:57 +00:00
|
|
|
debug_assert_eq!(ret, 0);
|
|
|
|
}
|
|
|
|
|
2015-11-26 19:05:10 +00:00
|
|
|
#[cfg(any(target_os = "linux",
|
|
|
|
target_os = "android",
|
|
|
|
target_os = "emscripten"))]
|
2016-03-25 04:46:45 +00:00
|
|
|
pub fn set_name(name: &CStr) {
|
2015-04-15 05:13:57 +00:00
|
|
|
const PR_SET_NAME: libc::c_int = 15;
|
2015-11-03 00:23:22 +00:00
|
|
|
// pthread wrapper only appeared in glibc 2.12, so we use syscall
|
|
|
|
// directly.
|
2015-04-15 05:13:57 +00:00
|
|
|
unsafe {
|
2016-03-25 04:46:45 +00:00
|
|
|
libc::prctl(PR_SET_NAME, name.as_ptr() as libc::c_ulong, 0, 0, 0);
|
2015-04-15 05:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "freebsd",
|
|
|
|
target_os = "dragonfly",
|
|
|
|
target_os = "bitrig",
|
|
|
|
target_os = "openbsd"))]
|
2016-03-25 04:46:45 +00:00
|
|
|
pub fn set_name(name: &CStr) {
|
2015-04-15 05:13:57 +00:00
|
|
|
unsafe {
|
2016-03-25 04:46:45 +00:00
|
|
|
libc::pthread_set_name_np(libc::pthread_self(), name.as_ptr());
|
2015-04-15 05:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(target_os = "macos", target_os = "ios"))]
|
2016-03-25 04:46:45 +00:00
|
|
|
pub fn set_name(name: &CStr) {
|
2015-04-15 05:13:57 +00:00
|
|
|
unsafe {
|
2016-03-25 04:46:45 +00:00
|
|
|
libc::pthread_setname_np(name.as_ptr());
|
2015-04-15 05:13:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-20 15:39:52 +00:00
|
|
|
#[cfg(target_os = "netbsd")]
|
2016-03-25 04:46:45 +00:00
|
|
|
pub fn set_name(name: &CStr) {
|
|
|
|
use ffi::CString;
|
2015-09-20 15:39:52 +00:00
|
|
|
let cname = CString::new(&b"%s"[..]).unwrap();
|
|
|
|
unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
libc::pthread_setname_np(libc::pthread_self(), cname.as_ptr(),
|
2016-03-25 04:46:45 +00:00
|
|
|
name.as_ptr() as *mut libc::c_void);
|
2015-09-20 15:39:52 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-28 11:02:31 +00:00
|
|
|
#[cfg(any(target_env = "newlib", target_os = "solaris"))]
|
2016-03-25 04:46:45 +00:00
|
|
|
pub fn set_name(_name: &CStr) {
|
2016-01-21 16:30:22 +00:00
|
|
|
// Newlib and Illumos has no way to set a thread name.
|
2015-10-25 01:51:34 +00:00
|
|
|
}
|
2015-09-20 15:39:52 +00:00
|
|
|
|
2015-04-15 05:13:57 +00:00
|
|
|
pub fn sleep(dur: Duration) {
|
|
|
|
let mut ts = libc::timespec {
|
2015-07-06 06:20:00 +00:00
|
|
|
tv_sec: dur.as_secs() as libc::time_t,
|
|
|
|
tv_nsec: dur.subsec_nanos() as libc::c_long,
|
2015-04-15 05:13:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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 libc::nanosleep(&ts, &mut ts) == -1 {
|
|
|
|
assert_eq!(os::errno(), libc::EINTR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn join(self) {
|
|
|
|
unsafe {
|
2015-11-03 00:23:22 +00:00
|
|
|
let ret = libc::pthread_join(self.id, ptr::null_mut());
|
2015-04-15 05:13:57 +00:00
|
|
|
mem::forget(self);
|
|
|
|
debug_assert_eq!(ret, 0);
|
|
|
|
}
|
|
|
|
}
|
2015-12-04 02:58:00 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2015-04-15 05:13:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Drop for Thread {
|
|
|
|
fn drop(&mut self) {
|
2015-11-03 00:23:22 +00:00
|
|
|
let ret = unsafe { libc::pthread_detach(self.id) };
|
2015-04-15 05:13:57 +00:00
|
|
|
debug_assert_eq!(ret, 0);
|
|
|
|
}
|
|
|
|
}
|
2014-11-24 03:21:17 +00:00
|
|
|
|
2015-12-29 22:33:58 +00:00
|
|
|
#[cfg(all(not(all(target_os = "linux", not(target_env = "musl"))),
|
2016-04-04 14:18:44 +00:00
|
|
|
not(target_os = "freebsd"),
|
2015-01-29 07:19:28 +00:00
|
|
|
not(target_os = "macos"),
|
2015-01-17 07:51:04 +00:00
|
|
|
not(target_os = "bitrig"),
|
2015-09-21 17:16:24 +00:00
|
|
|
not(all(target_os = "netbsd", not(target_vendor = "rumprun"))),
|
2016-01-21 16:30:22 +00:00
|
|
|
not(target_os = "openbsd"),
|
2016-01-28 11:02:31 +00:00
|
|
|
not(target_os = "solaris")))]
|
2016-01-23 07:49:57 +00:00
|
|
|
#[cfg_attr(test, allow(dead_code))]
|
2014-11-24 03:21:17 +00:00
|
|
|
pub mod guard {
|
2015-07-16 18:59:53 +00:00
|
|
|
pub unsafe fn current() -> Option<usize> { None }
|
|
|
|
pub unsafe fn init() -> Option<usize> { None }
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|
|
|
|
|
2015-01-29 07:19:28 +00:00
|
|
|
|
2015-12-29 22:33:58 +00:00
|
|
|
#[cfg(any(all(target_os = "linux", not(target_env = "musl")),
|
2016-04-04 14:18:44 +00:00
|
|
|
target_os = "freebsd",
|
2015-01-29 07:19:28 +00:00
|
|
|
target_os = "macos",
|
2015-01-17 07:51:04 +00:00
|
|
|
target_os = "bitrig",
|
2015-09-21 17:16:24 +00:00
|
|
|
all(target_os = "netbsd", not(target_vendor = "rumprun")),
|
2016-01-21 16:30:22 +00:00
|
|
|
target_os = "openbsd",
|
2016-01-28 11:02:31 +00:00
|
|
|
target_os = "solaris"))]
|
2016-01-23 07:49:57 +00:00
|
|
|
#[cfg_attr(test, allow(dead_code))]
|
2014-11-24 03:21:17 +00:00
|
|
|
pub mod guard {
|
2015-07-16 18:59:53 +00:00
|
|
|
use prelude::v1::*;
|
|
|
|
|
2016-01-23 07:49:57 +00:00
|
|
|
use libc;
|
2015-11-03 00:23:22 +00:00
|
|
|
use libc::mmap;
|
|
|
|
use libc::{PROT_NONE, MAP_PRIVATE, MAP_ANON, MAP_FAILED, MAP_FIXED};
|
2015-03-13 03:21:17 +00:00
|
|
|
use sys::os;
|
2014-11-24 03:21:17 +00:00
|
|
|
|
2015-02-05 07:53:02 +00:00
|
|
|
#[cfg(any(target_os = "macos",
|
2015-01-17 07:51:04 +00:00
|
|
|
target_os = "bitrig",
|
2016-01-21 16:30:22 +00:00
|
|
|
target_os = "openbsd",
|
2016-01-28 11:02:31 +00:00
|
|
|
target_os = "solaris"))]
|
2015-07-16 18:59:53 +00:00
|
|
|
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
|
|
|
|
current().map(|s| s as *mut libc::c_void)
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 14:18:44 +00:00
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
|
|
|
|
let mut ret = None;
|
|
|
|
let mut attr: libc::pthread_attr_t = ::mem::zeroed();
|
|
|
|
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
|
|
|
|
if libc::pthread_attr_get_np(libc::pthread_self(), &mut attr) == 0 {
|
|
|
|
let mut stackaddr = ::ptr::null_mut();
|
|
|
|
let mut stacksize = 0;
|
|
|
|
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
|
|
|
|
&mut stacksize), 0);
|
|
|
|
ret = Some(stackaddr);
|
|
|
|
}
|
|
|
|
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2015-09-20 15:39:52 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
|
2015-07-16 18:59:53 +00:00
|
|
|
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
|
|
|
|
let mut ret = None;
|
2016-01-23 07:49:57 +00:00
|
|
|
let mut attr: libc::pthread_attr_t = ::mem::zeroed();
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
|
|
|
|
if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 {
|
2016-01-23 07:49:57 +00:00
|
|
|
let mut stackaddr = ::ptr::null_mut();
|
2015-07-16 18:59:53 +00:00
|
|
|
let mut stacksize = 0;
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
|
|
|
|
&mut stacksize), 0);
|
2015-07-16 18:59:53 +00:00
|
|
|
ret = Some(stackaddr);
|
|
|
|
}
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
|
2015-07-16 18:59:53 +00:00
|
|
|
ret
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|
|
|
|
|
2015-07-16 18:59:53 +00:00
|
|
|
pub unsafe fn init() -> Option<usize> {
|
2015-03-13 03:21:17 +00:00
|
|
|
let psize = os::page_size();
|
2015-07-16 18:59:53 +00:00
|
|
|
let mut stackaddr = match get_stack_start() {
|
|
|
|
Some(addr) => addr,
|
|
|
|
None => return None,
|
|
|
|
};
|
2015-01-11 03:52:50 +00:00
|
|
|
|
|
|
|
// Ensure stackaddr is page aligned! A parent process might
|
|
|
|
// have reset RLIMIT_STACK to be non-page aligned. The
|
|
|
|
// pthread_attr_getstack() reports the usable stack area
|
|
|
|
// stackaddr < stackaddr + stacksize, so if stackaddr is not
|
|
|
|
// page-aligned, calculate the fix such that stackaddr <
|
|
|
|
// new_page_aligned_stackaddr < stackaddr + stacksize
|
2015-03-13 03:21:17 +00:00
|
|
|
let remainder = (stackaddr as usize) % psize;
|
2015-01-11 03:52:50 +00:00
|
|
|
if remainder != 0 {
|
2015-03-13 03:21:17 +00:00
|
|
|
stackaddr = ((stackaddr as usize) + psize - remainder)
|
2015-01-11 03:52:50 +00:00
|
|
|
as *mut libc::c_void;
|
|
|
|
}
|
2014-11-24 03:21:17 +00:00
|
|
|
|
|
|
|
// Rellocate the last page of the stack.
|
|
|
|
// This ensures SIGBUS will be raised on
|
|
|
|
// stack overflow.
|
|
|
|
let result = mmap(stackaddr,
|
2015-03-13 03:21:17 +00:00
|
|
|
psize as libc::size_t,
|
2014-11-24 03:21:17 +00:00
|
|
|
PROT_NONE,
|
|
|
|
MAP_PRIVATE | MAP_ANON | MAP_FIXED,
|
|
|
|
-1,
|
|
|
|
0);
|
|
|
|
|
|
|
|
if result != stackaddr || result == MAP_FAILED {
|
|
|
|
panic!("failed to allocate a guard page");
|
|
|
|
}
|
|
|
|
|
2016-04-04 14:18:44 +00:00
|
|
|
let offset = if cfg!(any(target_os = "linux", target_os = "freebsd")) {
|
|
|
|
2
|
|
|
|
} else {
|
|
|
|
1
|
|
|
|
};
|
2014-11-24 03:21:17 +00:00
|
|
|
|
2015-07-16 18:59:53 +00:00
|
|
|
Some(stackaddr as usize + offset * psize)
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 11:02:31 +00:00
|
|
|
#[cfg(target_os = "solaris")]
|
2016-01-21 16:30:22 +00:00
|
|
|
pub unsafe fn current() -> Option<usize> {
|
2016-01-31 08:55:00 +00:00
|
|
|
let mut current_stack: libc::stack_t = ::mem::zeroed();
|
2016-01-21 16:30:22 +00:00
|
|
|
assert_eq!(libc::stack_getbounds(&mut current_stack), 0);
|
|
|
|
Some(current_stack.ss_sp as usize)
|
|
|
|
}
|
|
|
|
|
2014-11-24 03:21:17 +00:00
|
|
|
#[cfg(target_os = "macos")]
|
2015-07-16 18:59:53 +00:00
|
|
|
pub unsafe fn current() -> Option<usize> {
|
2015-11-03 00:23:22 +00:00
|
|
|
Some((libc::pthread_get_stackaddr_np(libc::pthread_self()) as libc::size_t -
|
|
|
|
libc::pthread_get_stacksize_np(libc::pthread_self())) as usize)
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|
|
|
|
|
2015-09-20 15:39:52 +00:00
|
|
|
#[cfg(any(target_os = "openbsd", target_os = "bitrig"))]
|
2015-07-16 18:59:53 +00:00
|
|
|
pub unsafe fn current() -> Option<usize> {
|
2016-01-23 07:49:57 +00:00
|
|
|
let mut current_stack: libc::stack_t = ::mem::zeroed();
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_stackseg_np(libc::pthread_self(),
|
|
|
|
&mut current_stack), 0);
|
2015-03-13 03:21:17 +00:00
|
|
|
|
|
|
|
let extra = if cfg!(target_os = "bitrig") {3} else {1} * os::page_size();
|
2015-11-03 00:23:22 +00:00
|
|
|
Some(if libc::pthread_main_np() == 1 {
|
2015-01-29 07:19:28 +00:00
|
|
|
// main thread
|
2015-03-13 03:21:17 +00:00
|
|
|
current_stack.ss_sp as usize - current_stack.ss_size as usize + extra
|
2015-01-29 07:19:28 +00:00
|
|
|
} else {
|
|
|
|
// new thread
|
2015-03-13 03:21:17 +00:00
|
|
|
current_stack.ss_sp as usize - current_stack.ss_size as usize
|
2015-07-16 18:59:53 +00:00
|
|
|
})
|
2015-01-29 07:19:28 +00:00
|
|
|
}
|
|
|
|
|
2016-04-04 14:18:44 +00:00
|
|
|
#[cfg(target_os = "freebsd")]
|
|
|
|
pub unsafe fn current() -> Option<usize> {
|
|
|
|
let mut ret = None;
|
|
|
|
let mut attr: libc::pthread_attr_t = ::mem::zeroed();
|
|
|
|
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
|
|
|
|
if libc::pthread_attr_get_np(libc::pthread_self(), &mut attr) == 0 {
|
|
|
|
let mut guardsize = 0;
|
|
|
|
assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0);
|
|
|
|
if guardsize == 0 {
|
|
|
|
panic!("there is no guard page");
|
|
|
|
}
|
|
|
|
let mut stackaddr = ::ptr::null_mut();
|
|
|
|
let mut size = 0;
|
|
|
|
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
|
|
|
|
&mut size), 0);
|
|
|
|
ret = Some(stackaddr as usize - guardsize as usize);
|
|
|
|
}
|
|
|
|
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
2015-09-20 15:39:52 +00:00
|
|
|
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
|
2015-07-16 18:59:53 +00:00
|
|
|
pub unsafe fn current() -> Option<usize> {
|
|
|
|
let mut ret = None;
|
2016-01-23 07:49:57 +00:00
|
|
|
let mut attr: libc::pthread_attr_t = ::mem::zeroed();
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
|
|
|
|
if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 {
|
2015-07-16 18:59:53 +00:00
|
|
|
let mut guardsize = 0;
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0);
|
2015-07-16 18:59:53 +00:00
|
|
|
if guardsize == 0 {
|
|
|
|
panic!("there is no guard page");
|
|
|
|
}
|
2016-01-23 07:49:57 +00:00
|
|
|
let mut stackaddr = ::ptr::null_mut();
|
2015-07-16 18:59:53 +00:00
|
|
|
let mut size = 0;
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
|
|
|
|
&mut size), 0);
|
2015-07-16 18:59:53 +00:00
|
|
|
|
2015-09-20 15:39:52 +00:00
|
|
|
ret = if cfg!(target_os = "netbsd") {
|
|
|
|
Some(stackaddr as usize)
|
|
|
|
} else {
|
|
|
|
Some(stackaddr as usize + guardsize as usize)
|
|
|
|
};
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|
2015-11-03 00:23:22 +00:00
|
|
|
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
|
2015-09-07 22:36:29 +00:00
|
|
|
ret
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// glibc >= 2.15 has a __pthread_get_minstack() function that returns
|
|
|
|
// PTHREAD_STACK_MIN plus however many bytes are needed for thread-local
|
|
|
|
// storage. We need that information to avoid blowing up when a small stack
|
|
|
|
// is created in an application with big thread-local storage requirements.
|
|
|
|
// See #6233 for rationale and details.
|
|
|
|
#[cfg(target_os = "linux")]
|
std: Stabilize library APIs for 1.5
This commit stabilizes and deprecates library APIs whose FCP has closed in the
last cycle, specifically:
Stabilized APIs:
* `fs::canonicalize`
* `Path::{metadata, symlink_metadata, canonicalize, read_link, read_dir, exists,
is_file, is_dir}` - all moved to inherent methods from the `PathExt` trait.
* `Formatter::fill`
* `Formatter::width`
* `Formatter::precision`
* `Formatter::sign_plus`
* `Formatter::sign_minus`
* `Formatter::alternate`
* `Formatter::sign_aware_zero_pad`
* `string::ParseError`
* `Utf8Error::valid_up_to`
* `Iterator::{cmp, partial_cmp, eq, ne, lt, le, gt, ge}`
* `<[T]>::split_{first,last}{,_mut}`
* `Condvar::wait_timeout` - note that `wait_timeout_ms` is not yet deprecated
but will be once 1.5 is released.
* `str::{R,}MatchIndices`
* `str::{r,}match_indices`
* `char::from_u32_unchecked`
* `VecDeque::insert`
* `VecDeque::shrink_to_fit`
* `VecDeque::as_slices`
* `VecDeque::as_mut_slices`
* `VecDeque::swap_remove_front` - (renamed from `swap_front_remove`)
* `VecDeque::swap_remove_back` - (renamed from `swap_back_remove`)
* `Vec::resize`
* `str::slice_mut_unchecked`
* `FileTypeExt`
* `FileTypeExt::{is_block_device, is_char_device, is_fifo, is_socket}`
* `BinaryHeap::from` - `from_vec` deprecated in favor of this
* `BinaryHeap::into_vec` - plus a `Into` impl
* `BinaryHeap::into_sorted_vec`
Deprecated APIs
* `slice::ref_slice`
* `slice::mut_ref_slice`
* `iter::{range_inclusive, RangeInclusive}`
* `std::dynamic_lib`
Closes #27706
Closes #27725
cc #27726 (align not stabilized yet)
Closes #27734
Closes #27737
Closes #27742
Closes #27743
Closes #27772
Closes #27774
Closes #27777
Closes #27781
cc #27788 (a few remaining methods though)
Closes #27790
Closes #27793
Closes #27796
Closes #27810
cc #28147 (not all parts stabilized)
2015-10-22 23:28:45 +00:00
|
|
|
#[allow(deprecated)]
|
2015-04-15 05:13:57 +00:00
|
|
|
fn min_stack_size(attr: *const libc::pthread_attr_t) -> usize {
|
2016-02-04 21:56:59 +00:00
|
|
|
weak!(fn __pthread_get_minstack(*const libc::pthread_attr_t) -> libc::size_t);
|
2015-03-23 04:58:19 +00:00
|
|
|
|
2016-02-04 21:56:59 +00:00
|
|
|
match __pthread_get_minstack.get() {
|
2016-01-29 03:33:29 +00:00
|
|
|
None => libc::PTHREAD_STACK_MIN as usize,
|
2015-04-15 05:13:57 +00:00
|
|
|
Some(f) => unsafe { f(attr) as usize },
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 07:48:06 +00:00
|
|
|
// No point in looking up __pthread_get_minstack() on non-glibc
|
|
|
|
// platforms.
|
2016-01-29 03:33:29 +00:00
|
|
|
#[cfg(all(not(target_os = "linux"),
|
|
|
|
not(target_os = "netbsd")))]
|
|
|
|
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
|
|
|
|
libc::PTHREAD_STACK_MIN as usize
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "netbsd")]
|
2015-04-15 05:13:57 +00:00
|
|
|
fn min_stack_size(_: *const libc::pthread_attr_t) -> usize {
|
2016-01-29 03:33:29 +00:00
|
|
|
2048 // just a guess
|
2014-11-24 03:21:17 +00:00
|
|
|
}
|