Skip to content

Commit 6ab244c

Browse files
authored
Implement Eq and PartialEq for Thread. (#103)
And add a guarantee that the raw value returned by `Thread::to_raw` etc. uniquely identifies a thread.
1 parent 6b8c32f commit 6ab244c

2 files changed

Lines changed: 31 additions & 6 deletions

File tree

src/thread/libc.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,44 @@ extern "C" {
2525
///
2626
/// This type does not detach or free resources on drop. It just leaks the
2727
/// thread. To detach or join, call [`detach`] or [`join`] explicitly.
28-
#[derive(Copy, Clone)]
28+
// In this library, we assume `libc::pthread` can be casted to and from a
29+
// `usize` and directly compared for equality (without using `pthread_equal`).
30+
// POSIX says that `pthread_t` can be a struct type; if any platform ever
31+
// does that, the code below won't compile, and we'll have to figure out what
32+
// to do about it.
33+
#[derive(Copy, Clone, Eq, PartialEq)]
2934
pub struct Thread(libc::pthread_t);
3035

3136
impl Thread {
32-
/// Convert to `Self` from a raw pointer.
37+
/// Convert to `Self` from a raw pointer that was returned from
38+
/// `Thread::to_raw`.
3339
#[inline]
3440
pub fn from_raw(raw: *mut c_void) -> Self {
3541
Self(raw.expose_addr() as libc::pthread_t)
3642
}
3743

38-
/// Convert to `Self` from a raw non-null pointer.
44+
/// Convert to `Self` from a raw non-null pointer that was returned from
45+
/// `Thread::to_raw_non_null`.
3946
#[inline]
4047
pub fn from_raw_non_null(raw: NonNull<c_void>) -> Self {
4148
Self::from_raw(raw.as_ptr())
4249
}
4350

4451
/// Convert to a raw pointer from a `Self`.
52+
///
53+
/// This value is guaranteed to uniquely identify a thread, while it is
54+
/// running. After a thread has exited, this value may be reused by new
55+
/// threads.
4556
#[inline]
4657
pub fn to_raw(self) -> *mut c_void {
4758
from_exposed_addr_mut(self.0 as usize)
4859
}
4960

5061
/// Convert to a raw non-null pointer from a `Self`.
62+
///
63+
/// This value is guaranteed to uniquely identify a thread, while it is
64+
/// running. After a thread has exited, this value may be reused by new
65+
/// threads.
5166
#[inline]
5267
pub fn to_raw_non_null(self) -> NonNull<c_void> {
5368
NonNull::new(self.to_raw()).unwrap()

src/thread/linux_raw.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,39 @@ use rustix::thread::gettid;
3131
///
3232
/// This type does not detach or free resources on drop. It just leaks the
3333
/// thread. To detach or join, call [`detach`] or [`join`] explicitly.
34-
#[derive(Copy, Clone)]
34+
#[derive(Copy, Clone, Eq, PartialEq)]
3535
pub struct Thread(NonNull<ThreadData>);
3636

3737
impl Thread {
38-
/// Convert to `Self` from a raw pointer.
38+
/// Convert to `Self` from a raw pointer that was returned from
39+
/// `Thread::to_raw`.
3940
#[inline]
4041
pub fn from_raw(raw: *mut c_void) -> Self {
4142
Self(NonNull::new(raw.cast()).unwrap())
4243
}
4344

44-
/// Convert to `Self` from a raw non-null pointer.
45+
/// Convert to `Self` from a raw non-null pointer that was returned from
46+
/// `Thread::to_raw_non_null`.
4547
#[inline]
4648
pub fn from_raw_non_null(raw: NonNull<c_void>) -> Self {
4749
Self(raw.cast())
4850
}
4951

5052
/// Convert to a raw pointer from a `Self`.
53+
///
54+
/// This value is guaranteed to uniquely identify a thread, while it is
55+
/// running. After a thread has exited, this value may be reused by new
56+
/// threads.
5157
#[inline]
5258
pub fn to_raw(self) -> *mut c_void {
5359
self.0.cast().as_ptr()
5460
}
5561

5662
/// Convert to a raw non-null pointer from a `Self`.
63+
///
64+
/// This value is guaranteed to uniquely identify a thread, while it is
65+
/// running. After a thread has exited, this value may be reused by new
66+
/// threads.
5767
#[inline]
5868
pub fn to_raw_non_null(self) -> NonNull<c_void> {
5969
self.0.cast()

0 commit comments

Comments
 (0)