-
Notifications
You must be signed in to change notification settings - Fork 13
/
thrdpool.c
293 lines (236 loc) · 5.98 KB
/
thrdpool.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
Copyright (c) 2019 Sogou, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Author: Xie Han ([email protected])
*/
#include <errno.h>
#include <pthread.h>
#include <stdlib.h>
#include "msgqueue.h"
#include "thrdpool.h"
struct __thrdpool
{
msgqueue_t *msgqueue;
size_t nthreads;
size_t stacksize;
pthread_t tid;
pthread_mutex_t mutex;
pthread_key_t key;
pthread_cond_t *terminate;
};
struct __thrdpool_task_entry
{
void *link;
struct thrdpool_task task;
};
static pthread_t __zero_tid;
static void __thrdpool_exit_routine(void *context)
{
thrdpool_t *pool = (thrdpool_t *)context;
pthread_t tid;
/* One thread joins another. Don't need to keep all thread IDs. */
pthread_mutex_lock(&pool->mutex);
tid = pool->tid;
pool->tid = pthread_self();
if (--pool->nthreads == 0 && pool->terminate)
pthread_cond_signal(pool->terminate);
pthread_mutex_unlock(&pool->mutex);
if (!pthread_equal(tid, __zero_tid))
pthread_join(tid, NULL);
pthread_exit(NULL);
}
static void *__thrdpool_routine(void *arg)
{
thrdpool_t *pool = (thrdpool_t *)arg;
struct __thrdpool_task_entry *entry;
void (*task_routine)(void *);
void *task_context;
pthread_setspecific(pool->key, pool);
while (!pool->terminate)
{
entry = (struct __thrdpool_task_entry *)msgqueue_get(pool->msgqueue);
if (!entry)
break;
task_routine = entry->task.routine;
task_context = entry->task.context;
free(entry);
task_routine(task_context);
if (pool->nthreads == 0)
{
/* Thread pool was destroyed by the task. */
free(pool);
return NULL;
}
}
__thrdpool_exit_routine(pool);
return NULL;
}
static void __thrdpool_terminate(int in_pool, thrdpool_t *pool)
{
pthread_cond_t term = PTHREAD_COND_INITIALIZER;
pthread_mutex_lock(&pool->mutex);
msgqueue_set_nonblock(pool->msgqueue);
pool->terminate = &term;
if (in_pool)
{
/* Thread pool destroyed in a pool thread is legal. */
pthread_detach(pthread_self());
pool->nthreads--;
}
while (pool->nthreads > 0)
pthread_cond_wait(&term, &pool->mutex);
pthread_mutex_unlock(&pool->mutex);
if (!pthread_equal(pool->tid, __zero_tid))
pthread_join(pool->tid, NULL);
}
static int __thrdpool_create_threads(size_t nthreads, thrdpool_t *pool)
{
pthread_attr_t attr;
pthread_t tid;
int ret;
ret = pthread_attr_init(&attr);
if (ret == 0)
{
if (pool->stacksize)
pthread_attr_setstacksize(&attr, pool->stacksize);
while (pool->nthreads < nthreads)
{
ret = pthread_create(&tid, &attr, __thrdpool_routine, pool);
if (ret == 0)
pool->nthreads++;
else
break;
}
pthread_attr_destroy(&attr);
if (pool->nthreads == nthreads)
return 0;
__thrdpool_terminate(0, pool);
}
errno = ret;
return -1;
}
thrdpool_t *thrdpool_create(size_t nthreads, size_t stacksize)
{
thrdpool_t *pool;
int ret;
pool = (thrdpool_t *)malloc(sizeof (thrdpool_t));
if (!pool)
return NULL;
pool->msgqueue = msgqueue_create(0, 0);
if (pool->msgqueue)
{
ret = pthread_mutex_init(&pool->mutex, NULL);
if (ret == 0)
{
ret = pthread_key_create(&pool->key, NULL);
if (ret == 0)
{
pool->stacksize = stacksize;
pool->nthreads = 0;
pool->tid = __zero_tid;
pool->terminate = NULL;
if (__thrdpool_create_threads(nthreads, pool) >= 0)
return pool;
pthread_key_delete(pool->key);
}
pthread_mutex_destroy(&pool->mutex);
}
errno = ret;
msgqueue_destroy(pool->msgqueue);
}
free(pool);
return NULL;
}
inline void __thrdpool_schedule(const struct thrdpool_task *task, void *buf,
thrdpool_t *pool);
void __thrdpool_schedule(const struct thrdpool_task *task, void *buf,
thrdpool_t *pool)
{
((struct __thrdpool_task_entry *)buf)->task = *task;
msgqueue_put(buf, pool->msgqueue);
}
int thrdpool_schedule(const struct thrdpool_task *task, thrdpool_t *pool)
{
void *buf = malloc(sizeof (struct __thrdpool_task_entry));
if (buf)
{
__thrdpool_schedule(task, buf, pool);
return 0;
}
return -1;
}
inline int thrdpool_in_pool(thrdpool_t *pool);
int thrdpool_in_pool(thrdpool_t *pool)
{
return pthread_getspecific(pool->key) == pool;
}
int thrdpool_increase(thrdpool_t *pool)
{
pthread_attr_t attr;
pthread_t tid;
int ret;
ret = pthread_attr_init(&attr);
if (ret == 0)
{
if (pool->stacksize)
pthread_attr_setstacksize(&attr, pool->stacksize);
pthread_mutex_lock(&pool->mutex);
ret = pthread_create(&tid, &attr, __thrdpool_routine, pool);
if (ret == 0)
pool->nthreads++;
pthread_mutex_unlock(&pool->mutex);
pthread_attr_destroy(&attr);
if (ret == 0)
return 0;
}
errno = ret;
return -1;
}
int thrdpool_decrease(thrdpool_t *pool)
{
void *buf = malloc(sizeof (struct __thrdpool_task_entry));
struct __thrdpool_task_entry *entry;
if (buf)
{
entry = (struct __thrdpool_task_entry *)buf;
entry->task.routine = __thrdpool_exit_routine;
entry->task.context = pool;
msgqueue_put_head(entry, pool->msgqueue);
return 0;
}
return -1;
}
void thrdpool_exit(thrdpool_t *pool)
{
if (thrdpool_in_pool(pool))
__thrdpool_exit_routine(pool);
}
void thrdpool_destroy(void (*pending)(const struct thrdpool_task *),
thrdpool_t *pool)
{
int in_pool = thrdpool_in_pool(pool);
struct __thrdpool_task_entry *entry;
__thrdpool_terminate(in_pool, pool);
while (1)
{
entry = (struct __thrdpool_task_entry *)msgqueue_get(pool->msgqueue);
if (!entry)
break;
if (pending && entry->task.routine != __thrdpool_exit_routine)
pending(&entry->task);
free(entry);
}
pthread_key_delete(pool->key);
pthread_mutex_destroy(&pool->mutex);
msgqueue_destroy(pool->msgqueue);
if (!in_pool)
free(pool);
}