-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.cpp
113 lines (88 loc) · 2.46 KB
/
scheduler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Created by Max Azimi on 3/22/2022 AD.
*/
#include "scheduler.h"
/*
* TaskScheduler implementation.
*/
Scheduler::Scheduler() :
mThreadArgs({nullptr}),
mTasksRunning(0)
{
pthread_mutex_init(&mQueueLock, nullptr);
pthread_cond_init(&mQueueCond, nullptr);
pthread_cond_init(&mTasksFinishedCond, nullptr);
mThreadArgs.taskQueue = &mTaskQueue;
mThreadArgs.queueCond = &mQueueCond;
mThreadArgs.queueLock = &mQueueLock;
mThreadArgs.tasksRunning = &mTasksRunning;
mThreadArgs.tasksFinishedCond = &mTasksFinishedCond;
}
Scheduler::~Scheduler()
{
//for (auto & thread : mThreads) pthread_cancel(thread);
for (auto & task : mTaskQueue)
task->terminate();
waitUntilFinished();
pthread_cond_broadcast(&mQueueCond);
for (auto & thread : mThreads)
pthread_join(thread, nullptr);
printf("~TaskScheduler()--> number of task(s) terminated: (%lu)\n", mThreads.size());
mThreads.clear();
pthread_mutex_destroy(&mQueueLock);
pthread_cond_destroy(&mQueueCond);
pthread_cond_destroy(&mTasksFinishedCond);
}
void Scheduler::schedule(Task *task)
{
if (task == nullptr)
return;
pthread_t newThread;
if (pthread_create(&newThread, nullptr, thread_function, (void *)&mThreadArgs) < 0)
{
perror("pthread_create failed");
newThread = nullptr;
return;
}
mThreads.push_back(newThread);
pthread_mutex_lock(&mQueueLock);
mTaskQueue.push_back(task);
pthread_cond_signal(&mQueueCond);
pthread_mutex_unlock(&mQueueLock);
}
void Scheduler::waitUntilFinished()
{
pthread_mutex_lock(&mQueueLock);
while (!mTaskQueue.empty() || mTasksRunning > 0)
{
pthread_cond_wait(&mTasksFinishedCond, &mQueueLock);
}
pthread_mutex_unlock(&mQueueLock);
}
void *thread_function(void *args)
{
auto *threadArgs = (thread_args_t *)args;
while (pthread_mutex_lock(threadArgs->queueLock) < 0)
{
perror("pthread_mutex_lock failed");
}
while (threadArgs->taskQueue->empty())
{
pthread_cond_wait(threadArgs->queueCond, threadArgs->queueLock);
}
auto task = threadArgs->taskQueue->front(); // get pointer to the next task
threadArgs->taskQueue->pop_front(); // remove it from queue
(*threadArgs->tasksRunning)++;
pthread_mutex_unlock(threadArgs->queueLock);
pthread_mutex_lock(&task->mLock);
task->run();
pthread_mutex_unlock(&task->mLock);
delete task;
pthread_mutex_lock(threadArgs->queueLock);
if (--(*threadArgs->tasksRunning) == 0)
{
pthread_cond_broadcast(threadArgs->tasksFinishedCond);
}
pthread_mutex_unlock(threadArgs->queueLock);
pthread_exit(nullptr);
}