tg.spawn(fn, serial);
}
- // Both the main thread and thread pool index 0 use getThreadIndex()==0. Be
- // careful that they don't concurrently run scanSections. When serial is
- // true, fn() has finished at this point, so running execute is safe.
tg.spawn([] {
RelocationScanner scanner;
for (Partition &part : partitions) {
extern ThreadPoolStrategy strategy;
#if LLVM_ENABLE_THREADS
+#define GET_THREAD_INDEX_IMPL \
+ if (parallel::strategy.ThreadsRequested == 1) \
+ return 0; \
+ assert((threadIndex != UINT_MAX) && \
+ "getThreadIndex() must be called from a thread created by " \
+ "ThreadPoolExecutor"); \
+ return threadIndex;
+
#ifdef _WIN32
// Direct access to thread_local variables from a different DLL isn't
// possible with Windows Native TLS.
// Don't access this directly, use the getThreadIndex wrapper.
extern thread_local unsigned threadIndex;
-inline unsigned getThreadIndex() { return threadIndex; }
+inline unsigned getThreadIndex() { GET_THREAD_INDEX_IMPL; }
#endif
#else
inline unsigned getThreadIndex() { return 0; }
#if LLVM_ENABLE_THREADS
#ifdef _WIN32
-static thread_local unsigned threadIndex;
+static thread_local unsigned threadIndex = UINT_MAX;
-unsigned getThreadIndex() { return threadIndex; }
+unsigned getThreadIndex() { GET_THREAD_INDEX_IMPL; }
#else
-thread_local unsigned threadIndex;
+thread_local unsigned threadIndex = UINT_MAX;
#endif
namespace detail {
void add(std::function<void()> F, bool Sequential = false) override {
{
- bool UseSequentialQueue =
- Sequential || parallel::strategy.ThreadsRequested == 1;
+ if (parallel::strategy.ThreadsRequested == 1) {
+ F();
+ return;
+ }
+
std::lock_guard<std::mutex> Lock(Mutex);
- if (UseSequentialQueue)
+ if (Sequential)
WorkQueueSequential.emplace_front(std::move(F));
else
WorkQueue.emplace_back(std::move(F));
void llvm::parallelFor(size_t Begin, size_t End,
llvm::function_ref<void(size_t)> Fn) {
- // If we have zero or one items, then do not incur the overhead of spinning up
- // a task group. They are surprisingly expensive, and because they do not
- // support nested parallelism, a single entry task group can block parallel
- // execution underneath them.
#if LLVM_ENABLE_THREADS
- auto NumItems = End - Begin;
- if (NumItems > 1 && parallel::strategy.ThreadsRequested != 1) {
+ if (parallel::strategy.ThreadsRequested != 1) {
+ auto NumItems = End - Begin;
// Limit the number of tasks to MaxTasksPerGroup to limit job scheduling
// overhead on large inputs.
auto TaskSize = NumItems / parallel::detail::MaxTasksPerGroup;