|
|
@@ -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; |