Skip to content

Commit d9a123b

Browse files
committed
simplify pthread error handling
1 parent 53918d0 commit d9a123b

File tree

3 files changed

+44
-44
lines changed

3 files changed

+44
-44
lines changed

src/backends/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ cfg_if! {
1414
target_os = "illumos"
1515
))] {
1616
mod unix;
17-
mod unix_pthread_wrapper;
1817
pub use unix::guess_os_stack_limit;
1918
} else if #[cfg(target_os = "openbsd")] {
2019
mod openbsd;

src/backends/unix.rs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use super::unix_pthread_wrapper::PthreadAttr;
2-
31
#[cfg(any(target_os = "freebsd", target_os = "dragonfly", target_os = "illumos"))]
42
use libc::pthread_attr_get_np as get_attr;
53
#[cfg(any(target_os = "linux", target_os = "solaris", target_os = "netbsd"))]
@@ -8,13 +6,53 @@ use libc::pthread_getattr_np as get_attr;
86
pub unsafe fn guess_os_stack_limit() -> Option<usize> {
97
let mut attr = PthreadAttr::new()?;
108

11-
let res = get_attr(libc::pthread_self(), attr.as_mut_ptr());
12-
attr.handle_pthread_err(res)?;
9+
handle_pthread_err(get_attr(libc::pthread_self(), attr.as_mut_ptr()))?;
1310

1411
let mut stackaddr = std::ptr::null_mut();
1512
let mut stacksize = 0;
16-
let res = libc::pthread_attr_getstack(attr.as_mut_ptr(), &mut stackaddr, &mut stacksize);
17-
attr.handle_pthread_err(res)?;
13+
handle_pthread_err(libc::pthread_attr_getstack(
14+
attr.as_mut_ptr(),
15+
&mut stackaddr,
16+
&mut stacksize,
17+
))?;
1818

1919
Some(stackaddr as usize)
2020
}
21+
22+
struct PthreadAttr(std::mem::MaybeUninit<libc::pthread_attr_t>);
23+
24+
impl Drop for PthreadAttr {
25+
fn drop(&mut self) {
26+
unsafe {
27+
let ret = libc::pthread_attr_destroy(self.0.as_mut_ptr());
28+
if ret != 0 {
29+
let err = std::io::Error::last_os_error();
30+
panic!(
31+
"pthread_attr_destroy failed with error code {}: {}",
32+
ret, err
33+
);
34+
}
35+
}
36+
}
37+
}
38+
39+
fn handle_pthread_err(ret: libc::c_int) -> Option<()> {
40+
if ret != 0 {
41+
return None;
42+
}
43+
Some(())
44+
}
45+
46+
impl PthreadAttr {
47+
unsafe fn new() -> Option<Self> {
48+
let mut attr = std::mem::MaybeUninit::<libc::pthread_attr_t>::uninit();
49+
if libc::pthread_attr_init(attr.as_mut_ptr()) != 0 {
50+
return None;
51+
}
52+
Some(PthreadAttr(attr))
53+
}
54+
55+
fn as_mut_ptr(&mut self) -> *mut libc::pthread_attr_t {
56+
self.0.as_mut_ptr()
57+
}
58+
}

src/backends/unix_pthread_wrapper.rs

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)