1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[cfg(windows)]
pub type RawId = u32;
#[cfg(unix)]
pub type RawId = libc::pthread_t;
#[cfg(all(not(unix), not(windows)))]
pub type RawId = u8;
#[cfg(windows)]
#[inline]
pub fn get_raw_id() -> RawId {
extern "system" {
pub fn GetCurrentThreadId() -> RawId;
}
unsafe {
GetCurrentThreadId()
}
}
#[cfg(unix)]
#[inline]
pub fn get_raw_id() -> RawId {
unsafe {
libc::pthread_self()
}
}
#[cfg(all(not(unix), not(windows)))]
#[inline]
pub fn get_raw_id() -> RawId {
0
}
#[derive(Copy, Clone, Debug)]
pub struct ThreadId {
id: RawId,
}
impl ThreadId {
#[inline]
pub fn current() -> Self {
Self {
id: get_raw_id(),
}
}
#[inline]
pub const fn as_raw(&self) -> RawId {
self.id
}
}
impl core::cmp::PartialEq<ThreadId> for ThreadId {
#[cfg(not(unix))]
fn eq(&self, other: &ThreadId) -> bool {
self.id == other.id
}
#[cfg(unix)]
fn eq(&self, other: &ThreadId) -> bool {
extern "system" {
pub fn pthread_equal(left: RawId, right: RawId) -> libc::c_int;
}
unsafe {
pthread_equal(self.id, other.id) != 0
}
}
}
impl core::cmp::Eq for ThreadId {}
impl core::hash::Hash for ThreadId {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
}