improve thread-id check for thread local FLS callbacks on Windows with static linking

This commit is contained in:
daan 2020-05-05 10:10:46 -07:00
parent 7c24edfeb0
commit a278db940b

View File

@ -216,26 +216,24 @@ static bool _mi_heap_done(mi_heap_t* heap) {
heap = heap->tld->heap_backing; heap = heap->tld->heap_backing;
if (!mi_heap_is_initialized(heap)) return false; if (!mi_heap_is_initialized(heap)) return false;
// check thread-id as on Windows shutdown with FLS the main (exit) thread may call this on thread-local heaps // delete all non-backing heaps in this thread
if (heap->thread_id == _mi_thread_id()) { mi_heap_t* curr = heap->tld->heaps;
// delete all non-backing heaps in this thread while (curr != NULL) {
mi_heap_t* curr = heap->tld->heaps; mi_heap_t* next = curr->next; // save `next` as `curr` will be freed
while (curr != NULL) { if (curr != heap) {
mi_heap_t* next = curr->next; // save `next` as `curr` will be freed mi_assert_internal(!mi_heap_is_backing(curr));
if (curr != heap) { mi_heap_delete(curr);
mi_assert_internal(!mi_heap_is_backing(curr));
mi_heap_delete(curr);
}
curr = next;
}
mi_assert_internal(heap->tld->heaps == heap && heap->next == NULL);
mi_assert_internal(mi_heap_is_backing(heap));
// collect if not the main thread
if (heap != &_mi_heap_main) {
_mi_heap_collect_abandon(heap);
} }
curr = next;
} }
mi_assert_internal(heap->tld->heaps == heap && heap->next == NULL);
mi_assert_internal(mi_heap_is_backing(heap));
// collect if not the main thread
if (heap != &_mi_heap_main) {
_mi_heap_collect_abandon(heap);
}
// merge stats // merge stats
_mi_stats_done(&heap->tld->stats); _mi_stats_done(&heap->tld->stats);
@ -296,10 +294,6 @@ static void _mi_thread_done(mi_heap_t* default_heap);
#endif #endif
static DWORD mi_fls_key = (DWORD)(-1); static DWORD mi_fls_key = (DWORD)(-1);
static void NTAPI mi_fls_done(PVOID value) { static void NTAPI mi_fls_done(PVOID value) {
if (mi_fls_key != -1 && _mi_is_main_thread()) {
FlsSetValue(mi_fls_key, NULL); // null out once to prevent recursion on the main thread
mi_fls_key = -1;
}
if (value!=NULL) _mi_thread_done((mi_heap_t*)value); if (value!=NULL) _mi_thread_done((mi_heap_t*)value);
} }
#elif defined(MI_USE_PTHREADS) #elif defined(MI_USE_PTHREADS)
@ -361,12 +355,16 @@ void mi_thread_done(void) mi_attr_noexcept {
} }
static void _mi_thread_done(mi_heap_t* heap) { static void _mi_thread_done(mi_heap_t* heap) {
// check thread-id as on Windows shutdown with FLS the main (exit) thread may call this on thread-local heaps...
if (heap->thread_id != _mi_thread_id()) return;
// stats // stats
if (!_mi_is_main_thread() && mi_heap_is_initialized(heap)) { if (!_mi_is_main_thread() && mi_heap_is_initialized(heap)) {
_mi_stat_decrease(&heap->tld->stats.threads, 1); _mi_stat_decrease(&heap->tld->stats.threads, 1);
} }
// abandon the thread local heap // abandon the thread local heap
if (_mi_heap_done(heap)) return; // returns true if already ran if (_mi_heap_done(heap)) return; // returns true if already ran
} }
void _mi_heap_set_default_direct(mi_heap_t* heap) { void _mi_heap_set_default_direct(mi_heap_t* heap) {