Skip to content

Implement Partial{,Eq} for JoinHandle & os::Thread #29457

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

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
1 change: 1 addition & 0 deletions src/liblibc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7029,6 +7029,7 @@ pub mod funcs {
dwOptions: DWORD)
-> BOOL;
pub fn CloseHandle(hObject: HANDLE) -> BOOL;
pub fn CompareObjectHandles(h1: HANDLE, h2: HANDLE) -> BOOL;
pub fn OpenProcess(dwDesiredAccess: DWORD,
bInheritHandle: BOOL,
dwProcessId: DWORD)
Expand Down
10 changes: 10 additions & 0 deletions src/libstd/sys/unix/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use time::Duration;

use sys_common::thread::*;

#[derive(Eq)]
pub struct Thread {
id: libc::pthread_t,
}
Expand Down Expand Up @@ -169,6 +170,14 @@ impl Thread {
}
}

impl PartialEq for Thread {
fn eq(&self, other: &Self) -> bool {
unsafe {
pthread_equal(self.id, other.id) != 0
}
}
}

impl Drop for Thread {
fn drop(&mut self) {
let ret = unsafe { pthread_detach(self.id) };
Expand Down Expand Up @@ -403,6 +412,7 @@ extern {
value: *mut libc::c_void) -> libc::c_int;
fn pthread_join(native: libc::pthread_t,
value: *mut *mut libc::c_void) -> libc::c_int;
fn pthread_equal(t1: libc::pthread_t, t2: libc::pthread_t) -> libc::c_int;
fn pthread_attr_init(attr: *mut libc::pthread_attr_t) -> libc::c_int;
fn pthread_attr_destroy(attr: *mut libc::pthread_attr_t) -> libc::c_int;
fn pthread_attr_setstacksize(attr: *mut libc::pthread_attr_t,
Expand Down
11 changes: 10 additions & 1 deletion src/libstd/sys/windows/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ use sys::cvt;
/// An owned container for `HANDLE` object, closing them on Drop.
///
/// All methods are inherited through a `Deref` impl to `RawHandle`
#[derive(PartialEq, Eq)]
pub struct Handle(RawHandle);

/// A wrapper type for `HANDLE` objects to give them proper Send/Sync inference
/// as well as Rust-y methods.
///
/// This does **not** drop the handle when it goes out of scope, use `Handle`
/// instead for that.
#[derive(Copy, Clone)]
#[derive(Copy, Clone, Eq)]
pub struct RawHandle(HANDLE);

unsafe impl Send for RawHandle {}
Expand Down Expand Up @@ -106,3 +107,11 @@ impl RawHandle {
Ok(Handle::new(ret))
}
}

impl PartialEq for RawHandle {
fn eq(&self, other: &Self) -> bool {
unsafe {
libc::CompareObjectHandles(self.0, other.0) != libc::FALSE
}
}
}
1 change: 1 addition & 0 deletions src/libstd/sys/windows/thread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use sys::handle::Handle;
use sys_common::thread::*;
use time::Duration;

#[derive(PartialEq, Eq)]
pub struct Thread {
handle: Handle
}
Expand Down
21 changes: 21 additions & 0 deletions src/libstd/thread/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,13 +590,22 @@ impl<T> JoinInner<T> {
}
}

impl<T> PartialEq for JoinInner<T> {
fn eq(&self, other: &Self) -> bool {
self.native == other.native
}
}

impl<T> Eq for JoinInner<T> {}

/// An owned permission to join on a thread (block on its termination).
///
/// A `JoinHandle` *detaches* the child thread when it is dropped.
///
/// Due to platform restrictions, it is not possible to `Clone` this
/// handle: the ability to join a child thread is a uniquely-owned
/// permission.
#[derive(Eq, PartialEq)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct JoinHandle<T>(JoinInner<T>);

Expand Down Expand Up @@ -633,6 +642,7 @@ mod tests {

use any::Any;
use sync::mpsc::{channel, Sender};
use sync::{Barrier, Arc};
use result;
use super::{Builder};
use thread;
Expand Down Expand Up @@ -665,6 +675,17 @@ mod tests {
rx.recv().unwrap();
}

#[test]
fn test_thread_guard_equality() {
let barrier = Arc::new(Barrier::new(2));
let b = barrier.clone();
let h = thread::spawn(move|| {
b.wait();
});
assert!(h == h);
barrier.wait();
}

#[test]
fn test_join_panic() {
match thread::spawn(move|| {
Expand Down