Fix pthread key collision

This commit is contained in:
Jeremy Soller
2025-11-10 15:40:34 -07:00
parent 1c6bbf24af
commit 32b3d2d343
+4 -6
View File
@@ -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<extern "C" fn(value: *mut c_void)>,
) -> 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<pthread_key_t> = Cell::new(1);
static NEXTKEY: AtomicUsize = AtomicUsize::new(1);
pub(crate) unsafe fn run_all_destructors() {
for (key, Record { data }) in VALUES.take() {