Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Used pthread name functions returning result for FreeBSD and DragonFly #132607

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

YohDeadfall
Copy link
Contributor

pthread_getname_np and pthread_setname_np received a wider adoption in past years and was added to:

There's not so much advantage except that the result can be checked in debug builds. Ideally it should be unified with Linux' implementation, but it trims the input.

@rustbot
Copy link
Collaborator

rustbot commented Nov 4, 2024

r? @ibraheemdev

rustbot has assigned @ibraheemdev.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Nov 4, 2024
@ibraheemdev
Copy link
Member

ibraheemdev commented Nov 7, 2024

Is there any change here except the debug assertion? I'm not sure I see the benefit of this change, will it fail on older versions of FreeBSD/DragonFly?

@YohDeadfall
Copy link
Contributor Author

While pthread_setname_np isn't well standardized (the np suggests that) and was supported only by GNU originally, there's a movement to make it more portable. NuttX supports it too, but it will take some time to land it in the libc crate.

Yes, it will fail on non-maintained versions of FreeBSD/DragonFly, but:

  • the oldest version FreeBSD maintains is 13, and it already has the function in it (source);
  • for DragonFlyBSD the function is included into 6.0.0 (source), and any prior versions are unmaintained.

I would unify the code between Linux GNU, DragonFlyBSD and FreeBSD, but there's a small implementation difference. The Linux kernel requires thread names being no longer than 16 bytes including a null terminator. Otherwise, it returns an error. Other platforms just truncate the provided name.

// Available since glibc 2.12, musl 1.1.16, and uClibc 1.0.20.
let name = truncate_cstr::<{ TASK_COMM_LEN }>(name);
let res = libc::pthread_setname_np(libc::pthread_self(), name.as_ptr());
// We have no good way of propagating errors here, but in debug-builds let's check that this actually worked.
debug_assert_eq!(res, 0);

@ibraheemdev
Copy link
Member

I see, but the only difference between the FreeBSD/DragonFly and OpenBSD/NuttX code is the debug assertion? So the idea here is to assert that the call is valid on FreeBSD and DragonFly?

@YohDeadfall
Copy link
Contributor Author

Yes, a bit of safety in debug builds instead of having muted errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-unix Operating system: Unix-like S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants