-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy paththreadpool.c
209 lines (175 loc) · 5.45 KB
/
threadpool.c
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
208
209
/**
* threadpool.c
*
* This file will contain your implementation of a threadpool.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include "threadpool.h"
// _threadpool is the internal threadpool structure that is
// cast to type "threadpool" before it given out to callers
typedef struct work_st{
void (*routine) (void*);
void * arg;
struct work_st* next;
} work_t;
typedef struct _threadpool_st {
// you should fill in this structure with whatever you need
int num_threads; //number of active threads
int qsize; //number in the queue
pthread_t *threads; //pointer to threads
work_t* qhead; //queue head pointer
work_t* qtail; //queue tail pointer
pthread_mutex_t qlock; //lock on the queue list
pthread_mutex_t qmutexlock; //lock for not empty
pthread_cond_t q_not_empty; //non empty and empty condidtion vairiables
pthread_cond_t q_empty;
int shutdown;
int dont_accept;
} _threadpool;
/* This function is the work function of the thread */
void* do_work(threadpool p) {
_threadpool * pool = (_threadpool *) p;
work_t* cur; //The q element
while(1) {
pool->qsize = pool->qsize;
pthread_mutex_lock(&(pool->qlock)); //get the q lock.
while( pool->qsize == 0) { //if the size is 0 then wait.
if(pool->shutdown) {
pthread_mutex_unlock(&(pool->qlock));
pthread_exit(NULL);
}
//wait until the condition says its no emtpy and give up the lock.
pthread_mutex_unlock(&(pool->qlock)); //get the qlock.
pthread_cond_wait(&(pool->q_not_empty),&(pool->qlock));
//check to see if in shutdown mode.
if(pool->shutdown) {
pthread_mutex_unlock(&(pool->qlock));
pthread_exit(NULL);
}
}
cur = pool->qhead; //set the cur variable.
pool->qsize--; //decriment the size.
if(pool->qsize == 0) {
pool->qhead = NULL;
pool->qtail = NULL;
}
else {
pool->qhead = cur->next;
}
if(pool->qsize == 0 && ! pool->shutdown) {
//the q is empty again, now signal that its empty.
pthread_cond_signal(&(pool->q_empty));
}
pthread_mutex_unlock(&(pool->qlock));
(cur->routine) (cur->arg); //actually do work.
free(cur); //free the work storage.
}
}
threadpool create_threadpool(int num_threads_in_pool) {
_threadpool *pool;
int i;
// sanity check the argument
if ((num_threads_in_pool <= 0) || (num_threads_in_pool > MAXT_IN_POOL))
return NULL;
pool = (_threadpool *) malloc(sizeof(_threadpool));
if (pool == NULL) {
fprintf(stderr, "Out of memory creating a new threadpool!\n");
return NULL;
}
pool->threads = (pthread_t*) malloc (sizeof(pthread_t) * num_threads_in_pool);
if(!pool->threads) {
fprintf(stderr, "Out of memory creating a new threadpool!\n");
return NULL;
}
pool->num_threads = num_threads_in_pool; //set up structure members
pool->qsize = 0;
pool->qhead = NULL;
pool->qtail = NULL;
pool->shutdown = 0;
pool->dont_accept = 0;
//initialize mutex and condition variables.
if(pthread_mutex_init(&pool->qlock,NULL)) {
fprintf(stderr, "Mutex initiation error!\n");
return NULL;
}
if(pthread_mutex_init(&pool->qmutexlock,NULL)) {
fprintf(stderr, "Mutex initiation error!\n");
return NULL;
}
if(pthread_cond_init(&(pool->q_empty),NULL)) {
fprintf(stderr, "CV initiation error!\n");
return NULL;
}
if(pthread_cond_init(&(pool->q_not_empty),NULL)) {
fprintf(stderr, "CV initiation error!\n");
return NULL;
}
//make threads
for (i = 0;i < num_threads_in_pool;i++) {
if(pthread_create(&(pool->threads[i]),NULL,do_work,pool)) {
fprintf(stderr, "Thread initiation error!\n");
return NULL;
}
}
return (threadpool) pool;
}
void dispatch(threadpool from_me, dispatch_fn dispatch_to_here,
void *arg) {
_threadpool *pool = (_threadpool *) from_me;
work_t * cur;
int k;
k = pool->qsize;
//make a work queue element.
cur = (work_t*) malloc(sizeof(work_t));
if(cur == NULL) {
fprintf(stderr, "Out of memory creating a work struct!\n");
return;
}
cur->routine = dispatch_to_here;
cur->arg = arg;
cur->next = NULL;
pthread_mutex_lock(&(pool->qlock));
if(pool->dont_accept) { //Just incase someone is trying to queue more
free(cur); //work structs.
return;
}
if(pool->qsize == 0) {
pool->qhead = cur; //set to only one
pool->qtail = cur;
pthread_cond_signal(&(pool->q_not_empty)); //I am not empty.
} else {
pool->qtail->next = cur; //add to end;
pool->qtail = cur;
}
pool->qsize++;
pthread_mutex_unlock(&(pool->qlock)); //unlock the queue.
}
void destroy_threadpool(threadpool destroyme) {
_threadpool *pool = (_threadpool *) destroyme;
void* nothing;
int i = 0;
/*
pthread_mutex_lock(&(pool->qlock));
pool->dont_accept = 1;
while(pool->qsize != 0) {
pthread_cond_wait(&(pool->q_empty),&(pool->qlock)); //wait until the q is empty.
}
pool->shutdown = 1; //allow shutdown
pthread_cond_broadcast(&(pool->q_not_empty)); //allow code to return NULL;
pthread_mutex_unlock(&(pool->qlock));
//kill everything.
for(;i < pool->num_threads;i++) {
// pthread_cond_broadcast(&(pool->q_not_empty));
//allowcode to return NULL;/
pthread_join(pool->threads[i],¬hing);
}
*/
free(pool->threads);
pthread_mutex_destroy(&(pool->qlock));
pthread_cond_destroy(&(pool->q_empty));
pthread_cond_destroy(&(pool->q_not_empty));
return;
}