2 * threads.c request threading support
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 * Copyright 2000,2006 The FreeRADIUS server project
21 * Copyright 2000 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/rad_assert.h>
31 * Other OS's have sem_init, OS X doesn't.
33 #ifdef HAVE_SEMAPHORE_H
34 #include <semaphore.h>
38 #include <mach/task.h>
39 #include <mach/semaphore.h>
42 #define sem_t semaphore_t
44 #define sem_init(s,p,c) semaphore_create(mach_task_self(),s,SYNC_POLICY_FIFO,c)
46 #define sem_wait(s) semaphore_wait(*s)
48 #define sem_post(s) semaphore_signal(*s)
51 #ifdef HAVE_SYS_WAIT_H
57 #ifdef HAVE_OPENSSL_CRYPTO_H
58 #include <openssl/crypto.h>
60 #ifdef HAVE_OPENSSL_ERR_H
61 #include <openssl/err.h>
63 #ifdef HAVE_OPENSSL_EVP_H
64 #include <openssl/evp.h>
67 #define SEMAPHORE_LOCKED (0)
68 #define SEMAPHORE_UNLOCKED (1)
70 #define THREAD_RUNNING (1)
71 #define THREAD_CANCELLED (2)
72 #define THREAD_EXITED (3)
74 #define NUM_FIFOS RAD_LISTEN_MAX
78 * A data structure which contains the information about
81 * pthread_id pthread id
82 * thread_num server thread number, 1...number of threads
83 * semaphore used to block the thread until a request comes in
84 * status is the thread running or exited?
85 * request_count the number of requests that this thread has handled
86 * timestamp when the thread started executing.
88 typedef struct THREAD_HANDLE {
89 struct THREAD_HANDLE *prev;
90 struct THREAD_HANDLE *next;
94 unsigned int request_count;
100 * For the request queue.
102 typedef struct request_queue_t {
104 RAD_REQUEST_FUNP fun;
107 typedef struct thread_fork_t {
115 * A data structure to manage the thread pool. There's no real
116 * need for a data structure, but it makes things conceptually
119 typedef struct THREAD_POOL {
124 int active_threads; /* protected by queue_mutex */
128 int min_spare_threads;
129 int max_spare_threads;
130 unsigned int max_requests_per_thread;
131 unsigned long request_count;
132 time_t time_last_spawned;
137 pthread_mutex_t wait_mutex;
138 fr_hash_table_t *waiters;
142 * All threads wait on this semaphore, for requests
143 * to enter the queue.
148 * To ensure only one thread at a time touches the queue.
150 pthread_mutex_t queue_mutex;
154 fr_fifo_t *fifo[NUM_FIFOS];
157 static THREAD_POOL thread_pool;
158 static int pool_initialized = FALSE;
159 static time_t last_cleaned = 0;
161 static void thread_pool_manage(time_t now);
164 * A mapping of configuration file names to internal integers
166 static const CONF_PARSER thread_config[] = {
167 { "start_servers", PW_TYPE_INTEGER, 0, &thread_pool.start_threads, "5" },
168 { "max_servers", PW_TYPE_INTEGER, 0, &thread_pool.max_threads, "32" },
169 { "min_spare_servers", PW_TYPE_INTEGER, 0, &thread_pool.min_spare_threads, "3" },
170 { "max_spare_servers", PW_TYPE_INTEGER, 0, &thread_pool.max_spare_threads, "10" },
171 { "max_requests_per_server", PW_TYPE_INTEGER, 0, &thread_pool.max_requests_per_thread, "0" },
172 { "cleanup_delay", PW_TYPE_INTEGER, 0, &thread_pool.cleanup_delay, "5" },
173 { "max_queue_size", PW_TYPE_INTEGER, 0, &thread_pool.max_queue_size, "65536" },
174 { NULL, -1, 0, NULL, NULL }
178 #ifdef HAVE_OPENSSL_CRYPTO_H
181 * If we're linking against OpenSSL, then it is the
182 * duty of the application, if it is multithreaded,
183 * to provide OpenSSL with appropriate thread id
184 * and mutex locking functions
186 * Note: this only implements static callbacks.
187 * OpenSSL does not use dynamic locking callbacks
188 * right now, but may in the futiure, so we will have
189 * to add them at some point.
192 static pthread_mutex_t *ssl_mutexes = NULL;
194 static unsigned long ssl_id_function(void)
196 return (unsigned long) pthread_self();
199 static void ssl_locking_function(int mode, int n, const char *file, int line)
201 file = file; /* -Wunused */
202 line = line; /* -Wunused */
204 if (mode & CRYPTO_LOCK) {
205 pthread_mutex_lock(&(ssl_mutexes[n]));
207 pthread_mutex_unlock(&(ssl_mutexes[n]));
211 static int setup_ssl_mutexes(void)
215 #ifdef HAVE_OPENSSL_EVP_H
217 * Enable all ciphers and digests.
219 OpenSSL_add_all_algorithms();
222 ssl_mutexes = rad_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t));
224 radlog(L_ERR, "Error allocating memory for SSL mutexes!");
228 for (i = 0; i < CRYPTO_num_locks(); i++) {
229 pthread_mutex_init(&(ssl_mutexes[i]), NULL);
232 CRYPTO_set_id_callback(ssl_id_function);
233 CRYPTO_set_locking_callback(ssl_locking_function);
241 * We don't want to catch SIGCHLD for a host of reasons.
243 * - exec_wait means that someone, somewhere, somewhen, will
244 * call waitpid(), and catch the child.
246 * - SIGCHLD is delivered to a random thread, not the one that
249 * - if another thread catches the child, we have to coordinate
250 * with the thread doing the waiting.
252 * - if we don't waitpid() for non-wait children, they'll be zombies,
253 * and will hang around forever.
256 static void reap_children(void)
260 thread_fork_t mytf, *tf;
263 pthread_mutex_lock(&thread_pool.wait_mutex);
266 pid = waitpid(0, &status, WNOHANG);
270 tf = fr_hash_table_finddata(thread_pool.waiters, &mytf);
275 } while (fr_hash_table_num_elements(thread_pool.waiters) > 0);
277 pthread_mutex_unlock(&thread_pool.wait_mutex);
280 #define reap_children()
284 * Add a request to the list of waiting requests.
285 * This function gets called ONLY from the main handler thread...
287 * This function should never fail.
289 static int request_enqueue(REQUEST *request, RAD_REQUEST_FUNP fun)
291 request_queue_t *entry;
293 pthread_mutex_lock(&thread_pool.queue_mutex);
295 thread_pool.request_count++;
297 if (thread_pool.num_queued >= thread_pool.max_queue_size) {
298 pthread_mutex_unlock(&thread_pool.queue_mutex);
301 * Mark the request as done.
303 radlog(L_ERR, "!!! ERROR !!! The server is blocked: discarding new request %d", request->number);
304 request->child_state = REQUEST_DONE;
308 entry = rad_malloc(sizeof(*entry));
309 entry->request = request;
313 * Push the request onto the appropriate fifo for that
315 if (!fr_fifo_push(thread_pool.fifo[request->priority],
317 pthread_mutex_unlock(&thread_pool.queue_mutex);
318 radlog(L_ERR, "!!! ERROR !!! Failed inserting request %d into the queue", request->number);
319 request->child_state = REQUEST_DONE;
323 thread_pool.num_queued++;
325 pthread_mutex_unlock(&thread_pool.queue_mutex);
328 * There's one more request in the queue.
330 * Note that we're not touching the queue any more, so
331 * the semaphore post is outside of the mutex. This also
332 * means that when the thread wakes up and tries to lock
333 * the mutex, it will be unlocked, and there won't be
336 sem_post(&thread_pool.semaphore);
342 * Remove a request from the queue.
344 static int request_dequeue(REQUEST **request, RAD_REQUEST_FUNP *fun)
346 RAD_LISTEN_TYPE i, start;
347 request_queue_t *entry;
351 pthread_mutex_lock(&thread_pool.queue_mutex);
354 * Clear old requests from all queues.
356 * We only do one pass over the queue, in order to
357 * amortize the work across the child threads. Since we
358 * do N checks for one request de-queued, the old
359 * requests will be quickly cleared.
361 for (i = 0; i < RAD_LISTEN_MAX; i++) {
362 entry = fr_fifo_peek(thread_pool.fifo[i]);
364 (entry->request->master_state != REQUEST_STOP_PROCESSING)) {
368 * This entry was marked to be stopped. Acknowledge it.
370 entry = fr_fifo_pop(thread_pool.fifo[i]);
371 rad_assert(entry != NULL);
372 entry->request->child_state = REQUEST_DONE;
373 thread_pool.num_queued--;
381 * Pop results from the top of the queue
383 for (i = start; i < RAD_LISTEN_MAX; i++) {
384 entry = fr_fifo_pop(thread_pool.fifo[i]);
392 pthread_mutex_unlock(&thread_pool.queue_mutex);
398 rad_assert(thread_pool.num_queued > 0);
399 thread_pool.num_queued--;
400 *request = entry->request;
405 rad_assert(*request != NULL);
406 rad_assert((*request)->magic == REQUEST_MAGIC);
407 rad_assert(*fun != NULL);
410 * If the request has sat in the queue for too long,
413 * The main clean-up code can't delete the request from
414 * the queue, and therefore won't clean it up until we
415 * have acknowledged it as "done".
417 if ((*request)->master_state == REQUEST_STOP_PROCESSING) {
418 (*request)->child_state = REQUEST_DONE;
423 * The thread is currently processing a request.
425 thread_pool.active_threads++;
427 pthread_mutex_unlock(&thread_pool.queue_mutex);
434 * The main thread handler for requests.
436 * Wait on the semaphore until we have it, and process the request.
438 static void *request_handler_thread(void *arg)
440 RAD_REQUEST_FUNP fun;
441 THREAD_HANDLE *self = (THREAD_HANDLE *) arg;
444 * Loop forever, until told to exit.
448 * Wait to be signalled.
450 DEBUG2("Thread %d waiting to be assigned a request",
453 if (sem_wait(&thread_pool.semaphore) != 0) {
455 * Interrupted system call. Go back to
456 * waiting, but DON'T print out any more
459 if (errno == EINTR) {
460 DEBUG2("Re-wait %d", self->thread_num);
463 radlog(L_ERR, "Thread %d failed waiting for semaphore: %s: Exiting\n",
464 self->thread_num, strerror(errno));
468 DEBUG2("Thread %d got semaphore", self->thread_num);
470 #ifdef HAVE_OPENSSL_ERR_H
472 * Clear the error queue for the current thread.
478 * Try to grab a request from the queue.
480 * It may be empty, in which case we fail
483 if (!request_dequeue(&self->request, &fun)) continue;
485 self->request->child_pid = self->pthread_id;
486 self->request_count++;
488 DEBUG2("Thread %d handling request %d, (%d handled so far)",
489 self->thread_num, self->request->number,
490 self->request_count);
492 radius_handle_request(self->request, fun);
495 * Update the active threads.
497 pthread_mutex_lock(&thread_pool.queue_mutex);
498 rad_assert(thread_pool.active_threads > 0);
499 thread_pool.active_threads--;
500 pthread_mutex_unlock(&thread_pool.queue_mutex);
501 } while (self->status != THREAD_CANCELLED);
503 DEBUG2("Thread %d exiting...", self->thread_num);
505 #ifdef HAVE_OPENSSL_ERR_H
507 * If we linked with OpenSSL, the application
508 * must remove the thread's error queue before
509 * exiting to prevent memory leaks.
515 * Do this as the LAST thing before exiting.
517 self->request = NULL;
518 self->status = THREAD_EXITED;
524 * Take a THREAD_HANDLE, delete it from the thread pool and
525 * free its resources.
527 * This function is called ONLY from the main server thread,
528 * ONLY after the thread has exited.
530 static void delete_thread(THREAD_HANDLE *handle)
535 rad_assert(handle->request == NULL);
537 DEBUG2("Deleting thread %d", handle->thread_num);
541 rad_assert(thread_pool.total_threads > 0);
542 thread_pool.total_threads--;
545 * Remove the handle from the list.
548 rad_assert(thread_pool.head == handle);
549 thread_pool.head = next;
555 rad_assert(thread_pool.tail == handle);
556 thread_pool.tail = prev;
562 * Free the handle, now that it's no longer referencable.
569 * Spawn a new thread, and place it in the thread pool.
571 * The thread is started initially in the blocked state, waiting
574 static THREAD_HANDLE *spawn_thread(time_t now)
577 THREAD_HANDLE *handle;
581 * Ensure that we don't spawn too many threads.
583 if (thread_pool.total_threads >= thread_pool.max_threads) {
584 DEBUG2("Thread spawn failed. Maximum number of threads (%d) already running.", thread_pool.max_threads);
589 * Allocate a new thread handle.
591 handle = (THREAD_HANDLE *) rad_malloc(sizeof(THREAD_HANDLE));
592 memset(handle, 0, sizeof(THREAD_HANDLE));
595 handle->thread_num = thread_pool.max_thread_num++;
596 handle->request_count = 0;
597 handle->status = THREAD_RUNNING;
598 handle->timestamp = time(NULL);
601 * Initialize the thread's attributes to detached.
603 * We could call pthread_detach() later, but if the thread
604 * exits between the create & detach calls, it will need to
605 * be joined, which will never happen.
607 pthread_attr_init(&attr);
608 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
611 * Create the thread detached, so that it cleans up it's
612 * own memory when it exits.
614 * Note that the function returns non-zero on error, NOT
615 * -1. The return code is the error, and errno isn't set.
617 rcode = pthread_create(&handle->pthread_id, &attr,
618 request_handler_thread, handle);
620 radlog(L_ERR, "Thread create failed: %s",
624 pthread_attr_destroy(&attr);
627 * One more thread to go into the list.
629 thread_pool.total_threads++;
630 DEBUG2("Thread spawned new child %d. Total threads in pool: %d",
631 handle->thread_num, thread_pool.total_threads);
634 * Add the thread handle to the tail of the thread pool list.
636 if (thread_pool.tail) {
637 thread_pool.tail->next = handle;
638 handle->prev = thread_pool.tail;
639 thread_pool.tail = handle;
641 rad_assert(thread_pool.head == NULL);
642 thread_pool.head = thread_pool.tail = handle;
646 * Update the time we last spawned a thread.
648 thread_pool.time_last_spawned = now;
651 * And return the new handle to the caller.
657 * Temporary function to prevent server from executing a SIGHUP
658 * until all threads are finished handling requests. This returns
659 * the number of active threads to 'radiusd.c'.
661 int total_active_threads(void)
664 * We don't acquire the mutex, so this is just an estimate.
665 * We can't return with the lock held, so there's no point
666 * in getting the guaranteed correct value; by the time
667 * the caller sees it, it can be wrong again.
669 return thread_pool.active_threads;
674 static uint32_t pid_hash(const void *data)
676 const thread_fork_t *tf = data;
678 return fr_hash(&tf->pid, sizeof(tf->pid));
681 static int pid_cmp(const void *one, const void *two)
683 const thread_fork_t *a = one;
684 const thread_fork_t *b = two;
686 return (a->pid - b->pid);
691 * Allocate the thread pool, and seed it with an initial number
694 * FIXME: What to do on a SIGHUP???
696 int thread_pool_init(CONF_SECTION *cs, int spawn_flag)
699 CONF_SECTION *pool_cf;
705 * We're not spawning new threads, don't do
708 if (!spawn_flag) return 0;
711 * After a SIGHUP, we don't over-write the previous values.
713 if (!pool_initialized) {
715 * Initialize the thread pool to some reasonable values.
717 memset(&thread_pool, 0, sizeof(THREAD_POOL));
718 thread_pool.head = NULL;
719 thread_pool.tail = NULL;
720 thread_pool.total_threads = 0;
721 thread_pool.max_thread_num = 1;
722 thread_pool.cleanup_delay = 5;
723 thread_pool.spawn_flag = spawn_flag;
726 if ((pthread_mutex_init(&thread_pool.wait_mutex,NULL) != 0)) {
727 radlog(L_ERR, "FATAL: Failed to initialize wait mutex: %s",
733 * Create the hash table of child PID's
735 thread_pool.waiters = fr_hash_table_create(pid_hash,
738 if (!thread_pool.waiters) {
739 radlog(L_ERR, "FATAL: Failed to set up wait hash");
745 pool_cf = cf_subsection_find_next(cs, NULL, "thread");
747 radlog(L_ERR, "FATAL: Attempting to start in multi-threaded mode with no thread configuration in radiusd.conf");
751 if (cf_section_parse(pool_cf, NULL, thread_config) < 0) {
756 * Catch corner cases.
758 if (thread_pool.min_spare_threads < 1)
759 thread_pool.min_spare_threads = 1;
760 if (thread_pool.max_spare_threads < 1)
761 thread_pool.max_spare_threads = 1;
762 if (thread_pool.max_spare_threads < thread_pool.min_spare_threads)
763 thread_pool.max_spare_threads = thread_pool.min_spare_threads;
766 * The pool has already been initialized. Don't spawn
767 * new threads, and don't forget about forked children,
769 if (pool_initialized) {
774 * Initialize the queue of requests.
776 memset(&thread_pool.semaphore, 0, sizeof(thread_pool.semaphore));
777 rcode = sem_init(&thread_pool.semaphore, 0, SEMAPHORE_LOCKED);
779 radlog(L_ERR, "FATAL: Failed to initialize semaphore: %s",
784 rcode = pthread_mutex_init(&thread_pool.queue_mutex,NULL);
786 radlog(L_ERR, "FATAL: Failed to initialize queue mutex: %s",
792 * Allocate multiple fifos.
794 for (i = 0; i < RAD_LISTEN_MAX; i++) {
795 thread_pool.fifo[i] = fr_fifo_create(65536, NULL);
796 if (!thread_pool.fifo[i]) {
797 radlog(L_ERR, "FATAL: Failed to set up request fifo");
802 #ifdef HAVE_OPENSSL_CRYPTO_H
804 * If we're linking with OpenSSL too, then we need
805 * to set up the mutexes and enable the thread callbacks.
807 if (!setup_ssl_mutexes()) {
808 radlog(L_ERR, "FATAL: Failed to set up SSL mutexes");
815 * Create a number of waiting threads.
817 * If we fail while creating them, do something intelligent.
819 for (i = 0; i < thread_pool.start_threads; i++) {
820 if (spawn_thread(now) == NULL) {
825 DEBUG2("Thread pool initialized");
826 pool_initialized = TRUE;
832 * Assign a new request to a free thread.
834 * If there isn't a free thread, then try to create a new one,
835 * up to the configured limits.
837 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
839 time_t now = request->timestamp;
842 * We've been told not to spawn threads, so don't.
844 if (!thread_pool.spawn_flag) {
845 radius_handle_request(request, fun);
849 * Requests that care about child process exit
850 * codes have already either called
851 * rad_waitpid(), or they've given up.
859 * Add the new request to the queue.
861 if (!request_enqueue(request, fun)) return 0;
864 * If we haven't checked the number of child threads
865 * in a while, OR if the thread pool appears to be full,
868 if ((last_cleaned < now) ||
869 (thread_pool.active_threads == thread_pool.total_threads)) {
870 thread_pool_manage(now);
877 * Check the min_spare_threads and max_spare_threads.
879 * If there are too many or too few threads waiting, then we
880 * either create some more, or delete some.
882 static void thread_pool_manage(time_t now)
886 THREAD_HANDLE *handle, *next;
890 * We don't need a mutex lock here, as we're reading
891 * active_threads, and not modifying it. We want a close
892 * approximation of the number of active threads, and this
895 active_threads = thread_pool.active_threads;
896 spare = thread_pool.total_threads - active_threads;
898 static int old_total = -1;
899 static int old_active = -1;
901 if ((old_total != thread_pool.total_threads) ||
902 (old_active != active_threads)) {
903 DEBUG2("Threads: total/active/spare threads = %d/%d/%d",
904 thread_pool.total_threads, active_threads, spare);
905 old_total = thread_pool.total_threads;
906 old_active = active_threads;
911 * If there are too few spare threads. Go create some more.
913 if (spare < thread_pool.min_spare_threads) {
914 total = thread_pool.min_spare_threads - spare;
916 DEBUG2("Threads: Spawning %d spares", total);
919 * Create a number of spare threads.
921 for (i = 0; i < total; i++) {
922 handle = spawn_thread(now);
923 if (handle == NULL) {
928 return; /* there aren't too many spare threads */
932 * Only delete spare threads if we haven't already done
935 if (now == last_cleaned) {
941 * Loop over the thread pool, deleting exited threads.
943 for (handle = thread_pool.head; handle; handle = next) {
947 * Maybe we've asked the thread to exit, and it
950 if (handle->status == THREAD_EXITED) {
951 delete_thread(handle);
956 * Only delete the spare threads if sufficient time has
957 * passed since we last created one. This helps to minimize
958 * the amount of create/delete cycles.
960 if ((now - thread_pool.time_last_spawned) < thread_pool.cleanup_delay) {
965 * If there are too many spare threads, delete one.
967 * Note that we only delete ONE at a time, instead of
968 * wiping out many. This allows the excess servers to
969 * be slowly reaped, just in case the load spike comes again.
971 if (spare > thread_pool.max_spare_threads) {
973 spare -= thread_pool.max_spare_threads;
975 DEBUG2("Threads: deleting 1 spare out of %d spares", spare);
978 * Walk through the thread pool, deleting the
979 * first idle thread we come across.
981 for (handle = thread_pool.head; (handle != NULL) && (spare > 0) ; handle = next) {
985 * If the thread is not handling a
986 * request, but still live, then tell it
989 * It will eventually wake up, and realize
990 * it's been told to commit suicide.
992 if ((handle->request == NULL) &&
993 (handle->status == THREAD_RUNNING)) {
994 handle->status = THREAD_CANCELLED;
996 * Post an extra semaphore, as a
997 * signal to wake up, and exit.
999 sem_post(&thread_pool.semaphore);
1007 * If the thread has handled too many requests, then make it
1010 if (thread_pool.max_requests_per_thread > 0) {
1011 for (handle = thread_pool.head; handle; handle = next) {
1012 next = handle->next;
1015 * Not handling a request, but otherwise
1016 * live, we can kill it.
1018 if ((handle->request == NULL) &&
1019 (handle->status == THREAD_RUNNING) &&
1020 (handle->request_count > thread_pool.max_requests_per_thread)) {
1021 handle->status = THREAD_CANCELLED;
1022 sem_post(&thread_pool.semaphore);
1028 * Otherwise everything's kosher. There are not too few,
1029 * or too many spare threads. Exit happily.
1037 * Thread wrapper for fork().
1039 pid_t rad_fork(void)
1043 if (!pool_initialized) return fork();
1045 reap_children(); /* be nice to non-wait thingies */
1047 if (fr_hash_table_num_elements(thread_pool.waiters) >= 1024) {
1052 * Fork & save the PID for later reaping.
1055 if (child_pid > 0) {
1059 tf = rad_malloc(sizeof(*tf));
1060 memset(tf, 0, sizeof(*tf));
1062 tf->pid = child_pid;
1064 pthread_mutex_lock(&thread_pool.wait_mutex);
1065 rcode = fr_hash_table_insert(thread_pool.waiters, tf);
1066 pthread_mutex_unlock(&thread_pool.wait_mutex);
1069 radlog(L_ERR, "Failed to store PID, creating what will be a zombie process %d",
1075 * Return whatever we were told.
1082 * Wait 10 seconds at most for a child to exit, then give up.
1084 pid_t rad_waitpid(pid_t pid, int *status)
1087 thread_fork_t mytf, *tf;
1089 if (!pool_initialized) return waitpid(pid, status, 0);
1091 if (pid <= 0) return -1;
1095 pthread_mutex_lock(&thread_pool.wait_mutex);
1096 tf = fr_hash_table_finddata(thread_pool.waiters, &mytf);
1097 pthread_mutex_unlock(&thread_pool.wait_mutex);
1101 for (i = 0; i < 100; i++) {
1105 *status = tf->status;
1107 pthread_mutex_lock(&thread_pool.wait_mutex);
1108 fr_hash_table_delete(thread_pool.waiters, &mytf);
1109 pthread_mutex_unlock(&thread_pool.wait_mutex);
1112 usleep(100000); /* sleep for 1/10 of a second */
1116 * 10 seconds have passed, give up on the child.
1118 pthread_mutex_lock(&thread_pool.wait_mutex);
1119 fr_hash_table_delete(thread_pool.waiters, &mytf);
1120 pthread_mutex_unlock(&thread_pool.wait_mutex);
1126 * No rad_fork or rad_waitpid
1130 void thread_pool_lock(void)
1132 pthread_mutex_lock(&thread_pool.queue_mutex);
1135 void thread_pool_unlock(void)
1137 pthread_mutex_unlock(&thread_pool.queue_mutex);
1140 void thread_pool_queue_stats(int *array)
1144 if (pool_initialized) {
1145 for (i = 0; i < RAD_LISTEN_MAX; i++) {
1146 array[i] = fr_fifo_num_elements(thread_pool.fifo[i]);
1149 for (i = 0; i < RAD_LISTEN_MAX; i++) {
1154 #endif /* HAVE_PTHREAD_H */