From ca752127ed614ee2003fda24dab1a36c28f6a800 Mon Sep 17 00:00:00 2001
From: Dave Hylands <dhylands@gmail.com>
Date: Fri, 4 Mar 2016 23:50:51 -0800
Subject: [PATCH] Add gettid

---
 Cargo.toml          |  2 +-
 src/unistd.rs       |  6 ++++++
 test/test_unistd.rs | 11 +++++++++++
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/Cargo.toml b/Cargo.toml
index 8beb203508..9db0c1df48 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -20,7 +20,7 @@ preadv_pwritev = []
 signalfd = []
 
 [dependencies]
-libc     = "0.2.7"
+libc     = "0.2.8"
 bitflags = "0.4"
 cfg-if = "0.1.0"
 
diff --git a/src/unistd.rs b/src/unistd.rs
index 193086df38..5d57afd1b8 100644
--- a/src/unistd.rs
+++ b/src/unistd.rs
@@ -56,6 +56,12 @@ pub fn setpgid(pid: pid_t, pgid: pid_t) -> Result<()> {
     Errno::result(res).map(drop)
 }
 
+#[cfg(any(target_os = "linux", target_os = "android"))]
+#[inline]
+pub fn gettid() -> pid_t {
+    unsafe { libc::syscall(libc::SYS_gettid) as pid_t }    // no error handling, according to man page: "These functions are always successful."
+}
+
 #[inline]
 pub fn dup(oldfd: RawFd) -> Result<RawFd> {
     let res = unsafe { libc::dup(oldfd) };
diff --git a/test/test_unistd.rs b/test/test_unistd.rs
index 510f8f6c3b..2f361cfe56 100644
--- a/test/test_unistd.rs
+++ b/test/test_unistd.rs
@@ -54,6 +54,17 @@ fn test_getpid() {
     assert!(ppid > 0);
 }
 
+#[cfg(any(target_os = "linux", target_os = "android"))]
+mod linux_android {
+    use nix::unistd::gettid;
+
+    #[test]
+    fn test_gettid() {
+        let tid = gettid();
+        assert!(tid > 0);
+    }
+}
+
 macro_rules! execve_test_factory(
     ($test_name:ident, $syscall:ident, $unix_sh:expr, $android_sh:expr) => (
     #[test]