xref: /freebsd-src/contrib/llvm-project/llvm/lib/Support/ThreadPool.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
1 //==-- llvm/Support/ThreadPool.cpp - A ThreadPool implementation -*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a crude C++11 based thread pool.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/ThreadPool.h"
14 
15 #include "llvm/Config/llvm-config.h"
16 #include "llvm/Support/Threading.h"
17 #include "llvm/Support/raw_ostream.h"
18 
19 using namespace llvm;
20 
21 #if LLVM_ENABLE_THREADS
22 
23 ThreadPool::ThreadPool(ThreadPoolStrategy S)
24     : ThreadCount(S.compute_thread_count()) {
25   // Create ThreadCount threads that will loop forever, wait on QueueCondition
26   // for tasks to be queued or the Pool to be destroyed.
27   Threads.reserve(ThreadCount);
28   for (unsigned ThreadID = 0; ThreadID < ThreadCount; ++ThreadID) {
29     Threads.emplace_back([S, ThreadID, this] {
30       S.apply_thread_strategy(ThreadID);
31       while (true) {
32         std::function<void()> Task;
33         {
34           std::unique_lock<std::mutex> LockGuard(QueueLock);
35           // Wait for tasks to be pushed in the queue
36           QueueCondition.wait(LockGuard,
37                               [&] { return !EnableFlag || !Tasks.empty(); });
38           // Exit condition
39           if (!EnableFlag && Tasks.empty())
40             return;
41           // Yeah, we have a task, grab it and release the lock on the queue
42 
43           // We first need to signal that we are active before popping the queue
44           // in order for wait() to properly detect that even if the queue is
45           // empty, there is still a task in flight.
46           ++ActiveThreads;
47           Task = std::move(Tasks.front());
48           Tasks.pop();
49         }
50         // Run the task we just grabbed
51         Task();
52 
53         bool Notify;
54         {
55           // Adjust `ActiveThreads`, in case someone waits on ThreadPool::wait()
56           std::lock_guard<std::mutex> LockGuard(QueueLock);
57           --ActiveThreads;
58           Notify = workCompletedUnlocked();
59         }
60         // Notify task completion if this is the last active thread, in case
61         // someone waits on ThreadPool::wait().
62         if (Notify)
63           CompletionCondition.notify_all();
64       }
65     });
66   }
67 }
68 
69 void ThreadPool::wait() {
70   // Wait for all threads to complete and the queue to be empty
71   std::unique_lock<std::mutex> LockGuard(QueueLock);
72   CompletionCondition.wait(LockGuard, [&] { return workCompletedUnlocked(); });
73 }
74 
75 bool ThreadPool::isWorkerThread() const {
76   llvm::thread::id CurrentThreadId = llvm::this_thread::get_id();
77   for (const llvm::thread &Thread : Threads)
78     if (CurrentThreadId == Thread.get_id())
79       return true;
80   return false;
81 }
82 
83 // The destructor joins all threads, waiting for completion.
84 ThreadPool::~ThreadPool() {
85   {
86     std::unique_lock<std::mutex> LockGuard(QueueLock);
87     EnableFlag = false;
88   }
89   QueueCondition.notify_all();
90   for (auto &Worker : Threads)
91     Worker.join();
92 }
93 
94 #else // LLVM_ENABLE_THREADS Disabled
95 
96 // No threads are launched, issue a warning if ThreadCount is not 0
97 ThreadPool::ThreadPool(ThreadPoolStrategy S)
98     : ThreadCount(S.compute_thread_count()) {
99   if (ThreadCount != 1) {
100     errs() << "Warning: request a ThreadPool with " << ThreadCount
101            << " threads, but LLVM_ENABLE_THREADS has been turned off\n";
102   }
103 }
104 
105 void ThreadPool::wait() {
106   // Sequential implementation running the tasks
107   while (!Tasks.empty()) {
108     auto Task = std::move(Tasks.front());
109     Tasks.pop();
110     Task();
111   }
112 }
113 
114 ThreadPool::~ThreadPool() { wait(); }
115 
116 #endif
117