Skip to content

FreeBSD: add pthread_getthreadid_np()#2725

Merged
SteveLauC merged 2 commits intonix-rust:masterfrom
kostikbel:freebsd
Jan 25, 2026
Merged

FreeBSD: add pthread_getthreadid_np()#2725
SteveLauC merged 2 commits intonix-rust:masterfrom
kostikbel:freebsd

Conversation

@kostikbel
Copy link
Contributor

What does this PR do

The process::gettid() is missing on FreeBSD.
The PR adds its implementation.

The implementation returns the real tid of the caller's thread, it does
not try to mimic the Linux behavior where initial (main) thread id is
same as the processs id. This might be patched if needed, but pids and
tids are in different namespace on FreeBSD, so better keep it as is
unless there is a pressure.

Checklist:

  • I have read CONTRIBUTING.md
  • I have written necessary tests and rustdoc comments
  • A change log has been added if this PR modifies nix's API

@kostikbel kostikbel marked this pull request as ready for review January 18, 2026 16:01
Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

src/unistd.rs Outdated
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
#[cfg(freebsd)]
#[cfg(target_os = "freebsd")]

We only use the alias when it contains more than 2 OSes

/// 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(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

src/unistd.rs Outdated
Copy link
Member

Choose a reason for hiding this comment

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

Open question, should we call it gettid(), i.e., follow the Linux convention here, or we should use its own name?

I am slightly inclined to use its original name as Nix, in most cases, only exposes system stuff in Rusty way, we do not wrap them

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added it as gettid() because this is what some consumers need, that depend on nix with features = ["process"]. Using pthread_gettreadid_np() there would not allow e.g. perfetto-recorder to compile.

Copy link
Member

Choose a reason for hiding this comment

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

Using pthread_gettreadid_np() there would not allow e.g. perfetto-recorder to compile.

Well, this is unfortunate. But Nix does not provide cross-platform wrappers. Typically, they will be provided by crates that depend on Nix. So I still think we should use the original name: pthread_getthreadid_np :>

Copy link
Member

Choose a reason for hiding this comment

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

I agree. In the past Nix tried to provide a measure of portability. But it didn't work well. It's better when concerns are separated: Nix provides Rusty interfaces to OS APIs, and other crates atop Nix provide cross-platform abstractions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without this specific name, the patch is not very useful. The delivery path become this, then wait for next nix release, then push the change to that crate, then wait for its release.

Copy link
Member

Choose a reason for hiding this comment

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

Well, I can make a release right now, if you need one.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not that I need one, but it would be nice to have, some day.

@kostikbel kostikbel force-pushed the freebsd branch 2 times, most recently from c865442 to 5ff9695 Compare January 23, 2026 04:25
@kostikbel
Copy link
Contributor Author

Thanks for the PR. A changelog entry is needed, BTW, see https://github.com/nix-rust/nix/blob/master/CONTRIBUTING.md#pull-requests

All done, thanks for the review.

Copy link
Member

@SteveLauC SteveLauC left a comment

Choose a reason for hiding this comment

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

Thanks!

@SteveLauC SteveLauC changed the title FreeBSD: add implementation for process::gettid() FreeBSD: add pthread_gettreadid_np() Jan 25, 2026
@SteveLauC SteveLauC changed the title FreeBSD: add pthread_gettreadid_np() FreeBSD: add pthread_getthreadid_np() Jan 25, 2026
@SteveLauC SteveLauC added this pull request to the merge queue Jan 25, 2026
Merged via the queue into nix-rust:master with commit b44fd1a Jan 25, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants

Comments