summaryrefslogtreecommitdiff
path: root/common/tpool.c
blob: f980b86577d4d0cf420578bf1b8b874b03c5799a (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
/*
 * HttpAuth
 *
 * Copyright (C) 2008 Stefan Walter
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * -------------------------------------------------------------------------
 * Ideas from tpool.c - htun thread pool functions
 * Copyright (C) 2002 Moshe Jacobson <moshe@runslinux.net>,
 *                    Ola Nordström <ola@triblock.com>
 * -------------------------------------------------------------------------
 */

/*
 * most of the theory and implementation of the thread pool was taken
 * from the o'reilly pthreads programming book.
 */

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include <assert.h>
#include <signal.h>

#define __USE_GNU
#include <pthread.h>

#include "tpool.h"

typedef struct _tpool_work {
	void (*handler_routine)(void*);
	void *arg;
	struct _tpool_work *next;
} tpool_work;

typedef struct _tpool_thread {
	pthread_t handle;
	int is_active;
	struct _tpool_thread *next;
} tpool_thread;

static int tpool_max_threads = 0;
static int tpool_min_threads = 0;
static int tpool_running_threads = 0;
static int tpool_idle_threads = 0;
static int tpool_zombie_threads = 0;
static tpool_thread *tpool_thread_list = NULL;

static int tpool_max_queue_size = 0;
static int tpool_cur_queue_size = 0;
static tpool_work *tpool_queue_head = NULL;
static tpool_work *tpool_queue_tail = NULL;
static pthread_mutex_t tpool_queue_lock;
static pthread_cond_t tpool_queue_not_full;
static pthread_cond_t tpool_queue_not_empty;
static pthread_cond_t tpool_queue_empty;

static int tpool_queue_closed = 0;
static int tpool_shutdown = 0;
static int tpool_initialized = 0;

#define PTHREQ(x) do { \
	int r = (x); \
	if (r != 0) { \
		fprintf (stderr, "fatal: '%s' failed: %s", #x, strerror (r)); \
		assert (0 && "" #x); \
	} } while (0)

static void*
tpool_thread_func (void *data)
{
	tpool_thread *thread = (tpool_thread*)data;
	tpool_work *work;
	int stop = 0;
	sigset_t set;

	/* We handle these signals on the main thread */
	sigemptyset (&set);
	sigaddset (&set, SIGINT);
	sigaddset (&set, SIGTERM);
	pthread_sigmask (SIG_BLOCK, &set, NULL);

	assert (thread);
	assert (tpool_initialized);

	PTHREQ (pthread_mutex_lock (&tpool_queue_lock));

	assert (thread->is_active);
	++tpool_running_threads;
	++tpool_idle_threads;

	while (!tpool_shutdown && !stop) {

		/* No work to do? */
		while (tpool_cur_queue_size == 0 && !tpool_shutdown && !stop) {

			/* Too many idle threads? */
			if (tpool_running_threads > tpool_min_threads) {
				stop = 1;
				break;
			}

			/*
			 * Sleep until there is work, while asleep the queue_lock
			 * is relinquished
			 */
			PTHREQ (pthread_cond_wait (&tpool_queue_not_empty, &tpool_queue_lock));
		}

		/* Are we shutting down? */
		if (stop || tpool_shutdown)
			break;

		/* Pop the work off the queue */
		work = tpool_queue_head;
		tpool_cur_queue_size--;
		if (tpool_cur_queue_size == 0)
			tpool_queue_head = tpool_queue_tail = NULL;
		else
			tpool_queue_head = work->next;

		/* Broadcast queue state to anyone who's waiting */
		if (tpool_cur_queue_size < tpool_max_queue_size)
			PTHREQ (pthread_cond_broadcast (&tpool_queue_not_full));
		if (tpool_cur_queue_size == 0)
			PTHREQ (pthread_cond_signal (&tpool_queue_empty));

		--tpool_idle_threads;
		assert (tpool_idle_threads >= 0);

		/* Unlock the queue lock while processing */
		PTHREQ (pthread_mutex_unlock (&tpool_queue_lock));

			/* Perform the work */
			(work->handler_routine) (work->arg);
			free (work);

		PTHREQ (pthread_mutex_lock (&tpool_queue_lock));

		++tpool_idle_threads;
	}

	--tpool_idle_threads;
	--tpool_running_threads;
	++tpool_zombie_threads;
	thread->is_active = 0;
	assert (tpool_running_threads >= 0);
	assert (tpool_idle_threads >= 0);

	PTHREQ (pthread_mutex_unlock (&tpool_queue_lock));

	/* So we can double check the result code */
	return thread;
}

static void
free_pool_stuff (void)
{
	tpool_work *work;

	if (!tpool_initialized)
		return;

	/* No threads should be running at this point */
	assert (!tpool_thread_list);

	/* Clean up memory */
	while (tpool_queue_head != NULL) {
		work = tpool_queue_head;
		tpool_queue_head = tpool_queue_head->next;
		free (work);
	}

	pthread_cond_destroy (&tpool_queue_not_full);
	pthread_cond_destroy (&tpool_queue_not_empty);
	pthread_cond_destroy (&tpool_queue_empty);
	pthread_mutex_destroy (&tpool_queue_lock);

	tpool_initialized = 0;
}

int
tpool_init (int max_threads, int min_threads, int max_queued)
{
	assert (!tpool_initialized);

	/* set the desired thread pool values */
	tpool_max_threads = max_threads;
	tpool_running_threads = 0;
	tpool_idle_threads = 0;
	tpool_zombie_threads = 0;
	tpool_min_threads = min_threads;

	/* initialize the work queue */
	tpool_max_queue_size = max_queued;
	tpool_cur_queue_size = 0;
	tpool_queue_head = NULL;
	tpool_queue_tail = NULL;
	tpool_queue_closed = 0;
	tpool_shutdown = 0;

	/* create the mutexs and cond vars */
	PTHREQ (pthread_mutex_init (&tpool_queue_lock, NULL));
	PTHREQ (pthread_cond_init (&tpool_queue_not_empty, NULL));
	PTHREQ (pthread_cond_init (&tpool_queue_not_full, NULL));
	PTHREQ (pthread_cond_init (&tpool_queue_empty, NULL));

	tpool_initialized = 1;
	return 0;
}

static void
join_pool_threads (int all)
{
	tpool_thread *thread, **spot;
	void *exit_code;

	/* Should be called from within the lock */

	for (spot = &tpool_thread_list, thread = *spot;
	     thread && (all || tpool_zombie_threads > 0); ) {

		assert (thread->handle);

		if (all || !thread->is_active) {
			if (!thread->is_active)
				--tpool_zombie_threads;

			/* Join the thread */
			PTHREQ (pthread_join (thread->handle, &exit_code));
			assert (exit_code == thread);

			/* Remove and get the next in list */
			*spot = thread->next;
			free (thread);

		} else {
			/* Just get the next in list */
			spot = &thread->next;
		}

		thread = *spot;
	}
}

int
tpool_add_work (void (*routine)(void*), void *arg)
{
	tpool_work *work;
	tpool_thread *thread;
	int r, ret = -1;

	assert (tpool_initialized);

	PTHREQ (pthread_mutex_lock (&tpool_queue_lock));

	/* Join any old threads */
	join_pool_threads (0);

	/* Should have found all of these above */
	assert (tpool_zombie_threads == 0);

	/* Are we closed for business? */
	if (tpool_shutdown || tpool_queue_closed) {
		errno = ECANCELED;
		goto done;
	}

	/* No idle threads to run this work? */
	if (!tpool_idle_threads) {

		/* See if we can start another thread */
		if (tpool_max_threads <= 0 ||
		    tpool_running_threads < tpool_max_threads) {

			/* The thread context */
			thread = calloc (1, sizeof (tpool_thread));
			if (thread == NULL) {
				errno = ENOMEM;
				goto done;
			}

			/* Start the thread */
			thread->is_active = 1;
			r = pthread_create (&thread->handle, NULL, tpool_thread_func, (void*)thread);
			if (r != 0) {
				errno = r;
				free (thread);
				goto done;
			}

			/* Add the new thread to the list */
			thread->next = tpool_thread_list;
			tpool_thread_list = thread;
		}
	}

	/*
	 * If the queue is full, yield this thread, and see if
	 * another processes something out of the queue.
	 */
	if (tpool_max_queue_size > 0 && tpool_cur_queue_size >= tpool_max_queue_size) {
		PTHREQ (pthread_mutex_unlock (&tpool_queue_lock));
		pthread_yield();
		PTHREQ (pthread_mutex_lock (&tpool_queue_lock));
	}

	/* Are we closed for business? */
	if (tpool_shutdown || tpool_queue_closed) {
		errno = ECANCELED;
		goto done;
	}

	/* Is the queue still full? */
	if (tpool_max_queue_size > 0 && tpool_cur_queue_size >= tpool_max_queue_size) {
		errno = EAGAIN;
		goto done;
	}

	/* Allocate the work structure */
	work = calloc (1, sizeof (tpool_work));
	if (work == NULL) {
		errno = ENOMEM;
		goto done;
	}

	work->handler_routine = routine;
	work->arg = arg;
	work->next = NULL;

	if (tpool_cur_queue_size == 0) {
		tpool_queue_tail = tpool_queue_head = work;
	} else {
		tpool_queue_tail->next = work;
		tpool_queue_tail = work;
	}

	PTHREQ (pthread_cond_broadcast (&tpool_queue_not_empty));
	tpool_cur_queue_size++;
	ret = 0;

done:
	PTHREQ (pthread_mutex_unlock (&tpool_queue_lock));
	return ret;
}

int
tpool_destroy (int finish)
{
	assert (tpool_initialized);

	PTHREQ (pthread_mutex_lock (&tpool_queue_lock));

	/* Can't have two places calling shutdown */
	assert (!tpool_queue_closed && !tpool_shutdown);

	/* close the queue to any new work */
	tpool_queue_closed = 1;

	/* if the finish flag is set, drain the queue */
	if (finish) {
		while (tpool_cur_queue_size != 0)
			PTHREQ (pthread_cond_wait (&tpool_queue_empty, &tpool_queue_lock));
	}

	/* Set the shutdown flag */
	tpool_shutdown = 1;

	PTHREQ (pthread_mutex_unlock (&tpool_queue_lock));

	/* wake up all workers to rechedk the shutdown flag */
	PTHREQ (pthread_cond_broadcast (&tpool_queue_not_empty));
	PTHREQ (pthread_cond_broadcast (&tpool_queue_not_full));

	join_pool_threads (1);
	free_pool_stuff ();

	return 0;
}