summaryrefslogtreecommitdiff
path: root/evtp.c
blob: 680e957a27c1dd32e4fcb62f8e3c414b484fc874 (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
/*
Copyright (c) 2009 by Juliusz Chroboczek
Copyright (c) 2013 by Yoran Heling

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.
*/

#if defined(EV_CONFIG_H)
#include EV_CONFIG_H
#elif defined(HAVE_CONFIG_H)
#include "config.h"
#endif

#include "evtp.h"

#include <stdlib.h>
#include <errno.h>
#include <assert.h>
#include <pthread.h>


typedef struct evtp_queue_t {
	evtp_work_t *first;
	evtp_work_t *last;
} evtp_queue_t;

struct evtp_t {
	int maxthreads, threads, idle;
	evtp_queue_t scheduled, scheduled_back;
	/* Set when we request that all threads die. */
	int dying;
	/* Protects everything. */
	pthread_mutex_t lock;
	/* Signalled whenever a new continuation is enqueued or dying is set. */
	pthread_cond_t cond;
	/* Signalled whenever a thread dies. */
	pthread_cond_t die_cond;
#if EV_MULTIPLICITY
	struct ev_loop *loop;
#endif
	ev_async async;
};


/* (This was threadpool_run_callbacks()) */
static void evtp_async(EV_P_ ev_async *async, int revents) {
	evtp_t *threadpool = async->data;
	evtp_work_t *items;

	pthread_mutex_lock(&threadpool->lock);
	items = threadpool->scheduled_back.first;
	threadpool->scheduled_back.first = NULL;
	threadpool->scheduled_back.last = NULL;
	pthread_mutex_unlock(&threadpool->lock);

	while(items) {
		evtp_work_t *first = items;
		evtp_func_t func = first->done_func;
		items = items->next;
		/* Don't use 'first' after this function, application may have free()d it. */
		func(first);
		ev_unref(EV_A);
	}
}


evtp_t *evtp_create(EV_P_ int maxthreads) {
	evtp_t *tp = calloc(1, sizeof(evtp_t));
	if(tp == NULL)
		return NULL;

	tp->maxthreads = maxthreads;
	pthread_mutex_init(&tp->lock, NULL);
	pthread_cond_init(&tp->cond, NULL);
	pthread_cond_init(&tp->die_cond, NULL);

	ev_async_init(&tp->async, evtp_async);
	ev_async_start(EV_A_ &tp->async);
	ev_unref(EV_A);
	tp->async.data = tp;
#if EV_MULTIPLICITY
	tp->loop = loop;
#endif

	return tp;
}


int evtp_die(evtp_t *threadpool, int canblock) {
	int done;

	pthread_mutex_lock(&threadpool->lock);

	threadpool->dying = 1;
	pthread_cond_broadcast(&threadpool->cond);

	while(threadpool->threads > 0) {
		if(threadpool->scheduled_back.first || !canblock)
			break;
		pthread_cond_wait(&threadpool->die_cond, &threadpool->lock);
	}

	done = threadpool->threads == 0;

	pthread_mutex_unlock(&threadpool->lock);
	return done;
}


int evtp_destroy(evtp_t *threadpool) {
	int dead;
#if EV_MULTIPLICITY
	struct ev_loop *loop = threadpool->loop;
#endif

	pthread_mutex_lock(&threadpool->lock);
	dead =
		threadpool->threads == 0 &&
		threadpool->scheduled.first == NULL &&
		threadpool->scheduled_back.first == NULL;
	pthread_mutex_unlock(&threadpool->lock);

	if(!dead)
		return -1;

	pthread_cond_destroy(&threadpool->cond);
	pthread_cond_destroy(&threadpool->die_cond);
	pthread_mutex_destroy(&threadpool->lock);
	ev_ref(EV_A);
	ev_async_stop(EV_A_ &threadpool->async);
	free(threadpool);
	return 1;
}


static evtp_work_t *evtp_dequeue(evtp_queue_t *queue) {
    evtp_work_t *item;

    if(queue->first == NULL)
        return NULL;

    item = queue->first;
    queue->first = item->next;
    if(item->next == NULL)
        queue->last = NULL;
    return item;
}


static void evtp_enqueue(evtp_queue_t *queue, evtp_work_t *item) {
    item->next = NULL;
    if(queue->last)
        queue->last->next = item;
    else
        queue->first = item;
    queue->last = item;
}


static void *thread_main(void *pool) {
	evtp_t *threadpool = pool;
	evtp_work_t *item;
#if EV_MULTIPLICITY
	struct ev_loop *loop = threadpool->loop;
#endif

	pthread_mutex_lock(&threadpool->lock);

again:
	if(threadpool->scheduled.first == NULL) {
		struct timespec ts;

		if(threadpool->dying)
			goto die;

		/* Beware when benchmarking.  Under Linux with NPTL, idle threads
		   are slightly counter-productive in some benchmarks, but
		   extremely productive in others. */

		/* This constant may need to be tweaked. */
		if(threadpool->idle >= 2)
			goto die;

		/* Don't bother with POSIX clocks. */
		ts.tv_sec = time(NULL) + 1;
		ts.tv_nsec = 0;

		threadpool->idle++;
		pthread_cond_timedwait(&threadpool->cond, &threadpool->lock, &ts);
		threadpool->idle--;
		if(threadpool->scheduled.first == NULL)
			goto die;
	}

	item = evtp_dequeue(&threadpool->scheduled);
	pthread_mutex_unlock(&threadpool->lock);

	item->work_func(item);

	pthread_mutex_lock(&threadpool->lock);
	evtp_enqueue(&threadpool->scheduled_back, item);
	ev_async_send(EV_A_ &threadpool->async);

	goto again;

die:
	threadpool->threads--;
	pthread_cond_broadcast(&threadpool->die_cond);
	pthread_mutex_unlock(&threadpool->lock);
	return NULL;
}


/* This is called with the pool locked. */
static int evtp_new_thread(evtp_t *threadpool) {
	pthread_t thread;
	pthread_attr_t attr;
	int rc;

	assert(threadpool->threads < threadpool->maxthreads);

	pthread_attr_init(&attr);
	pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
	rc = pthread_create(&thread, &attr, thread_main, (void*)threadpool);
	if(rc) {
		errno = rc;
		return -1;
	}
	threadpool->threads++;
	return 1;
}


int evtp_submit(evtp_work_t *work, evtp_t *threadpool, evtp_func_t work_func, evtp_func_t done_func) {
	int rc = 0;
	int dosignal = 1;
#if EV_MULTIPLICITY
	struct ev_loop *loop = threadpool->loop;
#endif

	work->work_func = work_func;
	work->done_func = done_func;
	work->next = NULL;

	pthread_mutex_lock(&threadpool->lock);
	evtp_enqueue(&threadpool->scheduled, work);
	if(threadpool->idle == 0) {
		dosignal = 0;
		if(threadpool->threads < threadpool->maxthreads) {
			rc = evtp_new_thread(threadpool);
			if(rc < 0 && threadpool->threads > 0)
				rc = 0;             /* we'll recover */
		}
	}
	if(dosignal)
		pthread_cond_signal(&threadpool->cond);
	pthread_mutex_unlock(&threadpool->lock);

	ev_ref(EV_A);

	return rc;
}

/* vim: set noet sw=4 ts=4: */