2 * Unix SMB/CIFS implementation.
3 * thread pool implementation
4 * Copyright (C) Volker Lendecke 2009
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "system/time.h"
22 #include "system/wait.h"
23 #include "system/threads.h"
24 #include "pthreadpool.h"
25 #include "lib/util/dlinklist.h"
33 struct pthreadpool_job {
35 void (*fn)(void *private_data);
41 * List pthreadpools for fork safety
43 struct pthreadpool *prev, *next;
46 * Control access to this struct
48 pthread_mutex_t mutex;
51 * Threads waiting for work do so here
53 pthread_cond_t condvar;
58 size_t jobs_array_len;
59 struct pthreadpool_job *jobs;
65 * Indicate job completion
67 int (*signal_fn)(int jobid,
68 void (*job_fn)(void *private_data),
69 void *job_fn_private_data,
71 void *signal_fn_private_data;
74 * indicator to worker threads that they should shut down
79 * maximum number of threads
80 * 0 means no real thread, only strict sync processing.
90 * Number of idle threads
95 * Condition variable indicating that helper threads should
96 * quickly go away making way for fork() without anybody
97 * waiting on pool->condvar.
99 pthread_cond_t *prefork_cond;
102 * Waiting position for helper threads while fork is
103 * running. The forking thread will have locked it, and all
104 * idle helper threads will sit here until after the fork,
105 * where the forking thread will unlock it again.
107 pthread_mutex_t fork_mutex;
110 static pthread_mutex_t pthreadpools_mutex = PTHREAD_MUTEX_INITIALIZER;
111 static struct pthreadpool *pthreadpools = NULL;
112 static pthread_once_t pthreadpool_atfork_initialized = PTHREAD_ONCE_INIT;
114 static void pthreadpool_prep_atfork(void);
117 * Initialize a thread pool
120 int pthreadpool_init(unsigned max_threads, struct pthreadpool **presult,
121 int (*signal_fn)(int jobid,
122 void (*job_fn)(void *private_data),
123 void *job_fn_private_data,
125 void *signal_fn_private_data)
127 struct pthreadpool *pool;
130 pool = (struct pthreadpool *)malloc(sizeof(struct pthreadpool));
134 pool->signal_fn = signal_fn;
135 pool->signal_fn_private_data = signal_fn_private_data;
137 pool->jobs_array_len = 4;
139 pool->jobs_array_len, sizeof(struct pthreadpool_job));
141 if (pool->jobs == NULL) {
146 pool->head = pool->num_jobs = 0;
148 ret = pthread_mutex_init(&pool->mutex, NULL);
155 ret = pthread_cond_init(&pool->condvar, NULL);
157 pthread_mutex_destroy(&pool->mutex);
163 ret = pthread_mutex_init(&pool->fork_mutex, NULL);
165 pthread_cond_destroy(&pool->condvar);
166 pthread_mutex_destroy(&pool->mutex);
172 pool->shutdown = false;
173 pool->num_threads = 0;
174 pool->max_threads = max_threads;
176 pool->prefork_cond = NULL;
178 ret = pthread_mutex_lock(&pthreadpools_mutex);
180 pthread_mutex_destroy(&pool->fork_mutex);
181 pthread_cond_destroy(&pool->condvar);
182 pthread_mutex_destroy(&pool->mutex);
187 DLIST_ADD(pthreadpools, pool);
189 ret = pthread_mutex_unlock(&pthreadpools_mutex);
192 pthread_once(&pthreadpool_atfork_initialized, pthreadpool_prep_atfork);
199 static void pthreadpool_prepare_pool(struct pthreadpool *pool)
203 ret = pthread_mutex_lock(&pool->fork_mutex);
206 ret = pthread_mutex_lock(&pool->mutex);
209 while (pool->num_idle != 0) {
210 unsigned num_idle = pool->num_idle;
211 pthread_cond_t prefork_cond;
213 ret = pthread_cond_init(&prefork_cond, NULL);
217 * Push all idle threads off pool->condvar. In the
218 * child we can destroy the pool, which would result
219 * in undefined behaviour in the
220 * pthread_cond_destroy(pool->condvar). glibc just
223 pool->prefork_cond = &prefork_cond;
225 ret = pthread_cond_signal(&pool->condvar);
228 while (pool->num_idle == num_idle) {
229 ret = pthread_cond_wait(&prefork_cond, &pool->mutex);
233 pool->prefork_cond = NULL;
235 ret = pthread_cond_destroy(&prefork_cond);
240 * Probably it's well-defined somewhere: What happens to
241 * condvars after a fork? The rationale of pthread_atfork only
242 * writes about mutexes. So better be safe than sorry and
243 * destroy/reinit pool->condvar across a fork.
246 ret = pthread_cond_destroy(&pool->condvar);
250 static void pthreadpool_prepare(void)
253 struct pthreadpool *pool;
255 ret = pthread_mutex_lock(&pthreadpools_mutex);
260 while (pool != NULL) {
261 pthreadpool_prepare_pool(pool);
266 static void pthreadpool_parent(void)
269 struct pthreadpool *pool;
271 for (pool = DLIST_TAIL(pthreadpools);
273 pool = DLIST_PREV(pool)) {
274 ret = pthread_cond_init(&pool->condvar, NULL);
276 ret = pthread_mutex_unlock(&pool->mutex);
278 ret = pthread_mutex_unlock(&pool->fork_mutex);
282 ret = pthread_mutex_unlock(&pthreadpools_mutex);
286 static void pthreadpool_child(void)
289 struct pthreadpool *pool;
291 for (pool = DLIST_TAIL(pthreadpools);
293 pool = DLIST_PREV(pool)) {
295 pool->num_threads = 0;
300 ret = pthread_cond_init(&pool->condvar, NULL);
303 ret = pthread_mutex_unlock(&pool->mutex);
306 ret = pthread_mutex_unlock(&pool->fork_mutex);
310 ret = pthread_mutex_unlock(&pthreadpools_mutex);
314 static void pthreadpool_prep_atfork(void)
316 pthread_atfork(pthreadpool_prepare, pthreadpool_parent,
320 static int pthreadpool_free(struct pthreadpool *pool)
324 ret = pthread_mutex_lock(&pthreadpools_mutex);
328 DLIST_REMOVE(pthreadpools, pool);
329 ret = pthread_mutex_unlock(&pthreadpools_mutex);
332 ret = pthread_mutex_destroy(&pool->mutex);
333 ret1 = pthread_cond_destroy(&pool->condvar);
334 ret2 = pthread_mutex_destroy(&pool->fork_mutex);
353 * Destroy a thread pool. Wake up all idle threads for exit. The last
354 * one will free the pool.
357 int pthreadpool_destroy(struct pthreadpool *pool)
361 ret = pthread_mutex_lock(&pool->mutex);
366 if (pool->shutdown) {
367 ret = pthread_mutex_unlock(&pool->mutex);
372 pool->shutdown = true;
374 if (pool->num_threads == 0) {
375 ret = pthread_mutex_unlock(&pool->mutex);
378 ret = pthreadpool_free(pool);
383 * We have active threads, tell them to finish.
386 ret = pthread_cond_broadcast(&pool->condvar);
388 ret1 = pthread_mutex_unlock(&pool->mutex);
395 * Prepare for pthread_exit(), pool->mutex must be locked and will be
396 * unlocked here. This is a bit of a layering violation, but here we
397 * also take care of removing the pool if we're the last thread.
399 static void pthreadpool_server_exit(struct pthreadpool *pool)
404 pool->num_threads -= 1;
406 free_it = (pool->shutdown && (pool->num_threads == 0));
408 ret = pthread_mutex_unlock(&pool->mutex);
412 pthreadpool_free(pool);
416 static bool pthreadpool_get_job(struct pthreadpool *p,
417 struct pthreadpool_job *job)
419 if (p->num_jobs == 0) {
422 *job = p->jobs[p->head];
423 p->head = (p->head+1) % p->jobs_array_len;
428 static bool pthreadpool_put_job(struct pthreadpool *p,
430 void (*fn)(void *private_data),
433 struct pthreadpool_job *job;
435 if (p->num_jobs == p->jobs_array_len) {
436 struct pthreadpool_job *tmp;
437 size_t new_len = p->jobs_array_len * 2;
440 p->jobs, sizeof(struct pthreadpool_job) * new_len);
447 * We just doubled the jobs array. The array implements a FIFO
448 * queue with a modulo-based wraparound, so we have to memcpy
449 * the jobs that are logically at the queue end but physically
450 * before the queue head into the reallocated area. The new
451 * space starts at the current jobs_array_len, and we have to
452 * copy everything before the current head job into the new
455 memcpy(&p->jobs[p->jobs_array_len], p->jobs,
456 sizeof(struct pthreadpool_job) * p->head);
458 p->jobs_array_len = new_len;
461 job = &p->jobs[(p->head + p->num_jobs) % p->jobs_array_len];
464 job->private_data = private_data;
471 static void pthreadpool_undo_put_job(struct pthreadpool *p)
476 static void *pthreadpool_server(void *arg)
478 struct pthreadpool *pool = (struct pthreadpool *)arg;
481 res = pthread_mutex_lock(&pool->mutex);
488 struct pthreadpool_job job;
491 * idle-wait at most 1 second. If nothing happens in that
492 * time, exit this thread.
495 clock_gettime(CLOCK_REALTIME, &ts);
498 while ((pool->num_jobs == 0) && !pool->shutdown) {
501 res = pthread_cond_timedwait(
502 &pool->condvar, &pool->mutex, &ts);
505 if (pool->prefork_cond != NULL) {
507 * Me must allow fork() to continue
508 * without anybody waiting on
509 * &pool->condvar. Tell
510 * pthreadpool_prepare_pool that we
514 res = pthread_cond_signal(pool->prefork_cond);
517 res = pthread_mutex_unlock(&pool->mutex);
521 * pthreadpool_prepare_pool has
522 * already locked this mutex across
523 * the fork. This makes us wait
524 * without sitting in a condvar.
526 res = pthread_mutex_lock(&pool->fork_mutex);
528 res = pthread_mutex_unlock(&pool->fork_mutex);
531 res = pthread_mutex_lock(&pool->mutex);
535 if (res == ETIMEDOUT) {
537 if (pool->num_jobs == 0) {
539 * we timed out and still no work for
542 pthreadpool_server_exit(pool);
551 if (pthreadpool_get_job(pool, &job)) {
555 * Do the work with the mutex unlocked
558 res = pthread_mutex_unlock(&pool->mutex);
561 job.fn(job.private_data);
563 ret = pool->signal_fn(job.id,
564 job.fn, job.private_data,
565 pool->signal_fn_private_data);
567 res = pthread_mutex_lock(&pool->mutex);
571 pthreadpool_server_exit(pool);
576 if ((pool->num_jobs == 0) && pool->shutdown) {
578 * No more work to do and we're asked to shut down, so
581 pthreadpool_server_exit(pool);
587 static int pthreadpool_create_thread(struct pthreadpool *pool)
589 pthread_attr_t thread_attr;
592 sigset_t mask, omask;
595 * Create a new worker thread. It should not receive any signals.
600 res = pthread_attr_init(&thread_attr);
605 res = pthread_attr_setdetachstate(
606 &thread_attr, PTHREAD_CREATE_DETACHED);
608 pthread_attr_destroy(&thread_attr);
612 res = pthread_sigmask(SIG_BLOCK, &mask, &omask);
614 pthread_attr_destroy(&thread_attr);
618 res = pthread_create(&thread_id, &thread_attr, pthreadpool_server,
621 assert(pthread_sigmask(SIG_SETMASK, &omask, NULL) == 0);
623 pthread_attr_destroy(&thread_attr);
626 pool->num_threads += 1;
632 int pthreadpool_add_job(struct pthreadpool *pool, int job_id,
633 void (*fn)(void *private_data), void *private_data)
638 res = pthread_mutex_lock(&pool->mutex);
643 if (pool->shutdown) {
645 * Protect against the pool being shut down while
646 * trying to add a job
648 unlock_res = pthread_mutex_unlock(&pool->mutex);
649 assert(unlock_res == 0);
653 if (pool->max_threads == 0) {
654 unlock_res = pthread_mutex_unlock(&pool->mutex);
655 assert(unlock_res == 0);
658 * If no thread are allowed we do strict sync processing.
661 res = pool->signal_fn(job_id, fn, private_data,
662 pool->signal_fn_private_data);
667 * Add job to the end of the queue
669 if (!pthreadpool_put_job(pool, job_id, fn, private_data)) {
670 unlock_res = pthread_mutex_unlock(&pool->mutex);
671 assert(unlock_res == 0);
675 if (pool->num_idle > 0) {
677 * We have idle threads, wake one.
679 res = pthread_cond_signal(&pool->condvar);
681 pthreadpool_undo_put_job(pool);
683 unlock_res = pthread_mutex_unlock(&pool->mutex);
684 assert(unlock_res == 0);
688 if (pool->num_threads >= pool->max_threads) {
690 * No more new threads, we just queue the request
692 unlock_res = pthread_mutex_unlock(&pool->mutex);
693 assert(unlock_res == 0);
697 res = pthreadpool_create_thread(pool);
699 unlock_res = pthread_mutex_unlock(&pool->mutex);
700 assert(unlock_res == 0);
704 if (pool->num_threads != 0) {
706 * At least one thread is still available, let
707 * that one run the queued job.
709 unlock_res = pthread_mutex_unlock(&pool->mutex);
710 assert(unlock_res == 0);
715 * No thread could be created to run job, fallback to sync
718 pthreadpool_undo_put_job(pool);
720 unlock_res = pthread_mutex_unlock(&pool->mutex);
721 assert(unlock_res == 0);