Auto merge of #44112 - alexcrichton:thread-join, r=sfackler

std: Handle OS errors when joining threads

Also add to the documentation that the `join` method can panic.

cc #34971
cc #43539
This commit is contained in:
bors 2017-08-27 04:20:28 +00:00
commit 93cdf5e3c4
4 changed files with 13 additions and 2 deletions

View file

@ -168,7 +168,8 @@ impl Thread {
unsafe {
let ret = libc::pthread_join(self.id, ptr::null_mut());
mem::forget(self);
debug_assert_eq!(ret, 0);
assert!(ret == 0,
"failed to join thread: {}", io::Error::from_raw_os_error(ret));
}
}

View file

@ -273,6 +273,7 @@ pub const FILE_END: DWORD = 2;
pub const WAIT_OBJECT_0: DWORD = 0x00000000;
pub const WAIT_TIMEOUT: DWORD = 258;
pub const WAIT_FAILED: DWORD = 0xFFFFFFFF;
#[cfg(target_env = "msvc")]
pub const MAX_SYM_NAME: usize = 2000;

View file

@ -61,7 +61,11 @@ impl Thread {
}
pub fn join(self) {
unsafe { c::WaitForSingleObject(self.handle.raw(), c::INFINITE); }
let rc = unsafe { c::WaitForSingleObject(self.handle.raw(), c::INFINITE) };
if rc == c::WAIT_FAILED {
panic!("failed to join on thread: {}",
io::Error::last_os_error());
}
}
pub fn yield_now() {

View file

@ -1230,6 +1230,11 @@ impl<T> JoinHandle<T> {
/// [`Err`]: ../../std/result/enum.Result.html#variant.Err
/// [`panic`]: ../../std/macro.panic.html
///
/// # Panics
///
/// This function may panic on some platforms if a thread attempts to join
/// itself or otherwise may create a deadlock with joining threads.
///
/// # Examples
///
/// ```