Skip to content

Commit cea7d96

Browse files
committed
Implement Partial{,Eq} for JoinHandle & os::Thread
1 parent 01fd4d6 commit cea7d96

File tree

5 files changed

+43
-1
lines changed

5 files changed

+43
-1
lines changed

src/liblibc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7029,6 +7029,7 @@ pub mod funcs {
70297029
dwOptions: DWORD)
70307030
-> BOOL;
70317031
pub fn CloseHandle(hObject: HANDLE) -> BOOL;
7032+
pub fn CompareObjectHandles(h1: HANDLE, h2: HANDLE) -> BOOL;
70327033
pub fn OpenProcess(dwDesiredAccess: DWORD,
70337034
bInheritHandle: BOOL,
70347035
dwProcessId: DWORD)

src/libstd/sys/unix/thread.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use time::Duration;
2626

2727
use sys_common::thread::*;
2828

29+
#[derive(Eq)]
2930
pub struct Thread {
3031
id: libc::pthread_t,
3132
}
@@ -169,6 +170,14 @@ impl Thread {
169170
}
170171
}
171172

173+
impl PartialEq for Thread {
174+
fn eq(&self, other: &Self) -> bool {
175+
unsafe {
176+
pthread_equal(self.id, other.id) != 0
177+
}
178+
}
179+
}
180+
172181
impl Drop for Thread {
173182
fn drop(&mut self) {
174183
let ret = unsafe { pthread_detach(self.id) };
@@ -403,6 +412,7 @@ extern {
403412
value: *mut libc::c_void) -> libc::c_int;
404413
fn pthread_join(native: libc::pthread_t,
405414
value: *mut *mut libc::c_void) -> libc::c_int;
415+
fn pthread_equal(t1: libc::pthread_t, t2: libc::pthread_t) -> libc::c_int;
406416
fn pthread_attr_init(attr: *mut libc::pthread_attr_t) -> libc::c_int;
407417
fn pthread_attr_destroy(attr: *mut libc::pthread_attr_t) -> libc::c_int;
408418
fn pthread_attr_setstacksize(attr: *mut libc::pthread_attr_t,

src/libstd/sys/windows/handle.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,15 @@ use sys::cvt;
2020
/// An owned container for `HANDLE` object, closing them on Drop.
2121
///
2222
/// All methods are inherited through a `Deref` impl to `RawHandle`
23+
#[derive(PartialEq, Eq)]
2324
pub struct Handle(RawHandle);
2425

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

3334
unsafe impl Send for RawHandle {}
@@ -106,3 +107,11 @@ impl RawHandle {
106107
Ok(Handle::new(ret))
107108
}
108109
}
110+
111+
impl PartialEq for RawHandle {
112+
fn eq(&self, other: &Self) -> bool {
113+
unsafe {
114+
libc::CompareObjectHandles(self.0, other.0) != libc::FALSE
115+
}
116+
}
117+
}

src/libstd/sys/windows/thread.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use sys::handle::Handle;
2020
use sys_common::thread::*;
2121
use time::Duration;
2222

23+
#[derive(PartialEq, Eq)]
2324
pub struct Thread {
2425
handle: Handle
2526
}

src/libstd/thread/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,13 +590,22 @@ impl<T> JoinInner<T> {
590590
}
591591
}
592592

593+
impl<T> PartialEq for JoinInner<T> {
594+
fn eq(&self, other: &Self) -> bool {
595+
self.native == other.native
596+
}
597+
}
598+
599+
impl<T> Eq for JoinInner<T> {}
600+
593601
/// An owned permission to join on a thread (block on its termination).
594602
///
595603
/// A `JoinHandle` *detaches* the child thread when it is dropped.
596604
///
597605
/// Due to platform restrictions, it is not possible to `Clone` this
598606
/// handle: the ability to join a child thread is a uniquely-owned
599607
/// permission.
608+
#[derive(Eq, PartialEq)]
600609
#[stable(feature = "rust1", since = "1.0.0")]
601610
pub struct JoinHandle<T>(JoinInner<T>);
602611

@@ -633,6 +642,7 @@ mod tests {
633642

634643
use any::Any;
635644
use sync::mpsc::{channel, Sender};
645+
use sync::{Barrier, Arc};
636646
use result;
637647
use super::{Builder};
638648
use thread;
@@ -665,6 +675,17 @@ mod tests {
665675
rx.recv().unwrap();
666676
}
667677

678+
#[test]
679+
fn test_thread_guard_equality() {
680+
let barrier = Arc::new(Barrier::new(2));
681+
let b = barrier.clone();
682+
let h = thread::spawn(move|| {
683+
b.wait();
684+
});
685+
assert!(h == h);
686+
barrier.wait();
687+
}
688+
668689
#[test]
669690
fn test_join_panic() {
670691
match thread::spawn(move|| {

0 commit comments

Comments
 (0)