Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions llvm/lib/Support/Parallel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class ThreadPoolExecutor : public Executor {
if (S.UseJobserver)
TheJobserver = JobserverClient::getInstance();

ThreadsCreatedFuture = ThreadsCreated.get_future();
ThreadCount = S.compute_thread_count();
// Spawn all but one of the threads in another thread as spawning threads
// can take a while.
Expand Down Expand Up @@ -84,24 +85,28 @@ class ThreadPoolExecutor : public Executor {
void stop() {
{
std::lock_guard<std::mutex> Lock(Mutex);
if (Stop)
return;
Stop = true;
}

Cond.notify_all();
ThreadsCreated.get_future().wait();
}
ThreadsCreatedFuture.wait();

std::vector<std::thread> ThreadsToJoin;
{
std::lock_guard<std::mutex> Lock(Mutex);
ThreadsToJoin.swap(Threads);
}

~ThreadPoolExecutor() override {
stop();
std::thread::id CurrentThreadId = std::this_thread::get_id();
for (std::thread &T : Threads)
for (std::thread &T : ThreadsToJoin)
if (T.get_id() == CurrentThreadId)
T.detach();
else
T.join();
}

~ThreadPoolExecutor() override { stop(); }

struct Creator {
static void *call() { return new ThreadPoolExecutor(strategy); }
};
Expand Down Expand Up @@ -187,6 +192,7 @@ class ThreadPoolExecutor : public Executor {
std::mutex Mutex;
std::condition_variable Cond;
std::promise<void> ThreadsCreated;
std::future<void> ThreadsCreatedFuture;
std::vector<std::thread> Threads;
unsigned ThreadCount;

Expand Down
Loading