Files
llvm/lld/Common/Threads.cpp
Bob Haarman 4f5c8c29ac [lld] Move Threads to Common
Summary:
This will allow using the functionality from other linkers. It is also
a prerequisite for sharing the error logging code.

Reviewers: ruiu

Reviewed By: ruiu

Subscribers: emaste, mgorny, llvm-commits

Differential Revision: https://reviews.llvm.org/D38822

llvm-svn: 315725
2017-10-13 18:22:55 +00:00

32 lines
930 B
C++

//===- Threads.cpp --------------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/Common/Threads.h"
#include <thread>
static std::vector<std::thread> Threads;
bool lld::ThreadsEnabled = true;
// Runs a given function in a new thread.
void lld::runBackground(std::function<void()> Fn) {
Threads.emplace_back(Fn);
}
// Wait for all threads spawned for runBackground() to finish.
//
// You need to call this function from the main thread before exiting
// because it is not defined what will happen to non-main threads when
// the main thread exits.
void lld::waitForBackgroundThreads() {
for (std::thread &T : Threads)
if (T.joinable())
T.join();
}