lib/pthreadpool: fix a memory leak
[Samba.git] / lib / pthreadpool / pthreadpool.c
blobf97cdcc6c15861482ccbf6803bbadbaccd81b084
1 /*
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/>.
20 #include "replace.h"
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"
27 #ifdef NDEBUG
28 #undef NDEBUG
29 #endif
31 #include <assert.h>
33 struct pthreadpool_job {
34 int id;
35 void (*fn)(void *private_data);
36 void *private_data;
39 struct pthreadpool {
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;
56 * Array of jobs
58 size_t jobs_array_len;
59 struct pthreadpool_job *jobs;
61 size_t head;
62 size_t num_jobs;
65 * Indicate job completion
67 int (*signal_fn)(int jobid,
68 void (*job_fn)(void *private_data),
69 void *job_fn_private_data,
70 void *private_data);
71 void *signal_fn_private_data;
74 * indicator to worker threads that they should shut down
76 bool shutdown;
79 * maximum number of threads
81 int max_threads;
84 * Number of threads
86 int num_threads;
89 * Number of idle threads
91 int num_idle;
94 static pthread_mutex_t pthreadpools_mutex = PTHREAD_MUTEX_INITIALIZER;
95 static struct pthreadpool *pthreadpools = NULL;
96 static pthread_once_t pthreadpool_atfork_initialized = PTHREAD_ONCE_INIT;
98 static void pthreadpool_prep_atfork(void);
101 * Initialize a thread pool
104 int pthreadpool_init(unsigned max_threads, struct pthreadpool **presult,
105 int (*signal_fn)(int jobid,
106 void (*job_fn)(void *private_data),
107 void *job_fn_private_data,
108 void *private_data),
109 void *signal_fn_private_data)
111 struct pthreadpool *pool;
112 int ret;
114 pool = (struct pthreadpool *)malloc(sizeof(struct pthreadpool));
115 if (pool == NULL) {
116 return ENOMEM;
118 pool->signal_fn = signal_fn;
119 pool->signal_fn_private_data = signal_fn_private_data;
121 pool->jobs_array_len = 4;
122 pool->jobs = calloc(
123 pool->jobs_array_len, sizeof(struct pthreadpool_job));
125 if (pool->jobs == NULL) {
126 free(pool);
127 return ENOMEM;
130 pool->head = pool->num_jobs = 0;
132 ret = pthread_mutex_init(&pool->mutex, NULL);
133 if (ret != 0) {
134 free(pool->jobs);
135 free(pool);
136 return ret;
139 ret = pthread_cond_init(&pool->condvar, NULL);
140 if (ret != 0) {
141 pthread_mutex_destroy(&pool->mutex);
142 free(pool->jobs);
143 free(pool);
144 return ret;
147 pool->shutdown = false;
148 pool->num_threads = 0;
149 pool->max_threads = max_threads;
150 pool->num_idle = 0;
152 ret = pthread_mutex_lock(&pthreadpools_mutex);
153 if (ret != 0) {
154 pthread_cond_destroy(&pool->condvar);
155 pthread_mutex_destroy(&pool->mutex);
156 free(pool->jobs);
157 free(pool);
158 return ret;
160 DLIST_ADD(pthreadpools, pool);
162 ret = pthread_mutex_unlock(&pthreadpools_mutex);
163 assert(ret == 0);
165 pthread_once(&pthreadpool_atfork_initialized, pthreadpool_prep_atfork);
167 *presult = pool;
169 return 0;
172 static void pthreadpool_prepare(void)
174 int ret;
175 struct pthreadpool *pool;
177 ret = pthread_mutex_lock(&pthreadpools_mutex);
178 assert(ret == 0);
180 pool = pthreadpools;
182 while (pool != NULL) {
183 ret = pthread_mutex_lock(&pool->mutex);
184 assert(ret == 0);
185 pool = pool->next;
189 static void pthreadpool_parent(void)
191 int ret;
192 struct pthreadpool *pool;
194 for (pool = DLIST_TAIL(pthreadpools);
195 pool != NULL;
196 pool = DLIST_PREV(pool)) {
197 ret = pthread_mutex_unlock(&pool->mutex);
198 assert(ret == 0);
201 ret = pthread_mutex_unlock(&pthreadpools_mutex);
202 assert(ret == 0);
205 static void pthreadpool_child(void)
207 int ret;
208 struct pthreadpool *pool;
210 for (pool = DLIST_TAIL(pthreadpools);
211 pool != NULL;
212 pool = DLIST_PREV(pool)) {
214 pool->num_threads = 0;
215 pool->num_idle = 0;
216 pool->head = 0;
217 pool->num_jobs = 0;
219 ret = pthread_mutex_unlock(&pool->mutex);
220 assert(ret == 0);
223 ret = pthread_mutex_unlock(&pthreadpools_mutex);
224 assert(ret == 0);
227 static void pthreadpool_prep_atfork(void)
229 pthread_atfork(pthreadpool_prepare, pthreadpool_parent,
230 pthreadpool_child);
233 static int pthreadpool_free(struct pthreadpool *pool)
235 int ret, ret1;
237 ret = pthread_mutex_lock(&pthreadpools_mutex);
238 if (ret != 0) {
239 return ret;
241 DLIST_REMOVE(pthreadpools, pool);
242 ret = pthread_mutex_unlock(&pthreadpools_mutex);
243 assert(ret == 0);
245 ret = pthread_mutex_destroy(&pool->mutex);
246 ret1 = pthread_cond_destroy(&pool->condvar);
248 if (ret != 0) {
249 return ret;
251 if (ret1 != 0) {
252 return ret1;
255 free(pool->jobs);
256 free(pool);
258 return 0;
262 * Destroy a thread pool. Wake up all idle threads for exit. The last
263 * one will free the pool.
266 int pthreadpool_destroy(struct pthreadpool *pool)
268 int ret, ret1;
270 ret = pthread_mutex_lock(&pool->mutex);
271 if (ret != 0) {
272 return ret;
275 if (pool->shutdown) {
276 ret = pthread_mutex_unlock(&pool->mutex);
277 assert(ret == 0);
278 return EBUSY;
281 pool->shutdown = true;
283 if (pool->num_threads == 0) {
284 ret = pthread_mutex_unlock(&pool->mutex);
285 assert(ret == 0);
287 ret = pthreadpool_free(pool);
288 return ret;
292 * We have active threads, tell them to finish.
295 ret = pthread_cond_broadcast(&pool->condvar);
297 ret1 = pthread_mutex_unlock(&pool->mutex);
298 assert(ret1 == 0);
300 return ret;
304 * Prepare for pthread_exit(), pool->mutex must be locked and will be
305 * unlocked here. This is a bit of a layering violation, but here we
306 * also take care of removing the pool if we're the last thread.
308 static void pthreadpool_server_exit(struct pthreadpool *pool)
310 int ret;
311 bool free_it;
313 pool->num_threads -= 1;
315 free_it = (pool->shutdown && (pool->num_threads == 0));
317 ret = pthread_mutex_unlock(&pool->mutex);
318 assert(ret == 0);
320 if (free_it) {
321 pthreadpool_free(pool);
325 static bool pthreadpool_get_job(struct pthreadpool *p,
326 struct pthreadpool_job *job)
328 if (p->num_jobs == 0) {
329 return false;
331 *job = p->jobs[p->head];
332 p->head = (p->head+1) % p->jobs_array_len;
333 p->num_jobs -= 1;
334 return true;
337 static bool pthreadpool_put_job(struct pthreadpool *p,
338 int id,
339 void (*fn)(void *private_data),
340 void *private_data)
342 struct pthreadpool_job *job;
344 if (p->num_jobs == p->jobs_array_len) {
345 struct pthreadpool_job *tmp;
346 size_t new_len = p->jobs_array_len * 2;
348 tmp = realloc(
349 p->jobs, sizeof(struct pthreadpool_job) * new_len);
350 if (tmp == NULL) {
351 return false;
353 p->jobs = tmp;
356 * We just doubled the jobs array. The array implements a FIFO
357 * queue with a modulo-based wraparound, so we have to memcpy
358 * the jobs that are logically at the queue end but physically
359 * before the queue head into the reallocated area. The new
360 * space starts at the current jobs_array_len, and we have to
361 * copy everything before the current head job into the new
362 * area.
364 memcpy(&p->jobs[p->jobs_array_len], p->jobs,
365 sizeof(struct pthreadpool_job) * p->head);
367 p->jobs_array_len = new_len;
370 job = &p->jobs[(p->head + p->num_jobs) % p->jobs_array_len];
371 job->id = id;
372 job->fn = fn;
373 job->private_data = private_data;
375 p->num_jobs += 1;
377 return true;
380 static void *pthreadpool_server(void *arg)
382 struct pthreadpool *pool = (struct pthreadpool *)arg;
383 int res;
385 res = pthread_mutex_lock(&pool->mutex);
386 if (res != 0) {
387 return NULL;
390 while (1) {
391 struct timespec ts;
392 struct pthreadpool_job job;
395 * idle-wait at most 1 second. If nothing happens in that
396 * time, exit this thread.
399 clock_gettime(CLOCK_REALTIME, &ts);
400 ts.tv_sec += 1;
402 while ((pool->num_jobs == 0) && !pool->shutdown) {
404 pool->num_idle += 1;
405 res = pthread_cond_timedwait(
406 &pool->condvar, &pool->mutex, &ts);
407 pool->num_idle -= 1;
409 if (res == ETIMEDOUT) {
411 if (pool->num_jobs == 0) {
413 * we timed out and still no work for
414 * us. Exit.
416 pthreadpool_server_exit(pool);
417 return NULL;
420 break;
422 assert(res == 0);
425 if (pthreadpool_get_job(pool, &job)) {
426 int ret;
429 * Do the work with the mutex unlocked
432 res = pthread_mutex_unlock(&pool->mutex);
433 assert(res == 0);
435 job.fn(job.private_data);
437 ret = pool->signal_fn(job.id,
438 job.fn, job.private_data,
439 pool->signal_fn_private_data);
441 res = pthread_mutex_lock(&pool->mutex);
442 assert(res == 0);
444 if (ret != 0) {
445 pthreadpool_server_exit(pool);
446 return NULL;
450 if ((pool->num_jobs == 0) && pool->shutdown) {
452 * No more work to do and we're asked to shut down, so
453 * exit
455 pthreadpool_server_exit(pool);
456 return NULL;
461 int pthreadpool_add_job(struct pthreadpool *pool, int job_id,
462 void (*fn)(void *private_data), void *private_data)
464 pthread_attr_t thread_attr;
465 pthread_t thread_id;
466 int res;
467 sigset_t mask, omask;
469 res = pthread_mutex_lock(&pool->mutex);
470 if (res != 0) {
471 return res;
474 if (pool->shutdown) {
476 * Protect against the pool being shut down while
477 * trying to add a job
479 res = pthread_mutex_unlock(&pool->mutex);
480 assert(res == 0);
481 return EINVAL;
485 * Add job to the end of the queue
487 if (!pthreadpool_put_job(pool, job_id, fn, private_data)) {
488 pthread_mutex_unlock(&pool->mutex);
489 return ENOMEM;
492 if (pool->num_idle > 0) {
494 * We have idle threads, wake one.
496 res = pthread_cond_signal(&pool->condvar);
497 pthread_mutex_unlock(&pool->mutex);
498 return res;
501 if ((pool->max_threads != 0) &&
502 (pool->num_threads >= pool->max_threads)) {
504 * No more new threads, we just queue the request
506 pthread_mutex_unlock(&pool->mutex);
507 return 0;
511 * Create a new worker thread. It should not receive any signals.
514 sigfillset(&mask);
516 res = pthread_attr_init(&thread_attr);
517 if (res != 0) {
518 pthread_mutex_unlock(&pool->mutex);
519 return res;
522 res = pthread_attr_setdetachstate(
523 &thread_attr, PTHREAD_CREATE_DETACHED);
524 if (res != 0) {
525 pthread_attr_destroy(&thread_attr);
526 pthread_mutex_unlock(&pool->mutex);
527 return res;
530 res = pthread_sigmask(SIG_BLOCK, &mask, &omask);
531 if (res != 0) {
532 pthread_attr_destroy(&thread_attr);
533 pthread_mutex_unlock(&pool->mutex);
534 return res;
537 res = pthread_create(&thread_id, &thread_attr, pthreadpool_server,
538 (void *)pool);
539 if (res == 0) {
540 pool->num_threads += 1;
543 assert(pthread_sigmask(SIG_SETMASK, &omask, NULL) == 0);
545 pthread_attr_destroy(&thread_attr);
547 pthread_mutex_unlock(&pool->mutex);
548 return res;