1 #include "smaug/utility/thread_pool.h"
2 #include "smaug/utility/utils.h"
10 ThreadPool::~ThreadPool() {
12 for (
int i = 0; i <
workers.size(); i++) {
13 WorkerThread* worker = &
workers[i];
15 pthread_mutex_lock(&worker->statusMutex);
17 pthread_cond_signal(&worker->wakeupCond);
18 pthread_mutex_unlock(&worker->statusMutex);
20 for (
int i = 0; i <
workers.size(); i++) {
21 WorkerThread* worker = &
workers[i];
22 pthread_join(worker->thread, NULL);
23 pthread_mutex_destroy(&worker->statusMutex);
24 pthread_cond_destroy(&worker->wakeupCond);
25 pthread_cond_destroy(&worker->statusCond);
34 pthread_mutex_lock(&initArgs->cpuidMutex);
36 worker->status = Idle;
37 pthread_cond_signal(&initArgs->cpuidCond);
38 pthread_mutex_unlock(&initArgs->cpuidMutex);
46 worker->status = Running;
50 worker->func(worker->
args);
53 worker->status = Idle;
54 worker->
valid =
false;
57 bool exitThread = worker->
exit;
68 for (
int i = 0; i <
workers.size(); i++) {
75 pthread_mutex_lock(&initArgs.cpuidMutex);
76 while (initArgs.cpuid == -1 || worker->status == Uninitialized)
77 pthread_cond_wait(&initArgs.cpuidCond, &initArgs.cpuidMutex);
78 worker->
cpuid = initArgs.cpuid;
79 pthread_mutex_unlock(&initArgs.cpuidMutex);
80 assert(worker->status != Uninitialized &&
81 "Worker thread did not successfully initialize!");
86 for (
int i = 0; i <
workers.size(); i++) {
89 if (worker->status == Idle && !worker->
valid) {
107 for (
int i = 0; i <
workers.size(); i++) {
110 while (worker->status == Running || worker->
valid ==
true)