Skip to content

Commit 5382347

Browse files
author
Vytautas Astrauskas
committed
Use Box::into_raw instead of ManuallyDrop in Thread::new.
1 parent 753bc7d commit 5382347

File tree

5 files changed

+20
-36
lines changed

5 files changed

+20
-36
lines changed

Diff for: src/libstd/sys/cloudabi/thread.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,21 @@ unsafe impl Sync for Thread {}
2222
impl Thread {
2323
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
2424
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
25-
let mut p = mem::ManuallyDrop::new(box p);
25+
let p = Box::into_raw(box p);
2626
let mut native: libc::pthread_t = mem::zeroed();
2727
let mut attr: libc::pthread_attr_t = mem::zeroed();
2828
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
2929

3030
let stack_size = cmp::max(stack, min_stack_size(&attr));
3131
assert_eq!(libc::pthread_attr_setstacksize(&mut attr, stack_size), 0);
3232

33-
let ret = libc::pthread_create(
34-
&mut native,
35-
&attr,
36-
thread_start,
37-
&mut *p as &mut Box<dyn FnOnce()> as *mut _ as *mut _,
38-
);
33+
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
3934
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
4035

4136
return if ret != 0 {
4237
// The thread failed to start and as a result p was not consumed. Therefore, it is
43-
// safe to manually drop it.
44-
mem::ManuallyDrop::drop(&mut p);
38+
// safe to reconstruct the box so that it gets deallocated.
39+
let _ = Box::from_raw(p);
4540
Err(io::Error::from_raw_os_error(ret))
4641
} else {
4742
Ok(Thread { id: native })

Diff for: src/libstd/sys/hermit/thread.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,20 @@ impl Thread {
4848
p: Box<dyn FnOnce()>,
4949
core_id: isize,
5050
) -> io::Result<Thread> {
51-
let mut p = mem::ManuallyDrop::new(box p);
51+
let p = Box::into_raw(box p);
5252
let mut tid: Tid = u32::MAX;
5353
let ret = abi::spawn(
5454
&mut tid as *mut Tid,
5555
thread_start,
56-
&mut *p as &mut Box<dyn FnOnce()> as *mut _ as *mut u8 as usize,
56+
p as *mut u8 as usize,
5757
Priority::into(NORMAL_PRIO),
5858
core_id,
5959
);
6060

6161
return if ret != 0 {
6262
// The thread failed to start and as a result p was not consumed. Therefore, it is
63-
// safe to manually drop it.
64-
mem::ManuallyDrop::drop(&mut p);
63+
// safe to reconstruct the box so that it gets deallocated.
64+
let _ = Box::from_raw(p);
6565
Err(io::Error::new(io::ErrorKind::Other, "Unable to create thread!"))
6666
} else {
6767
Ok(Thread { tid: tid })

Diff for: src/libstd/sys/unix/thread.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ unsafe fn pthread_attr_setstacksize(
4141
impl Thread {
4242
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
4343
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
44-
let mut p = mem::ManuallyDrop::new(box p);
44+
let p = Box::into_raw(box p);
4545
let mut native: libc::pthread_t = mem::zeroed();
4646
let mut attr: libc::pthread_attr_t = mem::zeroed();
4747
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
@@ -63,18 +63,13 @@ impl Thread {
6363
}
6464
};
6565

66-
let ret = libc::pthread_create(
67-
&mut native,
68-
&attr,
69-
thread_start,
70-
&mut *p as &mut Box<dyn FnOnce()> as *mut _ as *mut _,
71-
);
66+
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
7267
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
7368

7469
return if ret != 0 {
7570
// The thread failed to start and as a result p was not consumed. Therefore, it is
76-
// safe to manually drop it.
77-
mem::ManuallyDrop::drop(&mut p);
71+
// safe to reconstruct the box so that it gets deallocated.
72+
let _ = Box::from_raw(p);
7873
Err(io::Error::from_raw_os_error(ret))
7974
} else {
8075
Ok(Thread { id: native })

Diff for: src/libstd/sys/vxworks/thread.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ unsafe fn pthread_attr_setstacksize(
2929
impl Thread {
3030
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
3131
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
32-
let mut p = mem::ManuallyDrop::new(box p);
32+
let p = Box::into_raw(box p);
3333
let mut native: libc::pthread_t = mem::zeroed();
3434
let mut attr: libc::pthread_attr_t = mem::zeroed();
3535
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
@@ -51,18 +51,13 @@ impl Thread {
5151
}
5252
};
5353

54-
let ret = libc::pthread_create(
55-
&mut native,
56-
&attr,
57-
thread_start,
58-
&mut *p as &mut Box<dyn FnOnce()> as *mut _ as *mut _,
59-
);
54+
let ret = libc::pthread_create(&mut native, &attr, thread_start, p as *mut _);
6055
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);
6156

6257
return if ret != 0 {
6358
// The thread failed to start and as a result p was not consumed. Therefore, it is
64-
// safe to manually drop it.
65-
mem::ManuallyDrop::drop(&mut p);
59+
// safe to reconstruct the box so that it gets deallocated.
60+
let _ = Box::from_raw(p);
6661
Err(io::Error::from_raw_os_error(ret))
6762
} else {
6863
Ok(Thread { id: native })

Diff for: src/libstd/sys/windows/thread.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::ffi::CStr;
22
use crate::io;
3-
use crate::mem;
43
use crate::ptr;
54
use crate::sys::c;
65
use crate::sys::handle::Handle;
@@ -20,7 +19,7 @@ pub struct Thread {
2019
impl Thread {
2120
// unsafe: see thread::Builder::spawn_unchecked for safety requirements
2221
pub unsafe fn new(stack: usize, p: Box<dyn FnOnce()>) -> io::Result<Thread> {
23-
let mut p = mem::ManuallyDrop::new(box p);
22+
let p = Box::into_raw(box p);
2423

2524
// FIXME On UNIX, we guard against stack sizes that are too small but
2625
// that's because pthreads enforces that stacks are at least
@@ -34,15 +33,15 @@ impl Thread {
3433
ptr::null_mut(),
3534
stack_size,
3635
thread_start,
37-
&mut *p as &mut Box<dyn FnOnce()> as *mut _ as *mut _,
36+
p as *mut _,
3837
c::STACK_SIZE_PARAM_IS_A_RESERVATION,
3938
ptr::null_mut(),
4039
);
4140

4241
return if ret as usize == 0 {
4342
// The thread failed to start and as a result p was not consumed. Therefore, it is
44-
// safe to manually drop it.
45-
mem::ManuallyDrop::drop(&mut p);
43+
// safe to reconstruct the box so that it gets deallocated.
44+
let _ = Box::from_raw(p);
4645
Err(io::Error::last_os_error())
4746
} else {
4847
Ok(Thread { handle: Handle::new(ret) })

0 commit comments

Comments
 (0)