Browse Source

only spawn threads as necessary

rebrand
Martin Dørum 3 years ago
parent
commit
5dd213551c
2 changed files with 16 additions and 9 deletions
  1. 1
    1
      src/DepNode.cc
  2. 15
    8
      src/parallel.cc

+ 1
- 1
src/DepNode.cc View File

@@ -81,7 +81,7 @@ void DepNode::joinBuild() {
return;
}

build_cond_.wait(lock, [=] { return build_complete_; });
build_cond_.wait(lock, [this] { return build_complete_; });
}

void DepNode::writeCompDB(const std::string &outDir, compdb::Writer &w) {

+ 15
- 8
src/parallel.cc View File

@@ -22,6 +22,8 @@ static std::mutex workers_mut;
static std::unique_ptr<Worker[]> workers;
static Worker *head= nullptr;
static int threads_running = 0;
static int max_threads = 0;
static int num_threads = 0;
static int waiting = 0;

static void workerFunc(Worker *w) {
@@ -49,16 +51,10 @@ ParallelContext init(int num) {
return ParallelContext{};
}

max_threads = num;
workers.reset(new Worker[num]);
for (int i = 0; i < num; ++i) {
Worker *w = &workers[i];
w->next_worker = head;
head = w;

w->running = true;
w->thread = std::thread(workerFunc, w);
}

// Don't spawn threads; they're spawned as needed
return ParallelContext{};
}

@@ -81,6 +77,8 @@ ParallelContext::~ParallelContext() {

workers.reset();
head = nullptr;
num_threads = 0;
max_threads = 0;
}

void run(std::function<void(void)> func) {
@@ -90,6 +88,15 @@ void run(std::function<void(void)> func) {
return;
}

if (head == nullptr && num_threads < max_threads) {
Worker *w = &workers[num_threads++];
w->next_worker = head;
head = w;

w->running = true;
w->thread = std::thread(workerFunc, w);
}

waiting += 1;
workers_cond.wait(lock, [] { return head != nullptr; });
Worker *w = head;

Loading…
Cancel
Save