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,8 +216,6 @@ static bool _mi_heap_done(mi_heap_t* heap) {
heap = heap->tld->heap_backing;
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
if (heap->thread_id == _mi_thread_id()) {
// delete all non-backing heaps in this thread
mi_heap_t* curr = heap->tld->heaps;
while (curr != NULL) {
@ -235,7 +233,7 @@ static bool _mi_heap_done(mi_heap_t* heap) {
if (heap != &_mi_heap_main) {
_mi_heap_collect_abandon(heap);
}
}
// merge stats
_mi_stats_done(&heap->tld->stats);
@ -296,10 +294,6 @@ static void _mi_thread_done(mi_heap_t* default_heap);
#endif
static DWORD mi_fls_key = (DWORD)(-1);
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);
}
#elif defined(MI_USE_PTHREADS)
@ -361,10 +355,14 @@ void mi_thread_done(void) mi_attr_noexcept {
}
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
if (!_mi_is_main_thread() && mi_heap_is_initialized(heap)) {
_mi_stat_decrease(&heap->tld->stats.threads, 1);
}
// abandon the thread local heap
if (_mi_heap_done(heap)) return; // returns true if already ran
}