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

Make sentinel value configurable in library/std/src/sys_common/thread_local_key.rs #105359

Merged
merged 1 commit into from
Dec 29, 2022

Conversation

flba-eb
Copy link
Contributor

@flba-eb flba-eb commented Dec 6, 2022

This is an excerpt of a changeset for the QNX/Neutrino OS. To make the patch for QNX smaller and easier to review, I've extracted this change (which is OS independent). I would be surprised if no other OS is also affected.

All this patch does is to define a const for a sentinel value instead of using it directly at several places.

There are OSs that always return the lowest free value. The algorithm in lazy_init always avoids keys with the sentinel value.
In affected OSs, this means that each call to lazy_init will always request two keys from the OS and returns/frees the first one (with sentinel value) immediately afterwards.

By making the sentinel value configurable, affected OSs can use a different value than zero to prevent this performance issue.

On QNX/Neutrino, it is planned to use a different sentinel value:

// Define a sentinel value that is unlikely to be returned
// as a TLS key (but it may be returned).
#[cfg(not(target_os = "nto"))]
const KEY_SENTVAL: usize = 0;
// On QNX/Neutrino, 0 is always returned when currently not in use.
// Using 0 would mean to always create two keys and remote the first 
// one (with value of 0) immediately afterwards.
#[cfg(target_os = "nto")]
const KEY_SENTVAL: usize = libc::PTHREAD_KEYS_MAX + 1;

It seems like no other OS defines PTHREAD_KEYS_MAX in Rusts libc, but limits.h on unix systems does.

There are OSs that always return the lowest free value.
The algorithm in `lazy_init` always avoids keys with the
sentinel value.
In affected OSs, this means that each call to `lazy_init`
will always request two keys from the OS and returns/frees
the first one (with sentinel value) immediately afterwards.

By making the sentinel value configurable, affected OSs can
use a different value than zero to prevent this performance
issue.
@rustbot
Copy link
Collaborator

rustbot commented Dec 6, 2022

r? @m-ou-se

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added 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 Dec 6, 2022
@flba-eb flba-eb marked this pull request as ready for review December 6, 2022 09:41
@rustbot
Copy link
Collaborator

rustbot commented Dec 6, 2022

Hey! It looks like you've submitted a new PR for the library teams!

If this PR contains changes to any rust-lang/rust public library APIs then please comment with @rustbot label +T-libs-api -T-libs to tag it appropriately. If this PR contains changes to any unstable APIs please edit the PR description to add a link to the relevant API Change Proposal or create one if you haven't already. If you're unsure where your change falls no worries, just leave it as is and the reviewer will take a look and make a decision to forward on if necessary.

Examples of T-libs-api changes:

  • Stabilizing library features
  • Introducing insta-stable changes such as new implementations of existing stable traits on existing stable types
  • Introducing new or changing existing unstable library APIs (excluding permanently unstable features / features without a tracking issue)
  • Changing public documentation in ways that create new stability guarantees
  • Changing observable runtime behavior of library APIs

@m-ou-se
Copy link
Member

m-ou-se commented Dec 28, 2022

@bors r+

@bors
Copy link
Contributor

bors commented Dec 28, 2022

📌 Commit 980065a has been approved by m-ou-se

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 28, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Dec 28, 2022
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#104402 (Move `ReentrantMutex` to `std::sync`)
 - rust-lang#104493 (available_parallelism: Gracefully handle zero value cfs_period_us)
 - rust-lang#105359 (Make sentinel value configurable in `library/std/src/sys_common/thread_local_key.rs`)
 - rust-lang#105497 (Clarify `catch_unwind` docs about panic hooks)
 - rust-lang#105570 (Properly calculate best failure in macro matching)
 - rust-lang#105702 (Format only modified files)
 - rust-lang#105998 (adjust message on non-unwinding panic)
 - rust-lang#106161 (Iterator::find: link to Iterator::position in docs for discoverability)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 22060f2 into rust-lang:master Dec 29, 2022
@rustbot rustbot added this to the 1.68.0 milestone Dec 29, 2022
@flba-eb flba-eb deleted the thread_local_key_sentinel_value branch January 9, 2023 07:55
@workingjubilee workingjubilee added the O-neutrino OS: QNX Neutrino, a POSIX-compatible real-time operating system label Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
O-neutrino OS: QNX Neutrino, a POSIX-compatible real-time operating system S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

5 participants