-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadPool.c
More file actions
207 lines (147 loc) · 6 KB
/
threadPool.c
File metadata and controls
207 lines (147 loc) · 6 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// Omri Fridental
#include "threadPool.h"
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#define ERROR_MESSAGE "Error in system call"
void tpThread(ThreadPool*);
/**
* creates the threadpool and return a pointer to it.
* allocates memeory.
* setts the number of threads running to be numOfThreads.
*
* @param numOfThreads the number of threads the threadpool had.
* @return
*/
ThreadPool* tpCreate(int numOfThreads) {
if (numOfThreads <= 0) return NULL;
// allocating memory for the ThreadPool and its members;
ThreadPool* threadPool = (ThreadPool*) malloc(sizeof(ThreadPool));
if (threadPool == NULL) {
fprintf(stderr, ERROR_MESSAGE);
return NULL;
}
threadPool->N = numOfThreads;
threadPool->tasksQueue = osCreateQueue();
threadPool->threads = (pthread_t*) calloc(sizeof(pthread_t), numOfThreads);
threadPool->destroying = 0;
threadPool->taskPendingCount = 0;
// setting up a lock on the queue, and starting act in different thread to start the thread pool loop.
if( pthread_mutex_init(&threadPool->queueLock, NULL) != 0 || pthread_cond_init(&threadPool->conditional, NULL)
|| threadPool->threads == NULL || threadPool->tasksQueue == NULL)
{
fprintf(stderr, ERROR_MESSAGE);
return NULL;
};
// opening numOfThreads new threads of the threadpool action.
int i;
for (i = 0; i < numOfThreads; i++) {
// if a creation of a thread failed, shutting down all threadpool.
if (pthread_create(&threadPool->threads[i], NULL, (void* (*) (void*))tpThread, (void*) threadPool) != 0) {
tpDestroy(threadPool, 0);
return NULL;
}
}
// returning the ThreadPool created.
return threadPool;
}
/**
* destroys threadPool. free resources.
* blocks the option to add more tasks.
*
*
*
* @param threadPool the threadPool pointer to destroy.
* @param shouldWaitForTasks a flag of:
* if its != 0, waits until *all* tasks, including ones on the queue, to end.
* else, if its 0, waits until the current tasks that are running to end. throws away the tasks queue.
*/
void tpDestroy(ThreadPool* threadPool, int shouldWaitForTasks) {
// if already destroying from other thread, do not destroy again.
if(threadPool->destroying)
return;
// defining that we currently destroying the threadpool, and setts the type of destroy by shouldWaitForTasks flag.
threadPool->destroying = 1;
threadPool->destoryType = shouldWaitForTasks ? DESTROY_WHEN_QUEUE_EMPTY: DESTROY_IMMIDETLY;
// if the shouldWaitForTasks is not 0, puts flag of wait until all
pthread_t *iter = threadPool->threads,
*end = threadPool->threads + threadPool->N;
while (iter != end) {
pthread_join(*iter, NULL);
iter ++;
}
// freeing sources:
// freeing queue memory.
while (! osIsQueueEmpty(threadPool->tasksQueue))
free(osDequeue(threadPool->tasksQueue));
osDestroyQueue(threadPool->tasksQueue);
free(threadPool->threads);
free(threadPool);
}
#define INSERTION_FAILTURE -1
#define INSERTION_SUCCESS 0
int tpInsertTask(ThreadPool* threadPool, void (*computeFunc) (void *), void* param) {
// if arguments are null, or the destroying flag is on, return FAILTURE
if (threadPool == NULL || computeFunc == NULL || threadPool->destroying)
return INSERTION_FAILTURE;
// else, add the function to the threadPool queue, and return SUCCESS
else {
// creating a task of the insertion parameter.
Task* task = (Task*) malloc (sizeof(Task));
task->func_ptr = computeFunc;
task->arg = param;
// thread safe of the queue by using a mutex, and adding the task to the queue. signaling threads that is
// an available new tasks in queue using conditional.
pthread_mutex_lock(&threadPool->queueLock);
osEnqueue(threadPool->tasksQueue, task);
threadPool-> taskPendingCount += 1;
pthread_cond_signal(&threadPool->conditional);
pthread_mutex_unlock(&threadPool->queueLock);
// return success.
return INSERTION_SUCCESS;
}
}
/**
* the work that each one of the threads in the threadPool is doing.
* waits until a new task is in the queue.
* aquire the task (so other threads won't get it)
* do the task.
* when the flag of destroying the queue is on, by the type of destroyment and the state of the queue,
* sutting down the work of the thread.
*
* @param threadPool
*/
void tpThread(ThreadPool* threadPool) {
threadPool->numThreadsRunning ++;
// while we allowed to get new tasks, do it.
while (1) {
// locking the mutex to reach queue.
pthread_mutex_lock(&threadPool->queueLock);
// while the the numbers of tasks waiting in queue is 0, and the threadpool isn't destroying, wait.
while (threadPool->taskPendingCount == 0 && !threadPool->destroying) {
pthread_cond_wait(&threadPool->conditional, &threadPool->queueLock);
}
// if we do destroying: if its a SHUTTDOWN_IMMIDETLY, or if its a SHUTTDOWN_WHEN_QUEUE_EMPTY,
// and queue is indeed empty, stop the work of the thread (exit)
if (threadPool->destroying &&
(threadPool->destoryType == DESTROY_IMMIDETLY ||
(threadPool->destoryType == DESTROY_WHEN_QUEUE_EMPTY && threadPool->taskPendingCount == 0))
) {
break;
}
// pull the first task in the queue.
Task *taskToPreform = (Task *) osDequeue(threadPool->tasksQueue);
threadPool->taskPendingCount -= 1;
// unlocking the mutex.
pthread_mutex_unlock(&threadPool->queueLock);
// preform the task, and free memory.
if (taskToPreform != NULL) {
taskToPreform->func_ptr(taskToPreform->arg);
free(taskToPreform);
}
}
threadPool->numThreadsRunning --;
// unlocking the mutex if its on, and exit the thread.
pthread_mutex_unlock(&threadPool->queueLock);
pthread_exit(NULL);
}