diff --git a/src/sys/mod.rs b/src/sys/mod.rs index 7675f94448..6b41f58dae 100644 --- a/src/sys/mod.rs +++ b/src/sys/mod.rs @@ -84,3 +84,6 @@ pub mod statfs; target_arch = "arm")), )] pub mod statvfs; + +#[cfg(unix)] +pub mod pthread; diff --git a/src/sys/pthread.rs b/src/sys/pthread.rs new file mode 100644 index 0000000000..5ad1e9da42 --- /dev/null +++ b/src/sys/pthread.rs @@ -0,0 +1,12 @@ +use libc::{self, pid_t}; + + +/// Obtain ID of the calling thread (see +/// [pthread_self(3)](http://man7.org/linux/man-pages/man3/pthread_self.3.html) +/// +/// The thread ID returned by pthread_self() is not the same thing as +/// the kernel thread ID returned by a call to gettid(2). +#[inline] +pub fn pthread_self() -> pid_t { + unsafe { libc::pthread_self() as pid_t } +} diff --git a/test/sys/mod.rs b/test/sys/mod.rs index 5e5eed4197..9b3e95d783 100644 --- a/test/sys/mod.rs +++ b/test/sys/mod.rs @@ -12,3 +12,5 @@ mod test_uio; #[cfg(target_os = "linux")] mod test_epoll; +#[cfg(unix)] +mod test_pthread; diff --git a/test/sys/test_pthread.rs b/test/sys/test_pthread.rs new file mode 100644 index 0000000000..8ae3032e05 --- /dev/null +++ b/test/sys/test_pthread.rs @@ -0,0 +1,6 @@ +use nix::sys::pthread::*; +#[test] +fn test_pthread_self() { + let tid = pthread_self(); + assert!(tid > 0); +}