Skip to content

Commit b44fd1a

Browse files
authored
FreeBSD: add pthread_getthreadid_np() (#2725)
* FreeBSD: add implementation for process::pthread_getthreadid_np() * FreeBSD: enable unistd::pthread_getthreadid_np() test
1 parent 06bb1be commit b44fd1a

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

changelog/2725.added.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added process::pthread_getthreadid_np() on FreeBSD.

src/unistd.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,14 @@ pub fn getpgrp() -> Pid {
403403
pub fn gettid() -> Pid {
404404
Pid(unsafe { libc::syscall(libc::SYS_gettid) as pid_t })
405405
}
406+
407+
/// Get the caller's thread ID (see
408+
/// [pthread_getthreadid_np(3)](https://man.freebsd.org/cgi/man.cgi?query=pthread_getthreadid_np&sektion=3&manpath=FreeBSD+15.0-RELEASE+and+Ports).
409+
#[cfg(target_os = "freebsd")]
410+
#[inline]
411+
pub fn pthread_getthreadid_np() -> Pid {
412+
Pid(unsafe { libc::pthread_getthreadid_np() as pid_t })
413+
}
406414
}
407415

408416
feature! {

test/test_unistd.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,19 @@ mod linux_android {
260260
}
261261
}
262262

263+
#[cfg(target_os = "freebsd")]
264+
mod freebsd {
265+
use nix::unistd::pthread_getthreadid_np;
266+
267+
#[test]
268+
fn test_pthread_getthreadid_np() {
269+
let tid: ::libc::pid_t = pthread_getthreadid_np().into();
270+
// FreeBSD has thread id namespace shared with pids, the split
271+
// is at PID_MAX = 99999.
272+
assert!(tid >= 100000);
273+
}
274+
}
275+
263276
#[test]
264277
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms
265278
#[cfg(not(any(

0 commit comments

Comments
 (0)