Skip to content

Commit 8264a61

Browse files
committed
Add gettid
1 parent 9f590fb commit 8264a61

File tree

9 files changed

+64
-0
lines changed

9 files changed

+64
-0
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,14 @@ signalfd = []
2222
[dependencies]
2323
libc = "0.2.7"
2424
bitflags = "0.4"
25+
c-nix = { path = "c-nix", version = "0.1" }
2526

2627
[dev-dependencies]
2728
rand = "0.3.8"
2829
tempdir = "0.3"
2930
tempfile = "2"
3031
nix-test = { path = "nix-test" }
32+
c-nix = { path = "c-nix", version = "0.1" }
3133

3234
[[test]]
3335
name = "test"

c-nix/Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "c-nix"
3+
version = "0.1.0"
4+
authors = ["Dave Hylands <[email protected]>"]
5+
build = "build.rs"
6+
7+
[dependencies]
8+
libc = "0.2.7"
9+
10+
[build-dependencies]
11+
gcc = "0.3.8"

c-nix/build.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
extern crate gcc;
2+
3+
fn main() {
4+
let mut c = gcc::Config::new();
5+
let config = c.file("c-src/gettid.c")
6+
.cpp(false);
7+
8+
config.compile("libc-nix.a");
9+
println!("cargo:rustc-flags=-l c-nix");
10+
}

c-nix/c-src/gettid.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <sys/syscall.h>
2+
3+
// SYS_gettid has different values on different platforms, so we let the
4+
// C preprocessor figure things out for us.
5+
6+
int c_gettid(void) {
7+
#if defined(__APPLE__)
8+
return syscall(SYS_thread_selfid);
9+
#else
10+
return syscall(SYS_gettid);
11+
#endif
12+
}

c-nix/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
extern crate libc;
2+
3+
extern {
4+
pub fn c_gettid() -> libc::pid_t;
5+
}
6+
7+
pub fn gettid() -> libc::pid_t {
8+
unsafe { c_gettid() }
9+
}
10+
11+
#[cfg(test)]
12+
mod test {
13+
#[test]
14+
fn it_works() {
15+
println!("gettid() = {}", gettid());
16+
}
17+
}

nix-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ license = "MIT"
1010

1111
[dependencies]
1212
libc = "*"
13+
c-nix = { path = "../c-nix", version = "0.1" }
1314

1415
[build-dependencies]
1516
gcc = "0.3.8"

nix-test/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod ffi {
88
use libc::{c_int, c_char, size_t};
99

1010
#[link(name = "nixtest", kind = "static")]
11+
#[link(name = "c-nix", kind = "static")]
1112
extern {
1213
pub fn get_int_const(errno: *const c_char) -> c_int;
1314
pub fn size_of(ty: *const c_char) -> size_t;

src/unistd.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ pub fn setpgid(pid: pid_t, pgid: pid_t) -> Result<()> {
112112
Errno::result(res).map(drop)
113113
}
114114

115+
extern {
116+
pub fn c_gettid() -> pid_t;
117+
}
118+
#[inline]
119+
pub fn gettid() -> pid_t {
120+
unsafe { c_gettid() } // no error handling, according to man page: "These functions are always successful."
121+
}
122+
115123
#[inline]
116124
pub fn dup(oldfd: RawFd) -> Result<RawFd> {
117125
let res = unsafe { ffi::dup(oldfd) };

test/test_unistd.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ fn test_wait() {
5050
fn test_getpid() {
5151
let pid = getpid();
5252
let ppid = getppid();
53+
let tid = gettid();
5354
assert!(pid > 0);
5455
assert!(ppid > 0);
56+
assert!(tid > 0);
5557
}
5658

5759
macro_rules! execve_test_factory(

0 commit comments

Comments
 (0)