Skip to content

Commit ff5c726

Browse files
committed
[OpenMP] Fix a wrong assertion in __kmp_get_global_thread_id
The function assumes that `__kmp_gtid_get_specific` always returns a valid gtid. That is not always true, because when creating the key for thread-specific data, a destructor is assigned. The dtor will be called at thread exit. However, before the dtor is called, the thread-specific data will be reset to NULL first (https://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html): > At thread exit, if a key value has a non-NULL destructor pointer, and the thread > has a non-NULL value associated with that key, the value of the key is set to NULL. This will lead to that `__kmp_gtid_get_specific` returns `KMP_GTID_DNE`. Reviewed By: jdoerfert Differential Revision: https://reviews.llvm.org/D159369
1 parent 518b08c commit ff5c726

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

openmp/runtime/src/kmp_runtime.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,12 @@ int __kmp_get_global_thread_id() {
178178
if (stack_diff <= stack_size) {
179179
/* The only way we can be closer than the allocated */
180180
/* stack size is if we are running on this thread. */
181-
KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == i);
181+
// __kmp_gtid_get_specific can return negative value because this
182+
// function can be called by thread destructor. However, before the
183+
// thread destructor is called, the value of the corresponding
184+
// thread-specific data will be reset to NULL.
185+
KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() < 0 ||
186+
__kmp_gtid_get_specific() == i);
182187
return i;
183188
}
184189
}

0 commit comments

Comments
 (0)