[Openmp-commits] [openmp] ff5c726 - [OpenMP] Fix a wrong assertion in `__kmp_get_global_thread_id`

Shilei Tian via Openmp-commits openmp-commits at lists.llvm.org
Wed Sep 6 09:22:06 PDT 2023


Author: Shilei Tian
Date: 2023-09-06T12:21:43-04:00
New Revision: ff5c7261ef01393acad207a10b6ea8fd579206ef

URL: https://github.com/llvm/llvm-project/commit/ff5c7261ef01393acad207a10b6ea8fd579206ef
DIFF: https://github.com/llvm/llvm-project/commit/ff5c7261ef01393acad207a10b6ea8fd579206ef.diff

LOG: [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

Added: 
    

Modified: 
    openmp/runtime/src/kmp_runtime.cpp

Removed: 
    


################################################################################
diff  --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp
index a1e36243f750c8..6a1ba97311dcc6 100644
--- a/openmp/runtime/src/kmp_runtime.cpp
+++ b/openmp/runtime/src/kmp_runtime.cpp
@@ -178,7 +178,12 @@ int __kmp_get_global_thread_id() {
       if (stack_
diff  <= stack_size) {
         /* The only way we can be closer than the allocated */
         /* stack size is if we are running on this thread. */
-        KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() == i);
+        // __kmp_gtid_get_specific can return negative value because this
+        // function can be called by thread destructor. However, before the
+        // thread destructor is called, the value of the corresponding
+        // thread-specific data will be reset to NULL.
+        KMP_DEBUG_ASSERT(__kmp_gtid_get_specific() < 0 ||
+                         __kmp_gtid_get_specific() == i);
         return i;
       }
     }


        


More information about the Openmp-commits mailing list