Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/2725.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added process::pthread_getthreadid_np() on FreeBSD.
8 changes: 8 additions & 0 deletions src/unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,14 @@ pub fn getpgrp() -> Pid {
pub fn gettid() -> Pid {
Pid(unsafe { libc::syscall(libc::SYS_gettid) as pid_t })
}

/// Get the caller's thread ID (see
/// [pthread_getthreadid_np(3)](https://man.freebsd.org/cgi/man.cgi?query=pthread_getthreadid_np&sektion=3&manpath=FreeBSD+15.0-RELEASE+and+Ports).
#[cfg(target_os = "freebsd")]
#[inline]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[inline]
#[inline]
#[doc(alias = "pthread_getthreadid_np")]

Maybe we should add an alias 👀

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use its own name, this alias is not needed.

See the below comment

pub fn pthread_getthreadid_np() -> Pid {
Pid(unsafe { libc::pthread_getthreadid_np() as pid_t })
}
}

feature! {
Expand Down
13 changes: 13 additions & 0 deletions test/test_unistd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,19 @@ mod linux_android {
}
}

#[cfg(target_os = "freebsd")]
mod freebsd {
use nix::unistd::pthread_getthreadid_np;

#[test]
fn test_pthread_getthreadid_np() {
let tid: ::libc::pid_t = pthread_getthreadid_np().into();
// FreeBSD has thread id namespace shared with pids, the split
// is at PID_MAX = 99999.
assert!(tid >= 100000);
}
}

#[test]
// `getgroups()` and `setgroups()` do not behave as expected on Apple platforms
#[cfg(not(any(
Expand Down