[PATCH] D60758: Add an assertion that parallelForEach doesn't nest.

Rui Ueyama via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 16 01:08:08 PDT 2019


ruiu created this revision.
ruiu added reviewers: sbc100, MaskRay, andrewrk.
Herald added a project: LLVM.

Add an assertion that parallelForEach doesn't nest.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D60758

Files:
  lld/Common/Threads.cpp
  lld/include/lld/Common/Threads.h


Index: lld/include/lld/Common/Threads.h
===================================================================
--- lld/include/lld/Common/Threads.h
+++ lld/include/lld/Common/Threads.h
@@ -64,20 +64,32 @@
 namespace lld {
 
 extern bool ThreadsEnabled;
+extern thread_local bool ParallelForEachRunning;
 
 template <typename R, class FuncTy> void parallelForEach(R &&Range, FuncTy Fn) {
+  // for_each is not reentrant, so guard against it.
+  assert(!ParallelForEachRunning);
+  ParallelForEachRunning = true;
+
   if (ThreadsEnabled)
     for_each(llvm::parallel::par, std::begin(Range), std::end(Range), Fn);
   else
     for_each(llvm::parallel::seq, std::begin(Range), std::end(Range), Fn);
+
+  ParallelForEachRunning = false;
 }
 
 inline void parallelForEachN(size_t Begin, size_t End,
                              llvm::function_ref<void(size_t)> Fn) {
+  assert(!ParallelForEachRunning);
+  ParallelForEachRunning = true;
+
   if (ThreadsEnabled)
     for_each_n(llvm::parallel::par, Begin, End, Fn);
   else
     for_each_n(llvm::parallel::seq, Begin, End, Fn);
+
+  ParallelForEachRunning = false;
 }
 
 template <typename R, class FuncTy> void parallelSort(R &&Range, FuncTy Fn) {
Index: lld/Common/Threads.cpp
===================================================================
--- lld/Common/Threads.cpp
+++ lld/Common/Threads.cpp
@@ -7,5 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "lld/Common/Threads.h"
+#include <thread>
 
 bool lld::ThreadsEnabled = true;
+thread_local bool lld::ParallelForEachRunning;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D60758.195320.patch
Type: text/x-patch
Size: 1580 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190416/3662fda2/attachment.bin>


More information about the llvm-commits mailing list