Skip to content

Commit e3f4492

Browse files
committed
fix theaps reference count error with pthreads
1 parent 8c356f4 commit e3f4492

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

include/mimalloc/internal.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ void __mi_stat_counter_increase_mt(mi_stat_counter_t* stat, size_t amount);
423423
#define MI_PTHREADS_GET_INVALID_KEY_IS_NULL 1
424424
#endif
425425

426-
mi_decl_noinline bool _mi_pthread_key_create(pthread_key_t* pkey, void* init);
426+
mi_decl_noinline bool _mi_pthread_key_create(pthread_key_t* pkey, void (*destruct)(void*), void* init);
427427

428428
static inline void* mi_pthread_key_get(pthread_key_t key) {
429429
#if !MI_PTHREADS_GET_INVALID_KEY_IS_NULL
@@ -434,7 +434,7 @@ static inline void* mi_pthread_key_get(pthread_key_t key) {
434434

435435
static inline bool mi_pthread_key_set(pthread_key_t* pkey, void* val) {
436436
if mi_likely(*pkey!=MI_PTHREAD_KEY_INVALID) { pthread_setspecific(*pkey,val); return true; }
437-
else if (val!=NULL) { return _mi_pthread_key_create(pkey,val); }
437+
else if (val!=NULL) { return _mi_pthread_key_create(pkey,NULL,val); }
438438
else return true;
439439
}
440440

src/arena.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,7 @@ static bool mi_heap_delete_page(const mi_heap_t* heap, const mi_heap_area_t* are
24442444
}
24452445

24462446
static void mi_heap_delete_pages(mi_heap_t* heap, mi_heap_t* heap_target) {
2447-
mi_theap_t* const theap_target = (heap_target != NULL ? _mi_heap_theap(heap_target) : NULL);
2447+
mi_theap_t* const theap_target = (heap_target != NULL ? _mi_heap_theap_peek(heap_target) : NULL);
24482448
// mi_theap_t* const theap = _mi_heap_theap(heap);
24492449
mi_heap_delete_visit_info_t info = { heap_target, theap_target, NULL };
24502450
_mi_heap_visit_blocks(heap, false, false, &mi_heap_delete_page, &info);

src/init.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,10 +916,17 @@ static void mi_win_tls_slot_set(size_t slot, size_t extended_slot, void* value)
916916
mi_decl_hidden pthread_key_t _mi_theap_default_key = MI_PTHREAD_KEY_INVALID;
917917
mi_decl_hidden pthread_key_t _mi_theap_cached_key = MI_PTHREAD_KEY_INVALID;
918918

919+
static void mi_theap_cached_key_destroy(void* theapv) {
920+
mi_theap_t* theap = (mi_theap_t*)theapv;
921+
if (theap!=NULL) {
922+
_mi_theap_decref(theap);
923+
}
924+
}
925+
919926
static void mi_tls_slots_init(void) {
920927
mi_atomic_do_once {
921-
_mi_pthread_key_create(&_mi_theap_default_key,NULL);
922-
_mi_pthread_key_create(&_mi_theap_cached_key,NULL);
928+
_mi_pthread_key_create(&_mi_theap_default_key,NULL,NULL);
929+
_mi_pthread_key_create(&_mi_theap_cached_key,&mi_theap_cached_key_destroy,NULL);
923930
}
924931
}
925932

@@ -1013,6 +1020,7 @@ void _mi_auto_process_init(void) {
10131020
mi_assert_internal(_mi_is_main_thread());
10141021

10151022
mi_process_init();
1023+
mi_tls_slots_init();
10161024
mi_process_setup_auto_thread_done();
10171025
_mi_thread_locals_init();
10181026

src/libc.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ void _mi_atomic_once_release(mi_atomic_once_t* once) {
140140
}
141141

142142
#if MI_USE_PTHREADS
143-
mi_decl_noinline bool _mi_pthread_key_create(pthread_key_t* pkey, void* init) {
144-
int err = pthread_key_create(pkey,NULL);
143+
mi_decl_noinline bool _mi_pthread_key_create(pthread_key_t* pkey, void (*destruct)(void*), void* init) {
144+
int err = pthread_key_create(pkey,destruct);
145145
if mi_unlikely(err!=0) {
146146
*pkey = MI_PTHREAD_KEY_INVALID;
147147
_mi_error_message(ENOMEM,"unable to allocate a thread local variable (error %d)\n", err);

0 commit comments

Comments
 (0)