std: Update std::rt::thread to specify stack sizes

It's now possible to spawn an OS thread with a stack that has a specific size.
This commit is contained in:
Alex Crichton 2013-12-12 18:06:39 -08:00
parent 7554f5c58f
commit 780afeaf0a
1 changed files with 16 additions and 8 deletions

View File

@ -33,7 +33,7 @@ pub struct Thread<T> {
priv packet: ~Option<T>,
}
static DEFAULT_STACK_SIZE: libc::size_t = 1024 * 1024;
static DEFAULT_STACK_SIZE: uint = 1024 * 1024;
// This is the starting point of rust os threads. The first thing we do
// is make sure that we don't trigger __morestack (also why this has a
@ -84,7 +84,7 @@ impl Thread<()> {
*cast::transmute::<&~Option<T>, **mut Option<T>>(&packet)
};
let main: proc() = proc() unsafe { *packet2 = Some(main()); };
let native = unsafe { imp::create(~main) };
let native = unsafe { imp::create(stack, ~main) };
Thread {
native: native,
@ -100,8 +100,14 @@ impl Thread<()> {
/// systems. Note that platforms may not keep the main program alive even if
/// there are detached thread still running around.
pub fn spawn(main: proc()) {
Thread::spawn_stack(DEFAULT_STACK_SIZE, main)
}
/// Performs the same functionality as `spawn`, but explicitly specifies a
/// stack size for the new thread.
pub fn spawn_stack(stack: uint, main: proc()) {
unsafe {
let handle = imp::create(~main);
let handle = imp::create(stack, ~main);
imp::detach(handle);
}
}
@ -145,13 +151,15 @@ mod imp {
use libc::types::os::arch::extra::{LPSECURITY_ATTRIBUTES, SIZE_T, BOOL,
LPVOID, DWORD, LPDWORD, HANDLE};
use ptr;
use libc;
use cast;
pub type rust_thread = HANDLE;
pub type rust_thread_return = DWORD;
pub unsafe fn create(p: ~proc()) -> rust_thread {
pub unsafe fn create(stack: uint, p: ~proc()) -> rust_thread {
let arg: *mut libc::c_void = cast::transmute(p);
CreateThread(ptr::mut_null(), DEFAULT_STACK_SIZE, super::thread_start,
CreateThread(ptr::mut_null(), stack as libc::size_t, super::thread_start,
arg, 0, ptr::mut_null())
}
@ -189,17 +197,17 @@ mod imp {
use libc::consts::os::posix01::PTHREAD_CREATE_JOINABLE;
use libc;
use ptr;
use super::DEFAULT_STACK_SIZE;
use unstable::intrinsics;
pub type rust_thread = libc::pthread_t;
pub type rust_thread_return = *libc::c_void;
pub unsafe fn create(p: ~proc()) -> rust_thread {
pub unsafe fn create(stack: uint, p: ~proc()) -> rust_thread {
let mut native: libc::pthread_t = intrinsics::uninit();
let mut attr: libc::pthread_attr_t = intrinsics::uninit();
assert_eq!(pthread_attr_init(&mut attr), 0);
assert_eq!(pthread_attr_setstacksize(&mut attr, DEFAULT_STACK_SIZE), 0);
assert_eq!(pthread_attr_setstacksize(&mut attr,
stack as libc::size_t), 0);
assert_eq!(pthread_attr_setdetachstate(&mut attr,
PTHREAD_CREATE_JOINABLE), 0);