-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtask.c
339 lines (280 loc) · 7.34 KB
/
task.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/******************************************************************************
* Copyright (C) 2008 - 2014 Andreas Öman
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
#include <stdlib.h>
#include <pthread.h>
#include <sys/queue.h>
#include <assert.h>
#include <errno.h>
#include "task.h"
#include "atomic.h"
#include "talloc.h"
#define MAX_TASK_THREADS 64
#define MAX_IDLE_TASK_THREADS 4
LIST_HEAD(task_thread_list, task_thread);
TAILQ_HEAD(task_queue, task);
TAILQ_HEAD(task_group_queue, task_group);
typedef struct task_thread {
LIST_ENTRY(task_thread) link;
pthread_t tid;
} task_thread_t;
struct task_group {
atomic_t tg_refcount;
struct task_queue tg_tasks;
TAILQ_ENTRY(task_group) tg_link;
int tg_max_concurrency;
int tg_num_processing;
};
typedef struct task {
TAILQ_ENTRY(task) t_link;
task_fn_t *t_fn;
void *t_opaque;
task_group_t *t_group;
} task_t;
static struct task_queue tasks = TAILQ_HEAD_INITIALIZER(tasks);
static struct task_group_queue task_groups =TAILQ_HEAD_INITIALIZER(task_groups);
static int num_task_threads;
static int num_task_threads_avail;
static pthread_mutex_t task_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t task_cond = PTHREAD_COND_INITIALIZER;
static int task_sys_running = 1;
static struct task_thread_list task_threads;
static uint64_t tasks_enqueued;
/**
*
*/
static void
task_group_release(task_group_t *tg)
{
if(atomic_dec(&tg->tg_refcount))
return;
assert(TAILQ_FIRST(&tg->tg_tasks) == NULL);
assert(tg->tg_num_processing == 0);
free(tg);
}
/**
*
*/
static void *
task_thread(void *aux)
{
task_thread_t *tt = aux;
task_t *t;
task_group_t *tg;
pthread_mutex_lock(&task_mutex);
while(task_sys_running) {
t = TAILQ_FIRST(&tasks);
tg = TAILQ_FIRST(&task_groups);
if(t == NULL && tg == NULL) {
if(num_task_threads_avail >= MAX_IDLE_TASK_THREADS) {
struct timespec timeout;
timeout.tv_sec = time(NULL) + 5;
timeout.tv_nsec = 0;
num_task_threads_avail++;
int r = pthread_cond_timedwait(&task_cond, &task_mutex, &timeout);
num_task_threads_avail--;
if(r == ETIMEDOUT) {
break;
}
} else {
num_task_threads_avail++;
pthread_cond_wait(&task_cond, &task_mutex);
num_task_threads_avail--;
}
continue;
}
if(t != NULL) {
TAILQ_REMOVE(&tasks, t, t_link);
pthread_mutex_unlock(&task_mutex);
t->t_fn(t->t_opaque);
free(t);
talloc_cleanup();
pthread_mutex_lock(&task_mutex);
// Released lock, must recheck for task groups
tg = TAILQ_FIRST(&task_groups);
}
if(tg != NULL) {
t = TAILQ_FIRST(&tg->tg_tasks);
TAILQ_REMOVE(&tg->tg_tasks, t, t_link);
tg->tg_num_processing++;
if(TAILQ_FIRST(&tg->tg_tasks) == NULL ||
tg->tg_num_processing == tg->tg_max_concurrency) {
// Remove if we are at max concurrency or there no more tasks to do
TAILQ_REMOVE(&task_groups, tg, tg_link);
}
pthread_mutex_unlock(&task_mutex);
t->t_fn(t->t_opaque);
pthread_mutex_lock(&task_mutex);
free(t);
assert(tg->tg_num_processing > 0);
tg->tg_num_processing--;
if(TAILQ_FIRST(&tg->tg_tasks) != NULL &&
tg->tg_num_processing == tg->tg_max_concurrency - 1) {
TAILQ_INSERT_TAIL(&task_groups, tg, tg_link);
}
// Decrease refcount owned by task
task_group_release(tg);
}
}
num_task_threads--;
if(task_sys_running) {
pthread_detach(tt->tid);
LIST_REMOVE(tt, link);
free(tt);
}
pthread_mutex_unlock(&task_mutex);
return NULL;
}
/**
*
*/
static void
task_launch_thread(void)
{
assert(task_sys_running != 0);
num_task_threads++;
task_thread_t *tt = calloc(1, sizeof(task_thread_t));
LIST_INSERT_HEAD(&task_threads, tt, link);
pthread_create(&tt->tid, NULL, task_thread, tt);
}
/**
*
*/
static void
task_schedule()
{
if(num_task_threads_avail > 0) {
pthread_cond_signal(&task_cond);
} else {
if(num_task_threads < MAX_TASK_THREADS) {
task_launch_thread();
}
}
}
/**
*
*/
void
task_run(task_fn_t *fn, void *opaque)
{
task_t *t = calloc(1, sizeof(task_t));
t->t_fn = fn;
t->t_opaque = opaque;
pthread_mutex_lock(&task_mutex);
TAILQ_INSERT_TAIL(&tasks, t, t_link);
task_schedule();
tasks_enqueued++;
pthread_mutex_unlock(&task_mutex);
}
/**
*
*/
task_group_t *
task_group_create_with_concurrency(int max_concurrency)
{
task_group_t *tg = calloc(1, sizeof(task_group_t));
atomic_set(&tg->tg_refcount, 1);
tg->tg_max_concurrency = max_concurrency;
TAILQ_INIT(&tg->tg_tasks);
return tg;
}
/**
*
*/
task_group_t *
task_group_create(void)
{
return task_group_create_with_concurrency(1);
}
/**
*
*/
void
task_group_destroy(task_group_t *tg)
{
task_group_release(tg);
}
/**
*
*/
void
task_run_in_group(task_fn_t *fn, void *opaque, task_group_t *tg)
{
task_t *t = calloc(1, sizeof(task_t));
t->t_fn = fn;
t->t_opaque = opaque;
pthread_mutex_lock(&task_mutex);
tasks_enqueued++;
if(task_sys_running) {
t->t_group = tg;
atomic_inc(&tg->tg_refcount);
if(TAILQ_FIRST(&tg->tg_tasks) == NULL &&
tg->tg_num_processing < tg->tg_max_concurrency) {
TAILQ_INSERT_TAIL(&task_groups, tg, tg_link);
}
TAILQ_INSERT_TAIL(&tg->tg_tasks, t, t_link);
task_schedule();
} else {
TAILQ_INSERT_TAIL(&tasks, t, t_link);
}
pthread_mutex_unlock(&task_mutex);
}
/**
*
*/
void
task_stop(void)
{
task_thread_t *tt;
pthread_mutex_lock(&task_mutex);
task_sys_running = 0;
pthread_cond_broadcast(&task_cond);
while((tt = LIST_FIRST(&task_threads)) != NULL) {
LIST_REMOVE(tt, link);
pthread_mutex_unlock(&task_mutex);
pthread_join(tt->tid, NULL);
pthread_mutex_lock(&task_mutex);
free(tt);
}
pthread_mutex_unlock(&task_mutex);
}
/**
*
*/
void
task_get_stats(task_stats_t *stats)
{
pthread_mutex_lock(&task_mutex);
stats->num_threads = num_task_threads;
stats->idle_threads = num_task_threads_avail;
stats->tasks_enqueued = tasks_enqueued;
pthread_mutex_unlock(&task_mutex);
}
int
task_system_overload(void)
{
pthread_mutex_lock(&task_mutex);
const int active = num_task_threads - num_task_threads_avail;
pthread_mutex_unlock(&task_mutex);
const int limit = MAX_TASK_THREADS * 3 / 4;
return active > limit;
}