-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththreadpool.c
More file actions
127 lines (105 loc) · 3.56 KB
/
threadpool.c
File metadata and controls
127 lines (105 loc) · 3.56 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
#include "threadpool.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
static void* threadpool_worker (void* arg);
threadpool* threadpool_create (size_t nthreads)
{
threadpool *pool = malloc(sizeof(threadpool));
assert(pool != NULL);
pool->num_active_jobs = 0;
pool->first_job = NULL;
pool->last_job = NULL;
pthread_mutex_init(&pool->job_list_lock, NULL);
pthread_mutex_init(&pool->worker_wakeup_lock, NULL);
pthread_cond_init(&pool->worker_wakeup_cond, NULL);
pool->flag_exit_please = 0;
pool->nthreads = nthreads;
pool->threads = calloc(nthreads, sizeof pool->threads[0]);
assert(pool->threads != NULL);
for (size_t i = 0; i < nthreads; i++) {
int err = pthread_create(&pool->threads[i], NULL, threadpool_worker, pool);
assert(err == 0);
}
return pool;
}
void threadpool_destroy (threadpool *pool)
{
pool->flag_exit_please = 1;
pthread_cond_broadcast(&pool->worker_wakeup_cond);
for (size_t i = 0; i < pool->nthreads; i++) {
int err = pthread_join(pool->threads[i], NULL);
assert(err == 0);
}
assert(pool->num_active_jobs == 0); // we should be done with all jobs now
free(pool->threads);
assert(pool->first_job == NULL); // there should be no jobs left in the queue
assert(pool->last_job == NULL);
pthread_mutex_destroy(&pool->job_list_lock);
pthread_mutex_destroy(&pool->worker_wakeup_lock);
pthread_cond_destroy(&pool->worker_wakeup_cond);
free(pool);
}
void threadpool_add_job (threadpool *pool, void (*func)(void*), void* arg)
{
job_list *new = malloc(sizeof(job_list));
assert(new != NULL);
new->func = func;
new->arg = arg;
new->next = NULL;
pthread_mutex_lock(&pool->job_list_lock);
if (pool->first_job == NULL) {
pool->first_job = new;
pool->last_job = new;
} else {
job_list *tmp = pool->last_job;
pool->last_job = new;
tmp->next = new;
}
pthread_mutex_unlock(&pool->job_list_lock);
__sync_fetch_and_add(&pool->num_active_jobs, 1);
pthread_cond_broadcast(&pool->worker_wakeup_cond);
}
void threadpool_wait (threadpool *pool)
{
while (pool->num_active_jobs > 0)
;
}
////////////////////////////////////////////////////////////////////////////////
static void* threadpool_worker (void* arg)
{
threadpool *pool = arg;
struct timeval now;
struct timespec t;
memset(&t, '\0', sizeof t);
while (1) {
pthread_mutex_lock(&pool->job_list_lock);
if (pool->first_job) {
job_list *node = pool->first_job;
pool->first_job = node->next;
if (pool->first_job == NULL) {
pool->last_job = NULL;
}
pthread_mutex_unlock(&pool->job_list_lock);
node->func(node->arg);
free(node);
__sync_sub_and_fetch(&pool->num_active_jobs, 1);
} else {
pthread_mutex_unlock(&pool->job_list_lock);
if (pool->flag_exit_please) {
pthread_cond_broadcast(&pool->worker_wakeup_cond);
pthread_exit(NULL);
return NULL;
} else {
pthread_mutex_lock(&pool->worker_wakeup_lock);
gettimeofday(&now, NULL);
t.tv_sec = now.tv_sec + 1;
pthread_cond_timedwait(&pool->worker_wakeup_cond,
&pool->worker_wakeup_lock, &t);
pthread_mutex_unlock(&pool->worker_wakeup_lock);
}
}
}
}