Skip to content

Commit d8f8f7a

Browse files
committed
Revise std::thread semantics
This commit makes several changes to `std::thread` in preparation for final stabilization: * It removes the ability to handle panics from `scoped` children; see rust-lang#20807 for discussion * It adds a `JoinHandle` structure, now returned from `spawn`, which makes it possible to join on children that do not share data from their parent's stack. The child is automatically detached when the handle is dropped, and the handle cannot be copied due to Posix semantics. * It moves all static methods from `std::thread::Thread` to free functions in `std::thread`. This was done in part because, due to the above changes, there are effectively no direct `Thread` constructors, and the static methods have tended to feel a bit awkward. * Adds an `io::Result` around the `Builder` methods `scoped` and `spawn`, making it possible to handle OS errors when creating threads. The convenience free functions entail an unwrap. * Stabilizes the entire module. Despite the fact that the API is changing somewhat here, this is part of a long period of baking and the changes are addressing all known issues prior to alpha2. If absolutely necessary, further breaking changes can be made prior to beta. Closes rust-lang#20807 [breaking-change]
1 parent e4e7aa2 commit d8f8f7a

File tree

3 files changed

+273
-113
lines changed

3 files changed

+273
-113
lines changed

src/libstd/sys/unix/thread.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use core::prelude::*;
1212

13+
use io;
1314
use boxed::Box;
1415
use cmp;
1516
use mem;
@@ -191,7 +192,7 @@ pub mod guard {
191192
}
192193
}
193194

194-
pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
195+
pub unsafe fn create(stack: uint, p: Thunk) -> io::Result<rust_thread> {
195196
let mut native: libc::pthread_t = mem::zeroed();
196197
let mut attr: libc::pthread_attr_t = mem::zeroed();
197198
assert_eq!(pthread_attr_init(&mut attr), 0);
@@ -226,9 +227,10 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
226227
if ret != 0 {
227228
// be sure to not leak the closure
228229
let _p: Box<Box<FnOnce()+Send>> = mem::transmute(arg);
229-
panic!("failed to spawn native thread: {}", ret);
230+
Err(io::Error::from_os_error(ret))
231+
} else {
232+
Ok(native)
230233
}
231-
native
232234
}
233235

234236
#[cfg(any(target_os = "linux", target_os = "android"))]

src/libstd/sys/windows/thread.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use boxed::Box;
1212
use cmp;
13+
use io;
1314
use mem;
1415
use ptr;
1516
use libc;
@@ -42,7 +43,7 @@ pub mod guard {
4243
}
4344
}
4445

45-
pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
46+
pub unsafe fn create(stack: uint, p: Thunk) -> io::Result<rust_thread> {
4647
let arg: *mut libc::c_void = mem::transmute(box p);
4748
// FIXME On UNIX, we guard against stack sizes that are too small but
4849
// that's because pthreads enforces that stacks are at least
@@ -60,9 +61,10 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
6061
if ret as uint == 0 {
6162
// be sure to not leak the closure
6263
let _p: Box<Thunk> = mem::transmute(arg);
63-
panic!("failed to spawn native thread: {:?}", ret);
64+
Err(io::Error::last_os_error())
65+
} else {
66+
Ok(ret)
6467
}
65-
return ret;
6668
}
6769

6870
pub unsafe fn set_name(_name: &str) {

0 commit comments

Comments
 (0)