Reduce code duplication in thread.rs

This commit is contained in:
Alan Somers 2016-04-05 03:25:32 +00:00
parent abc3777c06
commit 112463a3b1

View File

@ -201,28 +201,17 @@ pub mod guard {
current().map(|s| s as *mut libc::c_void)
}
#[cfg(target_os = "freebsd")]
#[cfg(any(target_os = "android", target_os = "freebsd",
target_os = "linux", target_os = "netbsd"))]
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
}
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
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_getattr_np(libc::pthread_self(), &mut attr) == 0 {
#[cfg(target_os = "freebsd")]
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
#[cfg(not(target_os = "freebsd"))]
let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr);
if e == 0 {
let mut stackaddr = ::ptr::null_mut();
let mut stacksize = 0;
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
@ -304,33 +293,18 @@ pub mod guard {
})
}
#[cfg(target_os = "freebsd")]
#[cfg(any(target_os = "android", target_os = "freebsd",
target_os = "linux", target_os = "netbsd"))]
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
}
#[cfg(any(target_os = "linux", target_os = "android", target_os = "netbsd"))]
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_getattr_np(libc::pthread_self(), &mut attr) == 0 {
#[cfg(target_os = "freebsd")]
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
#[cfg(not(target_os = "freebsd"))]
let e = libc::pthread_getattr_np(libc::pthread_self(), &mut attr);
if e == 0 {
//if libc::pthread_getattr_np(libc::pthread_self(), &mut attr) == 0 {
let mut guardsize = 0;
assert_eq!(libc::pthread_attr_getguardsize(&attr, &mut guardsize), 0);
if guardsize == 0 {
@ -341,7 +315,9 @@ pub mod guard {
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr,
&mut size), 0);
ret = if cfg!(target_os = "netbsd") {
ret = if cfg!(target_os = "freebsd") {
Some(stackaddr as usize - guardsize as usize)
} else if cfg!(target_os = "netbsd") {
Some(stackaddr as usize)
} else {
Some(stackaddr as usize + guardsize as usize)