[PATCH] Use std::async instead of our own parallel execution instrumentation.
Rui Ueyama
ruiu at google.com
Fri Oct 11 15:27:16 PDT 2013
Hi Bigcheese,
Is there any reason to use our own library rather than the standard one?
Because we are already using C++11 threading library, I think I don't see
any reason not to use std::async().
http://llvm-reviews.chandlerc.com/D1904
Files:
lib/Driver/Driver.cpp
Index: lib/Driver/Driver.cpp
===================================================================
--- lib/Driver/Driver.cpp
+++ lib/Driver/Driver.cpp
@@ -26,6 +26,7 @@
#include "llvm/Support/raw_ostream.h"
#include <mutex>
+#include <future>
namespace lld {
@@ -45,39 +46,40 @@
if (!inputGraph.size())
return false;
- bool fail = false;
-
// Read inputs
ScopedTask readTask(getDefaultDomain(), "Read Args");
- TaskGroup tg;
std::mutex diagnosticsMutex;
+ std::vector<std::future<bool> > tasks;
+ // First pass: submits tasks to the executor so that the files are parsed in
+ // paralell.
for (auto &ie : inputGraph.inputElements()) {
- tg.spawn([&] {
+ auto parseFuture = std::async(std::launch::async, [&]{
// Writes to the same output stream is not guaranteed to be thread-safe.
// We buffer the diagnostics output to a separate string-backed output
// stream, acquire the lock, and then print it out.
std::string buf;
llvm::raw_string_ostream stream(buf);
- if (error_code ec = ie->parse(context, stream)) {
+ error_code parseFailed = ie->parse(context, stream);
+ if (parseFailed) {
FileNode *fileNode = llvm::dyn_cast<FileNode>(ie.get());
- stream << fileNode->errStr(ec) << "\n";
- fail = true;
+ stream << fileNode->errStr(parseFailed) << "\n";
}
-
stream.flush();
if (!buf.empty()) {
std::lock_guard<std::mutex> lock(diagnosticsMutex);
diagnostics << buf;
}
+ return !parseFailed;
});
+ tasks.push_back(std::move(parseFuture));
}
- tg.sync();
+ // Second pass: collect parsing results. Exit from this function if error.
+ for (auto &task : tasks)
+ if (!task.get())
+ return false;
readTask.end();
- if (fail)
- return false;
-
std::unique_ptr<SimpleFileNode> fileNode(
new SimpleFileNode("Internal Files"));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D1904.1.patch
Type: text/x-patch
Size: 1934 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131011/95b42d09/attachment.bin>
More information about the llvm-commits
mailing list