Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only box the function once when creating threads. #31621

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/libstd/sys/common/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
use prelude::v1::*;

use alloc::boxed::FnBox;
use libc;
use sys::stack_overflow;

pub unsafe fn start_thread(main: *mut libc::c_void) {
pub unsafe fn start_thread<'a>(main: Box<FnBox() + 'a>) {
// Next, set up our stack overflow handler which may get triggered if we run
// out of stack.
let _handler = stack_overflow::Handler::new();

// Finally, let's run some code.
Box::from_raw(main as *mut Box<FnBox()>)()
main();
}
39 changes: 28 additions & 11 deletions src/libstd/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

use prelude::v1::*;

use alloc::boxed::FnBox;
use cmp;
#[cfg(not(any(target_env = "newlib", target_os = "solaris")))]
use ffi::CString;
Expand All @@ -33,9 +32,33 @@ unsafe impl Send for Thread {}
unsafe impl Sync for Thread {}

impl Thread {
pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>)
pub unsafe fn new<F: FnOnce()>(stack: usize, p: F)
-> io::Result<Thread> {

extern fn thread_start<F: FnOnce()>(main: *mut libc::c_void)
-> *mut libc::c_void {
unsafe {
let main = Box::from_raw(main as *mut F);
start_thread(main);
}
ptr::null_mut()
}

let p = box p;

match Thread::new_inner(stack, &*p as *const _ as *const _, thread_start::<F>) {
Ok(thread) => {
mem::forget(p); // ownership passed to pthread_create
Ok(thread)
}

Err(e) => Err(e),
}
}

unsafe fn new_inner(stack: usize, p: *const libc::c_void,
f: extern fn(*mut libc::c_void) -> *mut libc::c_void)
-> io::Result<Thread> {
let mut native: libc::pthread_t = mem::zeroed();
let mut attr: libc::pthread_attr_t = mem::zeroed();
assert_eq!(libc::pthread_attr_init(&mut attr), 0);
Expand All @@ -59,20 +82,14 @@ impl Thread {
}
};

let ret = libc::pthread_create(&mut native, &attr, thread_start,
&*p as *const _ as *mut _);
let ret = libc::pthread_create(&mut native, &attr, f,
p as *mut _);
assert_eq!(libc::pthread_attr_destroy(&mut attr), 0);

return if ret != 0 {
if ret != 0 {
Err(io::Error::from_raw_os_error(ret))
} else {
mem::forget(p); // ownership passed to pthread_create
Ok(Thread { id: native })
};

extern fn thread_start(main: *mut libc::c_void) -> *mut libc::c_void {
unsafe { start_thread(main); }
ptr::null_mut()
}
}

Expand Down
33 changes: 24 additions & 9 deletions src/libstd/sys/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,31 @@ pub struct Thread {
}

impl Thread {
pub unsafe fn new<'a>(stack: usize, p: Box<FnBox() + 'a>)
pub unsafe fn new<F: FnOnce()>(stack: usize, p: F)
-> io::Result<Thread> {
extern "system" fn thread_start<F: FnOnce()>(main: *mut c_void)
-> c::DWORD {
unsafe {
let main = Box::from_raw(main as *mut F);
start_thread(main);
}
0
}

let p = box p;

match Thread::new_inner(stack, &*p as *const _ as *const _, thread_start::<F>) {
Ok(thread) => {
mem::forget(p); // ownership passed to CreateThread
Ok(thread)
}
Err(e) => Err(e),
}
}

unsafe fn new_inner(stack: usize, p: *const c_void,
f: extern "system" fn(*mut c_void) -> c::DWORD)
-> io::Result<Thread> {
// FIXME On UNIX, we guard against stack sizes that are too small but
// that's because pthreads enforces that stacks are at least
// PTHREAD_STACK_MIN bytes big. Windows has no such lower limit, it's
Expand All @@ -37,21 +58,15 @@ impl Thread {
// Round up to the next 64 kB because that's what the NT kernel does,
// might as well make it explicit.
let stack_size = (stack + 0xfffe) & (!0xfffe);
let ret = c::CreateThread(ptr::null_mut(), stack_size,
thread_start, &*p as *const _ as *mut _,
let ret = c::CreateThread(ptr::null_mut(), stack,
f, p as *mut _,
0, ptr::null_mut());

return if ret as usize == 0 {
Err(io::Error::last_os_error())
} else {
mem::forget(p); // ownership passed to CreateThread
Ok(Thread { handle: Handle::new(ret) })
};

extern "system" fn thread_start(main: *mut c_void) -> c::DWORD {
unsafe { start_thread(main); }
0
}
}

pub fn set_name(_name: &str) {
Expand Down
2 changes: 1 addition & 1 deletion src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ impl Builder {

Ok(JoinHandle(JoinInner {
native: unsafe {
Some(try!(imp::Thread::new(stack_size, Box::new(main))))
Some(try!(imp::Thread::new(stack_size, main)))
},
thread: my_thread,
packet: Packet(my_packet),
Expand Down