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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#[cfg(all(unix, not(target_os = "linux"), not(target_os = "android"), not(target_os = "macos"), not(target_os = "ios"), not(target_os = "netbsd"), not(target_os = "freebsd")))]
pub type RawId = libc::pthread_t;
#[cfg(any(target_os = "linux", target_os = "android"))]
pub type RawId = libc::pid_t;
#[cfg(target_os = "freebsd")]
pub type RawId = libc::c_int;
#[cfg(target_os = "netbsd")]
pub type RawId = libc::c_uint;
#[cfg(any(target_os = "macos", target_os = "ios"))]
pub type RawId = u64;
#[cfg(target_os = "freebsd")]
#[inline]
pub fn get_raw_id() -> RawId {
#[link(name = "pthread")]
extern "C" {
fn pthread_getthreadid_np() -> libc::c_int;
}
unsafe { pthread_getthreadid_np() }
}
#[cfg(target_os = "netbsd")]
#[inline]
pub fn get_raw_id() -> RawId {
extern "C" {
fn _lwp_self() -> libc::c_uint;
}
unsafe { _lwp_self() }
}
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[inline]
pub fn get_raw_id() -> RawId {
#[link(name = "pthread")]
extern "C" {
fn pthread_threadid_np(thread: libc::pthread_t, thread_id: *mut u64) -> libc::c_int;
}
let mut tid: u64 = 0;
let err = unsafe { pthread_threadid_np(0, &mut tid) };
assert_eq!(err, 0);
tid
}
#[cfg(any(target_os = "android", target_os = "linux"))]
#[inline]
pub fn get_raw_id() -> RawId {
unsafe { libc::syscall(libc::SYS_gettid) as libc::pid_t }
}
#[cfg(all(unix, not(target_os = "linux"), not(target_os = "android"), not(target_os = "macos"), not(target_os = "ios"), not(target_os = "netbsd"), not(target_os = "freebsd")))]
#[inline]
pub fn get_raw_id() -> RawId {
unsafe {
libc::pthread_self()
}
}
#[inline(always)]
pub fn raw_thread_eq(left: RawId, right: RawId) -> bool {
#[cfg(any(target_os = "linux", target_os = "android", target_os = "macos", target_os = "ios", target_os = "netbsd", target_os = "freebsd"))]
{
left == right
}
#[cfg(all(not(target_os = "linux"), not(target_os = "android"), not(target_os = "macos"), not(target_os = "ios"), not(target_os = "netbsd"), not(target_os = "freebsd")))]
{
#[link(name = "pthread")]
extern "C" {
pub fn pthread_equal(left: RawId, right: RawId) -> libc::c_int;
}
unsafe {
pthread_equal(left, right) != 0
}
}
}
#[cfg(feature = "thread-name")]
pub fn get_current_thread_name() -> str_buf::StrBuf::<16> {
extern "C" {
pub fn pthread_getname_np(thread: libc::pthread_t, name: *mut i8, len: libc::size_t) -> libc::c_int;
}
let mut storage = [0u8; 16];
let result = unsafe {
pthread_getname_np(libc::pthread_self(), storage.as_mut_ptr() as _, storage.len() as _)
};
if result == 0 {
let slice = if let Some(null_idx) = storage.iter().position(|b| *b == b'\0') {
&storage[..null_idx]
} else {
&storage[..]
};
match core::str::from_utf8(slice) {
Ok(res) => return str_buf::StrBuf::from_str(res),
_ => (),
}
}
str_buf::StrBuf::new()
}