Skip to content

Commit

Permalink
Add gettid
Browse files Browse the repository at this point in the history
  • Loading branch information
dhylands committed Mar 5, 2016
1 parent 9f590fb commit 8264a61
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ signalfd = []
[dependencies]
libc = "0.2.7"
bitflags = "0.4"
c-nix = { path = "c-nix", version = "0.1" }

[dev-dependencies]
rand = "0.3.8"
tempdir = "0.3"
tempfile = "2"
nix-test = { path = "nix-test" }
c-nix = { path = "c-nix", version = "0.1" }

[[test]]
name = "test"
Expand Down
11 changes: 11 additions & 0 deletions c-nix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "c-nix"
version = "0.1.0"
authors = ["Dave Hylands <dhylands@gmail.com>"]
build = "build.rs"

[dependencies]
libc = "0.2.7"

[build-dependencies]
gcc = "0.3.8"
10 changes: 10 additions & 0 deletions c-nix/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
extern crate gcc;

fn main() {
let mut c = gcc::Config::new();
let config = c.file("c-src/gettid.c")
.cpp(false);

config.compile("libc-nix.a");
println!("cargo:rustc-flags=-l c-nix");
}
12 changes: 12 additions & 0 deletions c-nix/c-src/gettid.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <sys/syscall.h>

// SYS_gettid has different values on different platforms, so we let the
// C preprocessor figure things out for us.

int c_gettid(void) {
#if defined(__APPLE__)
return syscall(SYS_thread_selfid);
#else
return syscall(SYS_gettid);
#endif
}
17 changes: 17 additions & 0 deletions c-nix/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
extern crate libc;

extern {
pub fn c_gettid() -> libc::pid_t;
}

pub fn gettid() -> libc::pid_t {
unsafe { c_gettid() }
}

#[cfg(test)]
mod test {
#[test]
fn it_works() {
println!("gettid() = {}", gettid());
}
}
1 change: 1 addition & 0 deletions nix-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license = "MIT"

[dependencies]
libc = "*"
c-nix = { path = "../c-nix", version = "0.1" }

[build-dependencies]
gcc = "0.3.8"
1 change: 1 addition & 0 deletions nix-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod ffi {
use libc::{c_int, c_char, size_t};

#[link(name = "nixtest", kind = "static")]
#[link(name = "c-nix", kind = "static")]
extern {
pub fn get_int_const(errno: *const c_char) -> c_int;
pub fn size_of(ty: *const c_char) -> size_t;
Expand Down
8 changes: 8 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,14 @@ pub fn setpgid(pid: pid_t, pgid: pid_t) -> Result<()> {
Errno::result(res).map(drop)
}

extern {
pub fn c_gettid() -> pid_t;
}
#[inline]
pub fn gettid() -> pid_t {
unsafe { c_gettid() } // no error handling, according to man page: "These functions are always successful."
}

#[inline]
pub fn dup(oldfd: RawFd) -> Result<RawFd> {
let res = unsafe { ffi::dup(oldfd) };
Expand Down
2 changes: 2 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ fn test_wait() {
fn test_getpid() {
let pid = getpid();
let ppid = getppid();
let tid = gettid();
assert!(pid > 0);
assert!(ppid > 0);
assert!(tid > 0);
}

macro_rules! execve_test_factory(
Expand Down

0 comments on commit 8264a61

Please sign in to comment.