From 32b3d2d343ef39c49f9d93ce716b5ec1efb8557b Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Mon, 10 Nov 2025 15:40:34 -0700 Subject: [PATCH] Fix pthread key collision --- src/header/pthread/tls.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/header/pthread/tls.rs b/src/header/pthread/tls.rs index 11c0acd279..87faa5bcd0 100644 --- a/src/header/pthread/tls.rs +++ b/src/header/pthread/tls.rs @@ -3,7 +3,7 @@ use super::*; // TODO: Hashmap? use alloc::collections::BTreeMap; -use core::cell::{Cell, RefCell}; +use core::{cell::{Cell, RefCell}, sync::atomic::{AtomicUsize, Ordering}}; use crate::{header::errno::EINVAL, sync::Mutex}; @@ -33,9 +33,8 @@ pub unsafe extern "C" fn pthread_key_create( key_ptr: *mut pthread_key_t, destructor: Option, ) -> c_int { - let key = NEXTKEY.get(); - NEXTKEY.set(key + 1); - //println!("pthread_key_create new key {:#0x}, dtor {:p}", key, destructor); + let key = NEXTKEY.fetch_add(1, Ordering::SeqCst) as pthread_key_t; + //println!("pthread_key_create new key {:#0x}, dtor {:?}", key, destructor); // TODO //if key >= PTHREAD_KEYS_MAX { @@ -99,8 +98,7 @@ struct Record { data: *mut c_void, } -#[thread_local] -static NEXTKEY: Cell = Cell::new(1); +static NEXTKEY: AtomicUsize = AtomicUsize::new(1); pub(crate) unsafe fn run_all_destructors() { for (key, Record { data }) in VALUES.take() {