summaryrefslogtreecommitdiff
path: root/libcppbuild.cc
diff options
context:
space:
mode:
Diffstat (limited to 'libcppbuild.cc')
-rw-r--r--libcppbuild.cc54
1 files changed, 49 insertions, 5 deletions
diff --git a/libcppbuild.cc b/libcppbuild.cc
index 35eff8f..3cba49f 100644
--- a/libcppbuild.cc
+++ b/libcppbuild.cc
@@ -3,11 +3,18 @@
#include <filesystem>
#include <iostream>
#include <utility>
+#include <list>
+#include <chrono>
+#include <thread>
#include "libcppbuild.h"
#include "task.h"
#include "settings.h"
+#include <unistd.h>
+
+using namespace std::chrono_literals;
+
int main(int argc, const char* argv[])
{
Settings settings;
@@ -15,6 +22,9 @@ int main(int argc, const char* argv[])
// TODO: Set from commandline
settings.builddir = "build/foo";
+ settings.parallel_processes =
+ std::max(1u, std::thread::hardware_concurrency() * 3);
+
std::filesystem::path builddir(settings.builddir);
std::filesystem::create_directories(builddir);
@@ -49,16 +59,50 @@ int main(int argc, const char* argv[])
}
std::cout << "Building\n";
+
+ std::list<std::future<int>> processes;
+
// Start all tasks
- for(auto& task : tasks)
+ auto task = tasks.begin();
+ while(task != tasks.end())
{
- task.start();
+ while(processes.size() < settings.parallel_processes &&
+ task != tasks.end())
+ {
+ processes.emplace_back(
+ std::async(std::launch::async,
+ [task]()
+ {
+ return task->run();
+ }));
+ ++task;
+ std::this_thread::sleep_for(10ms);
+ }
+
+ for(auto process = processes.begin();
+ process != processes.end();
+ ++process)
+ {
+ if(process->valid())
+ {
+ if(process->get() != 0)
+ {
+ return 1;
+ }
+ processes.erase(process);
+ break;
+ }
+ }
+
+ std::this_thread::sleep_for(10ms);
}
- // Wait for all tasks
- for(auto& task : tasks)
+ for(auto process = processes.begin();
+ process != processes.end();
+ ++process)
{
- if(task.wait() != 0)
+ process->wait();
+ if(process->get() != 0)
{
return 1;
}