Skip to content

Commit

Permalink
Only call pthread_attr_destroy() after getattr_np() succeeds on all l…
Browse files Browse the repository at this point in the history
…ibcs

The calling convention of pthread_getattr_np() is to initialize the
pthread_attr_t, so _destroy() is only necessary on success (and _init()
isn't necessary beforehand).  On the other hand, FreeBSD wants the
attr_t to be initialized before pthread_attr_get_np(), and therefore it
should always be destroyed afterwards.
  • Loading branch information
tavianator committed Sep 9, 2020
1 parent a06edda commit a684153
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions library/std/src/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ pub mod guard {
unsafe fn get_stack_start() -> Option<*mut libc::c_void> {
let mut ret = None;
let mut attr: libc::pthread_attr_t = crate::mem::zeroed();
#[cfg(target_os = "freebsd")]
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
#[cfg(target_os = "freebsd")]
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
Expand All @@ -305,7 +306,7 @@ pub mod guard {
assert_eq!(libc::pthread_attr_getstack(&attr, &mut stackaddr, &mut stacksize), 0);
ret = Some(stackaddr);
}
if e == 0 || cfg!(not(target_env = "gnu")) {
if e == 0 || cfg!(target_os = "freebsd") {
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
}
ret
Expand Down Expand Up @@ -405,6 +406,7 @@ pub mod guard {
pub unsafe fn current() -> Option<Guard> {
let mut ret = None;
let mut attr: libc::pthread_attr_t = crate::mem::zeroed();
#[cfg(target_os = "freebsd")]
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
#[cfg(target_os = "freebsd")]
let e = libc::pthread_attr_get_np(libc::pthread_self(), &mut attr);
Expand Down Expand Up @@ -448,7 +450,7 @@ pub mod guard {
Some(stackaddr..stackaddr + guardsize)
};
}
if e == 0 || cfg!(not(target_env = "gnu")) {
if e == 0 || cfg!(target_os = "freebsd") {
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
}
ret
Expand Down

0 comments on commit a684153

Please sign in to comment.