Skip to content

Commit

Permalink
Revise std::thread semantics
Browse files Browse the repository at this point in the history
This commit makes several changes to `std::thread` in preparation for
final stabilization:

* It removes the ability to handle panics from `scoped` children; see
  #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 #20807

[breaking-change]
  • Loading branch information
aturon committed Feb 17, 2015
1 parent e4e7aa2 commit d8f8f7a
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 113 deletions.
8 changes: 5 additions & 3 deletions src/libstd/sys/unix/thread.rs
Expand Up @@ -10,6 +10,7 @@

use core::prelude::*;

use io;
use boxed::Box;
use cmp;
use mem;
Expand Down Expand Up @@ -191,7 +192,7 @@ pub mod guard {
}
}

pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
pub unsafe fn create(stack: uint, p: Thunk) -> io::Result<rust_thread> {
let mut native: libc::pthread_t = mem::zeroed();
let mut attr: libc::pthread_attr_t = mem::zeroed();
assert_eq!(pthread_attr_init(&mut attr), 0);
Expand Down Expand Up @@ -226,9 +227,10 @@ pub unsafe fn create(stack: uint, p: Thunk) -> rust_thread {
if ret != 0 {
// be sure to not leak the closure
let _p: Box<Box<FnOnce()+Send>> = mem::transmute(arg);
panic!("failed to spawn native thread: {}", ret);
Err(io::Error::from_os_error(ret))
} else {
Ok(native)
}
native
}

#[cfg(any(target_os = "linux", target_os = "android"))]
Expand Down
8 changes: 5 additions & 3 deletions src/libstd/sys/windows/thread.rs
Expand Up @@ -10,6 +10,7 @@

use boxed::Box;
use cmp;
use io;
use mem;
use ptr;
use libc;
Expand Down Expand Up @@ -42,7 +43,7 @@ pub mod guard {
}
}

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

pub unsafe fn set_name(_name: &str) {
Expand Down

0 comments on commit d8f8f7a

Please sign in to comment.