Skip to content

Cleanup TLS implementation #102656

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

Closed
wants to merge 1 commit into from
Closed
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
17 changes: 16 additions & 1 deletion library/std/src/sys/unix/thread_local_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,23 @@ pub type Key = libc::pthread_key_t;

#[inline]
pub unsafe fn create(dtor: Option<unsafe extern "C" fn(*mut u8)>) -> Key {
let dtor = mem::transmute(dtor);
Copy link
Member

Choose a reason for hiding this comment

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

Could we annotate the type on dtor explicitly?

let mut key = 0;
assert_eq!(libc::pthread_key_create(&mut key, mem::transmute(dtor)), 0);
assert_eq!(libc::pthread_key_create(&mut key, dtor), 0);
// POSIX allows the key created here to be 0, but `StaticKey` needs to
// use 0 as a sentinel value to check who won the race to set the shared
// TLS key. Therefore, we employ this small trick to avoid having to waste
// the key value.
if key == 0 {
let mut new = 0;
// Only check the results after the old key has been destroyed to avoid
// leaking it.
let r_c = libc::pthread_key_create(&mut new, dtor);
let r_d = libc::pthread_key_delete(key);
assert_eq!(r_c, 0);
debug_assert_eq!(r_d, 0);
key = new;
}
key
}

Expand Down
26 changes: 3 additions & 23 deletions library/std/src/sys_common/thread_local_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,6 @@ pub struct Key {
key: imp::Key,
}

/// Constant initialization value for static TLS keys.
///
/// This value specifies no destructor by default.
pub const INIT: StaticKey = StaticKey::new(None);

impl StaticKey {
#[rustc_const_unstable(feature = "thread_local_internals", issue = "none")]
pub const fn new(dtor: Option<unsafe extern "C" fn(*mut u8)>) -> StaticKey {
Expand Down Expand Up @@ -170,24 +165,9 @@ impl StaticKey {
return key;
}

// POSIX allows the key created here to be 0, but the compare_exchange
// below relies on using 0 as a sentinel value to check who won the
// race to set the shared TLS key. As far as I know, there is no
// guaranteed value that cannot be returned as a posix_key_create key,
// so there is no value we can initialize the inner key with to
// prove that it has not yet been set. As such, we'll continue using a
// value of 0, but with some gyrations to make sure we have a non-0
// value returned from the creation routine.
// FIXME: this is clearly a hack, and should be cleaned up.
let key1 = imp::create(self.dtor);
let key = if key1 != 0 {
key1
} else {
let key2 = imp::create(self.dtor);
imp::destroy(key1);
key2
};
rtassert!(key != 0);
let key = imp::create(self.dtor);
// Implementations have to ensure key values are not zero.
debug_assert_ne!(key, 0);
match self.key.compare_exchange(0, key as usize, Ordering::SeqCst, Ordering::SeqCst) {
// The CAS succeeded, so we've created the actual key
Ok(_) => key as usize,
Expand Down