summaryrefslogtreecommitdiff
path: root/main/threadpool.c
blob: 7b7df4e682aa53e3b6fa0de6dd5a6745b2fadb8a (plain)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2012, Digium, Inc.
 *
 * Mark Michelson <mmmichelson@digium.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */


#include "asterisk.h"

#include "asterisk/threadpool.h"
#include "asterisk/taskprocessor.h"
#include "asterisk/astobj2.h"
#include "asterisk/utils.h"

#define THREAD_BUCKETS 89

static int id_counter;

struct ast_threadpool {
	struct ast_threadpool_listener *listener;
	struct ao2_container *active_threads;
	struct ao2_container *idle_threads;
	struct ao2_container *zombie_threads;
	struct ast_taskprocessor *tps;
	struct ast_taskprocessor *control_tps;
};

enum worker_state {
	ALIVE,
	ZOMBIE,
	DEAD,
};

struct worker_thread {
	int id;
	ast_cond_t cond;
	ast_mutex_t lock;
	pthread_t thread;
	struct ast_threadpool *pool;
	enum worker_state state;
	int wake_up;
};

static int worker_thread_hash(const void *obj, int flags)
{
	const struct worker_thread *worker = obj;

	return worker->id;
}

static int worker_thread_cmp(void *obj, void *arg, int flags)
{
	struct worker_thread *worker1 = obj;
	struct worker_thread *worker2 = arg;

	return worker1->id == worker2->id ? CMP_MATCH : 0;
}

static void worker_thread_destroy(void *obj)
{
	struct worker_thread *worker = obj;
	ast_mutex_destroy(&worker->lock);
	ast_cond_destroy(&worker->cond);
}

static int worker_active(struct worker_thread *worker);

static void *worker_start(void *arg)
{
	struct worker_thread *worker = arg;

	worker_active(worker);
	return NULL;
}

static struct worker_thread *worker_thread_alloc(struct ast_threadpool *pool)
{
	struct worker_thread *worker = ao2_alloc(sizeof(*worker), worker_thread_destroy);
	if (!worker) {
		/* XXX Dangit! */
		return NULL;
	}
	worker->id = ast_atomic_fetchadd_int(&id_counter, 1);
	ast_mutex_init(&worker->lock);
	ast_cond_init(&worker->cond, NULL);
	worker->pool = pool;
	worker->thread = AST_PTHREADT_NULL;
	worker->state = ALIVE;
	if (ast_pthread_create(&worker->thread, NULL, worker_start, worker) < 0) {
		/* XXX Poop! */
		ao2_ref(worker, -1);
		return NULL;
	}
	return worker;
}

static void threadpool_send_state_changed(struct ast_threadpool *pool)
{
	int active_size = ao2_container_count(pool->active_threads);
	int idle_size = ao2_container_count(pool->idle_threads);
	int zombie_size = ao2_container_count(pool->zombie_threads);

	pool->listener->callbacks->state_changed(pool->listener, active_size, idle_size, zombie_size);
}

struct thread_worker_pair {
	struct ast_threadpool *pool;
	struct worker_thread *worker;
};

static void thread_worker_pair_destructor(void *obj)
{
	struct thread_worker_pair *pair = obj;
	ao2_ref(pair->pool, -1);
	ao2_ref(pair->worker, -1);
}

static struct thread_worker_pair *thread_worker_pair_init(struct ast_threadpool *pool,
		struct worker_thread *worker)
{
	struct thread_worker_pair *pair = ao2_alloc(sizeof(*pair), thread_worker_pair_destructor);
	if (!pair) {
		/*XXX Crap */
		return NULL;
	}
	ao2_ref(pool, +1);
	pair->pool = pool;
	ao2_ref(worker, +1);
	pair->worker = worker;
	return pair;
}

static int queued_active_thread_idle(void *data)
{
	struct thread_worker_pair *pair = data;

	ao2_link(pair->pool->idle_threads, pair->worker);
	ao2_unlink(pair->pool->active_threads, pair->worker);

	threadpool_send_state_changed(pair->pool);

	ao2_ref(pair, -1);
	return 0;
}

static void threadpool_active_thread_idle(struct ast_threadpool *pool,
		struct worker_thread *worker)
{
	struct thread_worker_pair *pair = thread_worker_pair_init(pool, worker);
	if (!pair) {
		/*XXX Crap */
		return;
	}
	ast_taskprocessor_push(pool->control_tps, queued_active_thread_idle, pair);
}

static int queued_zombie_thread_dead(void *data)
{
	struct thread_worker_pair *pair = data;

	ao2_unlink(pair->pool->zombie_threads, pair->worker);
	threadpool_send_state_changed(pair->pool);

	ao2_ref(pair, -1);
	return 0;
}

static void threadpool_zombie_thread_dead(struct ast_threadpool *pool,
		struct worker_thread *worker)
{
	struct thread_worker_pair *pair = thread_worker_pair_init(pool, worker);
	if (!pair) {
		/* XXX Crap */
		return;
	}
	ast_taskprocessor_push(pool->control_tps, queued_zombie_thread_dead, pair);
}

static int worker_idle(struct worker_thread *worker)
{
	SCOPED_MUTEX(lock, &worker->lock);
	if (worker->state != ALIVE) {
		return 0;
	}
	threadpool_active_thread_idle(worker->pool, worker);
	while (!worker->wake_up) {
		ast_cond_wait(&worker->cond, lock);
	}
	worker->wake_up = 0;
	return worker->state == ALIVE;
}

static int threadpool_execute(struct ast_threadpool *pool)
{
	return ast_taskprocessor_execute(pool->tps);
}

static int worker_active(struct worker_thread *worker)
{
	int alive = 1;
	while (alive) {
		if (threadpool_execute(worker->pool)) {
			alive = worker_idle(worker);
		}
	}

	/* Reaching this portion means the thread is
	 * on death's door. It may have been killed while
	 * it was idle, in which case it can just die
	 * peacefully. If it's a zombie, though, then
	 * it needs to let the pool know so
	 * that the thread can be removed from the
	 * list of zombie threads.
	 */
	if (worker->state == ZOMBIE) {
		threadpool_zombie_thread_dead(worker->pool, worker);
	}

	return 0;
}

static void worker_set_state(struct worker_thread *worker, enum worker_state state)
{
	SCOPED_MUTEX(lock, &worker->lock);
	worker->state = state;
	worker->wake_up = 1;
	ast_cond_signal(&worker->cond);
}

static int worker_shutdown(void *obj, void *arg, int flags)
{
	struct worker_thread *worker = obj;

	worker_set_state(worker, DEAD);
	if (worker->thread != AST_PTHREADT_NULL) {
		pthread_join(worker->thread, NULL);
		worker->thread = AST_PTHREADT_NULL;
	}
	return 0;
}

static void threadpool_tps_listener_destroy(void *private_data)
{
	struct ast_threadpool *pool = private_data;
	/* XXX Probably should let the listener know we're being destroyed? */

	/* Threads should all be shut down by now, so this should be a painless
	 * operation
	 */
	ao2_ref(pool->active_threads, -1);
	ao2_ref(pool->idle_threads, -1);
	ao2_ref(pool->zombie_threads, -1);
	ao2_ref(pool->listener, -1);
	ao2_ref(pool, -1);
}

static void *threadpool_tps_listener_alloc(struct ast_taskprocessor_listener *listener)
{
	RAII_VAR(struct ast_threadpool *, pool,
			ao2_alloc(sizeof(*pool), threadpool_tps_listener_destroy), ao2_cleanup);

	pool->control_tps = ast_taskprocessor_get("CHANGE THIS", TPS_REF_DEFAULT);
	if (!pool->control_tps) {
		return NULL;
	}
	pool->active_threads = ao2_container_alloc(THREAD_BUCKETS, worker_thread_hash, worker_thread_cmp);
	if (!pool->active_threads) {
		return NULL;
	}
	pool->idle_threads = ao2_container_alloc(THREAD_BUCKETS, worker_thread_hash, worker_thread_cmp);
	if (!pool->idle_threads) {
		return NULL;
	}
	pool->zombie_threads = ao2_container_alloc(THREAD_BUCKETS, worker_thread_hash, worker_thread_cmp);
	if (!pool->zombie_threads) {
		return NULL;
	}

	pool->tps = listener->tps;

	ao2_ref(pool, +1);
	return pool;
}

struct task_pushed_data {
	struct ast_threadpool *pool;
	int was_empty;
};

static void task_pushed_data_destroy(void *obj)
{
	struct task_pushed_data *tpd = obj;
	ao2_ref(tpd->pool, -1);
}

static struct task_pushed_data *task_pushed_data_alloc(struct ast_threadpool *pool,
		int was_empty)
{
	struct task_pushed_data *tpd = ao2_alloc(sizeof(*tpd),
			task_pushed_data_destroy);

	if (!tpd) {
		return NULL;
	}
	ao2_ref(pool, +1);
	tpd->pool = pool;
	tpd->was_empty = was_empty;
	return tpd;
}

static int activate_threads(void *obj, void *arg, int flags)
{
	struct worker_thread *worker = obj;
	struct ast_threadpool *pool = arg;

	ao2_link(pool->active_threads, worker);
	worker_set_state(worker, ALIVE);
	return 0;
}

static int handle_task_pushed(void *data)
{
	struct task_pushed_data *tpd = data;
	struct ast_threadpool *pool = tpd->pool;
	int was_empty = tpd->was_empty;

	pool->listener->callbacks->tps_task_pushed(pool->listener, was_empty);
	ao2_callback(pool->idle_threads, OBJ_UNLINK, activate_threads, pool);
	ao2_ref(tpd, -1);
	return 0;
}

static void threadpool_tps_task_pushed(struct ast_taskprocessor_listener *listener,
		int was_empty)
{
	struct ast_threadpool *pool = listener->private_data;
	struct task_pushed_data *tpd = task_pushed_data_alloc(pool, was_empty);

	if (!tpd) {
		/* XXX Drat! */
		return;
	}

	ast_taskprocessor_push(pool->control_tps, handle_task_pushed, tpd);
}

static int handle_emptied(void *data)
{
	struct ast_threadpool *pool = data;

	pool->listener->callbacks->emptied(pool->listener);
	ao2_ref(pool, -1);
	return 0;
}

static void threadpool_tps_emptied(struct ast_taskprocessor_listener *listener)
{
	struct ast_threadpool *pool = listener->private_data;

	ao2_ref(pool, +1);
	ast_taskprocessor_push(pool->control_tps, handle_emptied, pool);
}

static void threadpool_tps_shutdown(struct ast_taskprocessor_listener *listener)
{
	/*
	 * The threadpool triggers the taskprocessor to shut down. As a result,
	 * we have the freedom of shutting things down in three stages:
	 *
	 * 1) Before the tasprocessor is shut down
	 * 2) During taskprocessor shutdown (here)
	 * 3) After taskprocessor shutdown
	 *
	 * In the spirit of the taskprocessor shutdown, this would be
	 * where we make sure that all the worker threads are no longer
	 * executing. We could just do this before we even shut down
	 * the taskprocessor, but this feels more "right".
	 */

	struct ast_threadpool *pool = listener->private_data;
	ao2_callback(pool->active_threads, 0, worker_shutdown, NULL);
	ao2_callback(pool->idle_threads, 0, worker_shutdown, NULL);
	ao2_callback(pool->zombie_threads, 0, worker_shutdown, NULL);
}

static struct ast_taskprocessor_listener_callbacks threadpool_tps_listener_callbacks = {
	.alloc = threadpool_tps_listener_alloc,
	.task_pushed = threadpool_tps_task_pushed,
	.emptied = threadpool_tps_emptied,
	.shutdown = threadpool_tps_shutdown,
	.destroy = threadpool_tps_listener_destroy,
};

static void grow(struct ast_threadpool *pool, int delta)
{
	int i;
	for (i = 0; i < delta; ++i) {
		struct worker_thread *worker = worker_thread_alloc(pool);
		if (!worker) {
			/* XXX Abandon */
			return;
		}
		ao2_link(pool->active_threads, worker);
	}
}

static int kill_threads(void *obj, void *arg, int flags)
{
	struct worker_thread *worker = obj;
	int *num_to_kill = arg;

	if ((*num_to_kill)-- > 0) {
		worker_shutdown(worker, arg, flags);
		return CMP_MATCH;
	} else {
		return CMP_STOP;
	}
}

static int zombify_threads(void *obj, void *arg, void *data, int flags)
{
	struct worker_thread *worker = obj;
	struct ast_threadpool *pool = arg;
	int *num_to_zombify = data;

	if ((*num_to_zombify)-- > 0) {
		ao2_link(pool->zombie_threads, worker);
		worker_set_state(worker, ZOMBIE);
		return CMP_MATCH;
	} else {
		return CMP_STOP;
	}
}

static void shrink(struct ast_threadpool *pool, int delta)
{
	/* 
	 * Preference is to kill idle threads, but
	 * we'll move on to deactivating active threads
	 * if we have to
	 */
	int idle_threads = ao2_container_count(pool->idle_threads);
	int idle_threads_to_kill = MIN(delta, idle_threads);
	int active_threads_to_zombify = delta - idle_threads_to_kill;

	ao2_callback(pool->idle_threads, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK,
			kill_threads, &idle_threads_to_kill);

	ao2_callback_data(pool->active_threads, OBJ_UNLINK | OBJ_NODATA | OBJ_MULTIPLE | OBJ_NOLOCK,
			zombify_threads, pool, &active_threads_to_zombify);
}

struct set_size_data {
	struct ast_threadpool *pool;
	int size;
};

static void set_size_data_destroy(void *obj)
{
	struct set_size_data *ssd = obj;
	ao2_ref(ssd->pool, -1);
}

static struct set_size_data *set_size_data_alloc(struct ast_threadpool *pool,
		int size)
{
	struct set_size_data *ssd = ao2_alloc(sizeof(*ssd), set_size_data_destroy);
	if (!ssd) {
		/* XXX Crap */
		return NULL;
	}

	ao2_ref(pool, +1);
	ssd->pool = pool;
	ssd->size = size;
	return ssd;
}

static int queued_set_size(void *data)
{
	struct set_size_data *ssd = data;
	struct ast_threadpool *pool = ssd->pool;
	int num_threads = ssd->size;

	/* We don't count zombie threads as being "live when potentially resizing */
	int current_size = ao2_container_count(pool->active_threads) +
		ao2_container_count(pool->idle_threads);

	if (current_size == num_threads) {
		return 0;
	}

	if (current_size < num_threads) {
		grow(pool, num_threads - current_size);
	} else {
		shrink(pool, current_size - num_threads);
	}

	threadpool_send_state_changed(pool);
	ao2_ref(ssd, -1);
	return 0;
}

void ast_threadpool_set_size(struct ast_threadpool *pool, unsigned int size)
{
	struct set_size_data *ssd;

	ssd = set_size_data_alloc(pool, size);
	if (!ssd) {
		/* XXX *groan* */
		return;
	}

	ast_taskprocessor_push(pool->control_tps, queued_set_size, ssd);
}

struct ast_threadpool *ast_threadpool_create(struct ast_threadpool_listener *listener, int initial_size)
{
	struct ast_threadpool *pool;
	struct ast_taskprocessor *tps;
	RAII_VAR(struct ast_taskprocessor_listener *, tps_listener,
			ast_taskprocessor_listener_alloc(&threadpool_tps_listener_callbacks),
			ao2_cleanup);

	if (!tps_listener) {
		return NULL;
	}

	tps = ast_taskprocessor_create_with_listener("XXX CHANGE THIS XXX", tps_listener);

	if (!tps) {
		return NULL;
	}

	pool = tps_listener->private_data;
	ast_threadpool_set_size(pool, initial_size);
	return pool;
}

void ast_threadpool_shutdown(struct ast_threadpool *pool)
{
	/* Pretty simple really. We just shut down the
	 * taskprocessors and everything else just
	 * takes care of itself via the taskprocessor callbacks
	 */
	ast_taskprocessor_unreference(pool->control_tps);
	ast_taskprocessor_unreference(pool->tps);
}