use libc;
#[cfg(any(target_os = "macos", target_os = "ios"))]
#[inline]
pub fn current_tid() -> Result<u64, libc::c_int> {
#[link(name = "pthread")]
extern "C" {
fn pthread_threadid_np(thread: libc::pthread_t, thread_id: *mut u64) -> libc::c_int;
}
unsafe {
let mut tid = 0;
let err = pthread_threadid_np(0, &mut tid);
match err {
0 => Ok(tid),
_ => Err(err),
}
}
}
#[cfg(target_os = "fuchsia")]
#[inline]
pub fn current_tid() -> Result<u64, libc::c_int> {
Ok(0)
}
#[cfg(any(target_os = "linux", target_os = "android"))]
#[inline]
pub fn current_tid() -> Result<u64, libc::c_int> {
unsafe { Ok(libc::syscall(libc::SYS_gettid) as u64) }
}
#[cfg(all(
target_family = "unix",
not(any(
target_os = "macos",
target_os = "ios",
target_os = "linux",
target_os = "android",
target_os = "fuchsia"
))
))]
pub fn current_tid() -> Result<u64, libc::c_int> {
unsafe { Ok(libc::pthread_self() as u64) }
}
#[cfg(target_os = "windows")]
#[inline]
pub fn current_tid() -> Result<u64, libc::c_int> {
extern "C" {
fn GetCurrentThreadId() -> libc::c_ulong;
}
unsafe { Ok(u64::from(GetCurrentThreadId())) }
}