1 /* worker thread pool based on POSIX threads
2 * author: John Tsiombikas <nuclear@member.fsf.org>
3 * This code is public domain.
11 #if defined(__APPLE__) && defined(__MACH__)
20 #if defined(unix) || defined(__unix__)
25 # include <sys/sysctl.h>
29 #if defined(WIN32) || defined(__WIN32__)
36 tpool_callback work, done;
37 struct work_item *next;
42 struct thread_pool *pool;
47 struct thread_data *tdata;
52 struct work_item *workq, *workq_tail;
53 pthread_mutex_t workq_mutex;
54 pthread_cond_t workq_condvar;
56 int nactive; /* number of active workers (not sleeping) */
58 pthread_cond_t done_condvar;
63 int nref; /* reference count */
65 #if defined(WIN32) || defined(__WIN32__)
72 static void *thread_func(void *args);
73 static void send_done_event(struct thread_pool *tpool);
75 static struct work_item *alloc_work_item(void);
76 static void free_work_item(struct work_item *w);
79 struct thread_pool *tpool_create(int num_threads)
82 struct thread_pool *tpool;
84 if(!(tpool = calloc(1, sizeof *tpool))) {
87 pthread_mutex_init(&tpool->workq_mutex, 0);
88 pthread_cond_init(&tpool->workq_condvar, 0);
89 pthread_cond_init(&tpool->done_condvar, 0);
90 pthread_key_create(&tpool->idkey, 0);
92 pthread_setspecific(tpool->idkey, (void*)0xffffffff);
94 #if !defined(WIN32) && !defined(__WIN32__)
95 tpool->wait_pipe[0] = tpool->wait_pipe[1] = -1;
98 if(num_threads <= 0) {
99 num_threads = tpool_num_processors();
101 tpool->num_threads = num_threads;
103 if(!(tpool->threads = calloc(num_threads, sizeof *tpool->threads))) {
107 if(!(tpool->tdata = malloc(num_threads * sizeof *tpool->tdata))) {
108 free(tpool->threads);
113 for(i=0; i<num_threads; i++) {
114 tpool->tdata[i].id = i;
115 tpool->tdata[i].pool = tpool;
117 if(pthread_create(tpool->threads + i, 0, thread_func, tpool->tdata + i) == -1) {
118 /*tpool->threads[i] = 0;*/
119 tpool_destroy(tpool);
126 void tpool_destroy(struct thread_pool *tpool)
132 tpool->should_quit = 1;
134 pthread_cond_broadcast(&tpool->workq_condvar);
137 for(i=0; i<tpool->num_threads; i++) {
138 pthread_join(tpool->threads[i], 0);
141 free(tpool->threads);
145 /* also wake up anyone waiting on the wait* calls */
147 pthread_cond_broadcast(&tpool->done_condvar);
148 send_done_event(tpool);
150 pthread_mutex_destroy(&tpool->workq_mutex);
151 pthread_cond_destroy(&tpool->workq_condvar);
152 pthread_cond_destroy(&tpool->done_condvar);
153 pthread_key_delete(tpool->idkey);
155 #if defined(WIN32) || defined(__WIN32__)
156 if(tpool->wait_event) {
157 CloseHandle(tpool->wait_event);
160 if(tpool->wait_pipe[0] >= 0) {
161 close(tpool->wait_pipe[0]);
162 close(tpool->wait_pipe[1]);
167 int tpool_addref(struct thread_pool *tpool)
169 return ++tpool->nref;
172 int tpool_release(struct thread_pool *tpool)
174 if(--tpool->nref <= 0) {
175 tpool_destroy(tpool);
181 void tpool_begin_batch(struct thread_pool *tpool)
186 void tpool_end_batch(struct thread_pool *tpool)
189 pthread_cond_broadcast(&tpool->workq_condvar);
192 int tpool_enqueue(struct thread_pool *tpool, void *data,
193 tpool_callback work_func, tpool_callback done_func)
195 struct work_item *job;
197 if(!(job = alloc_work_item())) {
200 job->work = work_func;
201 job->done = done_func;
205 pthread_mutex_lock(&tpool->workq_mutex);
207 tpool->workq_tail->next = job;
208 tpool->workq_tail = job;
210 tpool->workq = tpool->workq_tail = job;
213 pthread_mutex_unlock(&tpool->workq_mutex);
215 if(!tpool->in_batch) {
216 pthread_cond_broadcast(&tpool->workq_condvar);
221 void tpool_clear(struct thread_pool *tpool)
223 pthread_mutex_lock(&tpool->workq_mutex);
224 while(tpool->workq) {
225 void *tmp = tpool->workq;
226 tpool->workq = tpool->workq->next;
229 tpool->workq = tpool->workq_tail = 0;
231 pthread_mutex_unlock(&tpool->workq_mutex);
234 int tpool_queued_jobs(struct thread_pool *tpool)
237 pthread_mutex_lock(&tpool->workq_mutex);
239 pthread_mutex_unlock(&tpool->workq_mutex);
243 int tpool_active_jobs(struct thread_pool *tpool)
246 pthread_mutex_lock(&tpool->workq_mutex);
247 res = tpool->nactive;
248 pthread_mutex_unlock(&tpool->workq_mutex);
252 int tpool_pending_jobs(struct thread_pool *tpool)
255 pthread_mutex_lock(&tpool->workq_mutex);
256 res = tpool->qsize + tpool->nactive;
257 pthread_mutex_unlock(&tpool->workq_mutex);
261 void tpool_wait(struct thread_pool *tpool)
263 pthread_mutex_lock(&tpool->workq_mutex);
264 while(tpool->nactive || tpool->qsize) {
265 pthread_cond_wait(&tpool->done_condvar, &tpool->workq_mutex);
267 pthread_mutex_unlock(&tpool->workq_mutex);
270 void tpool_wait_pending(struct thread_pool *tpool, int pending_target)
272 pthread_mutex_lock(&tpool->workq_mutex);
273 while(tpool->qsize + tpool->nactive > pending_target) {
274 pthread_cond_wait(&tpool->done_condvar, &tpool->workq_mutex);
276 pthread_mutex_unlock(&tpool->workq_mutex);
279 #if defined(WIN32) || defined(__WIN32__)
280 long tpool_timedwait(struct thread_pool *tpool, long timeout)
282 fprintf(stderr, "tpool_timedwait currently unimplemented on windows\n");
287 /* TODO: actually does this work with MinGW ? */
288 int tpool_get_wait_fd(struct thread_pool *tpool)
293 fprintf(stderr, "warning: tpool_get_wait_fd call on Windows does nothing\n");
298 void *tpool_get_wait_handle(struct thread_pool *tpool)
300 if(!tpool->wait_event) {
301 if(!(tpool->wait_event = CreateEvent(0, 0, 0, 0))) {
305 return tpool->wait_event;
308 static void send_done_event(struct thread_pool *tpool)
310 if(tpool->wait_event) {
311 SetEvent(tpool->wait_event);
317 long tpool_timedwait(struct thread_pool *tpool, long timeout)
319 struct timespec tout_ts;
320 struct timeval tv0, tv;
321 gettimeofday(&tv0, 0);
323 long sec = timeout / 1000;
324 tout_ts.tv_nsec = tv0.tv_usec * 1000 + (timeout % 1000) * 1000000;
325 tout_ts.tv_sec = tv0.tv_sec + sec;
327 pthread_mutex_lock(&tpool->workq_mutex);
328 while(tpool->nactive || tpool->qsize) {
329 if(pthread_cond_timedwait(&tpool->done_condvar,
330 &tpool->workq_mutex, &tout_ts) == ETIMEDOUT) {
334 pthread_mutex_unlock(&tpool->workq_mutex);
336 gettimeofday(&tv, 0);
337 return (tv.tv_sec - tv0.tv_sec) * 1000 + (tv.tv_usec - tv0.tv_usec) / 1000;
340 int tpool_get_wait_fd(struct thread_pool *tpool)
342 if(tpool->wait_pipe[0] < 0) {
343 if(pipe(tpool->wait_pipe) == -1) {
347 return tpool->wait_pipe[0];
350 void *tpool_get_wait_handle(struct thread_pool *tpool)
355 fprintf(stderr, "warning: tpool_get_wait_handle call on UNIX does nothing\n");
360 static void send_done_event(struct thread_pool *tpool)
362 if(tpool->wait_pipe[1] >= 0) {
363 write(tpool->wait_pipe[1], tpool, 1);
366 #endif /* WIN32/UNIX */
368 static void *thread_func(void *args)
370 struct thread_data *tdata = args;
371 struct thread_pool *tpool = tdata->pool;
373 pthread_setspecific(tpool->idkey, (void*)(intptr_t)tdata->id);
375 pthread_mutex_lock(&tpool->workq_mutex);
376 while(!tpool->should_quit) {
378 pthread_cond_wait(&tpool->workq_condvar, &tpool->workq_mutex);
379 if(tpool->should_quit) break;
382 while(!tpool->should_quit && tpool->workq) {
383 /* grab the first job */
384 struct work_item *job = tpool->workq;
385 tpool->workq = tpool->workq->next;
387 tpool->workq_tail = 0;
390 pthread_mutex_unlock(&tpool->workq_mutex);
393 job->work(job->data);
395 job->done(job->data);
399 pthread_mutex_lock(&tpool->workq_mutex);
400 /* notify everyone interested that we're done with this job */
401 pthread_cond_broadcast(&tpool->done_condvar);
402 send_done_event(tpool);
406 pthread_mutex_unlock(&tpool->workq_mutex);
412 int tpool_thread_id(struct thread_pool *tpool)
414 int id = (intptr_t)pthread_getspecific(tpool->idkey);
415 if(id >= tpool->num_threads) {
422 /* The following highly platform-specific code detects the number
423 * of processors available in the system. It's used by the thread pool
424 * to autodetect how many threads to spawn.
425 * Currently works on: Linux, BSD, Darwin, and Windows.
427 int tpool_num_processors(void)
429 #if defined(unix) || defined(__unix__)
430 # if defined(__bsd__)
431 /* BSD systems provide the num.processors through sysctl */
432 int num, mib[] = {CTL_HW, HW_NCPU};
433 size_t len = sizeof num;
435 sysctl(mib, 2, &num, &len, 0, 0);
438 # elif defined(__sgi)
439 /* SGI IRIX flavour of the _SC_NPROC_ONLN sysconf */
440 return sysconf(_SC_NPROC_ONLN);
442 /* Linux (and others?) have the _SC_NPROCESSORS_ONLN sysconf */
443 return sysconf(_SC_NPROCESSORS_ONLN);
444 # endif /* bsd/sgi/other */
446 #elif defined(WIN32) || defined(__WIN32__)
447 /* under windows we need to call GetSystemInfo */
449 GetSystemInfo(&info);
450 return info.dwNumberOfProcessors;
454 #define MAX_WPOOL_SIZE 64
455 static pthread_mutex_t wpool_lock = PTHREAD_MUTEX_INITIALIZER;
456 static struct work_item *wpool;
457 static int wpool_size;
459 /* work item allocator */
460 static struct work_item *alloc_work_item(void)
464 pthread_mutex_lock(&wpool_lock);
466 pthread_mutex_unlock(&wpool_lock);
467 return malloc(sizeof(struct work_item));
473 pthread_mutex_unlock(&wpool_lock);
477 static void free_work_item(struct work_item *w)
479 pthread_mutex_lock(&wpool_lock);
480 if(wpool_size >= MAX_WPOOL_SIZE) {
487 pthread_mutex_unlock(&wpool_lock);