[Openmp-commits] [PATCH] D121058: [OpenMP][FIX] Avoid races in the handling of to be deleted mapping entries

Johannes Doerfert via Phabricator via Openmp-commits openmp-commits at lists.llvm.org
Sat Mar 5 14:26:50 PST 2022


jdoerfert added inline comments.


================
Comment at: openmp/libomptarget/src/device.cpp:386
+      // simply skip the deletion step then.
+      HT.setDeleteThreadId(__kmpc_global_thread_num(NULL));
+      HT.decRefCount(UseHoldRefCount);
----------------
tianshilei1992 wrote:
> Unfortunately, `__kmpc_global_thread_num` cannot give you a unique id. I did a simple test:
> ```
> #include <cstdint>
> #include <cstdio>
> #include <thread>
> #include <vector>
> 
> extern "C" int32_t __kmpc_global_thread_num(void *);
> 
> void foo() {
>   std::chrono::seconds timespan(1);
>   std::this_thread::sleep_for(timespan);
>   int gtid = __kmpc_global_thread_num(nullptr);
>   printf("gtid = %d.\n", gtid);
> }
> 
> int main(int argc, char *argv[]) {
>   std::vector<std::thread> pool(8);
>   for (int i = 0; i < 8; ++i) {
>     pool[i] = std::thread(foo);
>   }
> 
>   for (std::thread &t : pool) {
>     if (t.joinable()) {
>       t.join();
>     }
>   }
> 
>   return 0;
> }
> ```
> It prints:
> ```
> gtid = 0.
> gtid = 9.
> gtid = 10.
> gtid = 9.
> gtid = 10.
> gtid = 9.
> gtid = 10.
> gtid = 9.
> ```
> Since `libomptarget` is not forced to use OpenMP thread, we cannot use functions in `libomp` to tell thread id. `std::thread::id` should be used here I think.
Interesting. I was expecting only openmp threads but you are right. Let's do std::trhead::id then.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121058/new/

https://reviews.llvm.org/D121058



More information about the Openmp-commits mailing list