Skip to content

Commit

Permalink
Consistently using as_mut_ptr() and as_ptr() in thread
Browse files Browse the repository at this point in the history
Signed-off-by: xizheyin <[email protected]>
  • Loading branch information
xizheyin committed Feb 11, 2025
1 parent f701add commit 2dc051f
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions library/std/src/sys/pal/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,27 @@ impl Thread {
let p = Box::into_raw(Box::new(p));
let mut native: libc::pthread_t = mem::zeroed();
let mut attr: mem::MaybeUninit<libc::pthread_attr_t> = mem::MaybeUninit::uninit();
assert_eq!(libc::pthread_attr_init((&raw mut attr) as *mut _), 0);
let mut attr: libc::pthread_attr_t = unsafe { attr.assume_init() };
assert_eq!(libc::pthread_attr_init(attr.as_mut_ptr()), 0);
//let mut attr: libc::pthread_attr_t = unsafe { attr.assume_init() };

#[cfg(target_os = "espidf")]
if stack > 0 {
// Only set the stack if a non-zero value is passed
// 0 is used as an indication that the default stack size configured in the ESP-IDF menuconfig system should be used
assert_eq!(
libc::pthread_attr_setstacksize(&mut attr, cmp::max(stack, min_stack_size(&attr))),
libc::pthread_attr_setstacksize(
attr.as_mut_ptr(),
cmp::max(stack, min_stack_size(&attr))
),
0
);
}

#[cfg(not(target_os = "espidf"))]
{
let stack_size = cmp::max(stack, min_stack_size(&attr));
let stack_size = cmp::max(stack, min_stack_size(attr.as_ptr()));

match libc::pthread_attr_setstacksize(&mut attr, stack_size) {
match libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size) {
0 => {}
n => {
assert_eq!(n, libc::EINVAL);
Expand All @@ -78,16 +81,16 @@ impl Thread {
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);
assert_eq!(libc::pthread_attr_setstacksize(attr.as_mut_ptr(), stack_size), 0);
}
};
}

let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
let ret = libc::pthread_create(&mut native, attr.as_ptr(), 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);
assert_eq!(libc::pthread_attr_destroy(attr.as_mut_ptr()), 0);

return if ret != 0 {
// The thread failed to start and as a result p was not consumed. Therefore, it is
Expand Down

0 comments on commit 2dc051f

Please sign in to comment.