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 The FreeRADIUS server project
21 * Copyright 2000 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/autoconf.h>
30 * Other OS's have sem_init, OS X doesn't.
33 #include <semaphore.h>
35 #include <mach/task.h>
36 #include <mach/semaphore.h>
39 #define sem_t semaphore_t
41 #define sem_init(s,p,c) semaphore_create(mach_task_self(),s,SYNC_POLICY_FIFO,c)
43 #define sem_wait(s) semaphore_wait(*s)
45 #define sem_post(s) semaphore_signal(*s)
50 #ifdef HAVE_SYS_WAIT_H
54 #include <freeradius-devel/radiusd.h>
55 #include <freeradius-devel/rad_assert.h>
56 #include <freeradius-devel/conffile.h>
57 #include <freeradius-devel/modules.h>
59 static const char rcsid[] =
64 #ifdef HAVE_OPENSSL_CRYPTO_H
65 #include <openssl/crypto.h>
67 #ifdef HAVE_OPENSSL_ERR_H
68 #include <openssl/err.h>
71 #define SEMAPHORE_LOCKED (0)
72 #define SEMAPHORE_UNLOCKED (1)
74 #define THREAD_RUNNING (1)
75 #define THREAD_CANCELLED (2)
76 #define THREAD_EXITED (3)
81 * Ordered this way because we prefer proxy, then ongoing, then
84 #define FIFO_START (1)
85 #define FIFO_PROXY (0)
88 * A data structure which contains the information about
91 * pthread_id pthread id
92 * thread_num server thread number, 1...number of threads
93 * semaphore used to block the thread until a request comes in
94 * status is the thread running or exited?
95 * request_count the number of requests that this thread has handled
96 * timestamp when the thread started executing.
98 typedef struct THREAD_HANDLE {
99 struct THREAD_HANDLE *prev;
100 struct THREAD_HANDLE *next;
101 pthread_t pthread_id;
104 unsigned int request_count;
110 * For the request queue.
112 typedef struct request_queue_t {
114 RAD_REQUEST_FUNP fun;
117 typedef struct thread_fork_t {
125 * A data structure to manage the thread pool. There's no real
126 * need for a data structure, but it makes things conceptually
129 typedef struct THREAD_POOL {
134 int active_threads; /* protected by queue_mutex */
138 int min_spare_threads;
139 int max_spare_threads;
140 unsigned int max_requests_per_thread;
141 unsigned long request_count;
142 time_t time_last_spawned;
146 pthread_mutex_t wait_mutex;
147 lrad_hash_table_t *waiters;
150 * All threads wait on this semaphore, for requests
151 * to enter the queue.
156 * To ensure only one thread at a time touches the queue.
158 pthread_mutex_t queue_mutex;
163 lrad_fifo_t *fifo[NUM_FIFOS];
166 static THREAD_POOL thread_pool;
167 static int pool_initialized = FALSE;
171 * A mapping of configuration file names to internal integers
173 static const CONF_PARSER thread_config[] = {
174 { "start_servers", PW_TYPE_INTEGER, 0, &thread_pool.start_threads, "5" },
175 { "max_servers", PW_TYPE_INTEGER, 0, &thread_pool.max_threads, "32" },
176 { "min_spare_servers", PW_TYPE_INTEGER, 0, &thread_pool.min_spare_threads, "3" },
177 { "max_spare_servers", PW_TYPE_INTEGER, 0, &thread_pool.max_spare_threads, "10" },
178 { "max_requests_per_server", PW_TYPE_INTEGER, 0, &thread_pool.max_requests_per_thread, "0" },
179 { "cleanup_delay", PW_TYPE_INTEGER, 0, &thread_pool.cleanup_delay, "5" },
180 { "max_queue_size", PW_TYPE_INTEGER, 0, &thread_pool.max_queue_size, "65536" },
181 { NULL, -1, 0, NULL, NULL }
185 #ifdef HAVE_OPENSSL_CRYPTO_H
188 * If we're linking against OpenSSL, then it is the
189 * duty of the application, if it is multithreaded,
190 * to provide OpenSSL with appropriate thread id
191 * and mutex locking functions
193 * Note: this only implements static callbacks.
194 * OpenSSL does not use dynamic locking callbacks
195 * right now, but may in the futiure, so we will have
196 * to add them at some point.
199 static pthread_mutex_t *ssl_mutexes = NULL;
201 static unsigned long ssl_id_function(void)
203 return (unsigned long) pthread_self();
206 static void ssl_locking_function(int mode, int n, const char *file, int line)
208 file = file; /* -Wunused */
209 line = line; /* -Wunused */
211 if (mode & CRYPTO_LOCK) {
212 pthread_mutex_lock(&(ssl_mutexes[n]));
214 pthread_mutex_unlock(&(ssl_mutexes[n]));
218 static int setup_ssl_mutexes(void)
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;
262 if (lrad_hash_table_num_elements(thread_pool.waiters) == 0) return;
264 pthread_mutex_lock(&thread_pool.wait_mutex);
267 pid = waitpid(0, &status, WNOHANG);
271 tf = lrad_hash_table_finddata(thread_pool.waiters, &mytf);
278 pthread_mutex_unlock(&thread_pool.wait_mutex);
282 * Add a request to the list of waiting requests.
283 * This function gets called ONLY from the main handler thread...
285 * This function should never fail.
287 static int request_enqueue(REQUEST *request, RAD_REQUEST_FUNP fun)
289 int fifo = FIFO_START;
290 request_queue_t *entry;
292 pthread_mutex_lock(&thread_pool.queue_mutex);
294 thread_pool.request_count++;
297 * FIXME: Handle proxy replies separately?
299 if (thread_pool.num_queued >= thread_pool.max_queue_size) {
300 pthread_mutex_unlock(&thread_pool.queue_mutex);
303 * Mark the request as done.
305 radlog(L_ERR|L_CONS, "!!! ERROR !!! The server is blocked: discarding new request %d", request->number);
306 request->finished = TRUE;
311 * Requests get handled in priority. First, we handle
312 * replies from a home server, to finish ongoing requests.
314 * Then, we handle requests with State, to finish
315 * multi-packet transactions.
317 * Finally, we handle new requests.
319 if (request->proxy_reply) {
325 entry = rad_malloc(sizeof(*entry));
326 entry->request = request;
329 if (!lrad_fifo_push(thread_pool.fifo[fifo], entry)) {
330 pthread_mutex_unlock(&thread_pool.queue_mutex);
331 radlog(L_ERR, "!!! ERROR !!! Failed inserting request %d into the queue", request->number);
332 request->finished = TRUE;
336 thread_pool.num_queued++;
338 pthread_mutex_unlock(&thread_pool.queue_mutex);
341 * There's one more request in the queue.
343 * Note that we're not touching the queue any more, so
344 * the semaphore post is outside of the mutex. This also
345 * means that when the thread wakes up and tries to lock
346 * the mutex, it will be unlocked, and there won't be
349 sem_post(&thread_pool.semaphore);
355 * Remove a request from the queue.
357 static int request_dequeue(REQUEST **request, RAD_REQUEST_FUNP *fun)
360 request_queue_t *entry;
364 pthread_mutex_lock(&thread_pool.queue_mutex);
366 fifo_state = thread_pool.fifo_state;
371 * Pop an entry from the current queue, and go to
374 entry = lrad_fifo_pop(thread_pool.fifo[fifo_state]);
376 if (fifo_state >= NUM_FIFOS) fifo_state = 0;
377 } while ((fifo_state != thread_pool.fifo_state) && !entry);
380 pthread_mutex_unlock(&thread_pool.queue_mutex);
386 rad_assert(thread_pool.num_queued > 0);
387 thread_pool.num_queued--;
388 *request = entry->request;
392 rad_assert(*request != NULL);
393 rad_assert((*request)->magic == REQUEST_MAGIC);
394 rad_assert(*fun != NULL);
397 * If the request has sat in the queue for too long,
400 * The main clean-up code won't delete the request from
401 * the request list, until it's marked "finished"
403 if ((*request)->options & RAD_REQUEST_OPTION_STOP_NOW) {
404 (*request)->finished = 1;
409 * The thread is currently processing a request.
411 thread_pool.active_threads++;
412 thread_pool.fifo_state = fifo_state;
414 pthread_mutex_unlock(&thread_pool.queue_mutex);
416 rad_assert((*request)->child_pid == NO_SUCH_CHILD_PID);
423 * The main thread handler for requests.
425 * Wait on the semaphore until we have it, and process the request.
427 static void *request_handler_thread(void *arg)
429 RAD_REQUEST_FUNP fun;
430 THREAD_HANDLE *self = (THREAD_HANDLE *) arg;
431 #ifdef HAVE_PTHREAD_SIGMASK
435 * Block SIGHUP handling for the child threads.
437 * This ensures that only the main server thread will
438 * process HUP signals.
440 * If we don't have sigprocmask, then it shouldn't be
441 * a problem, either, as the sig_hup handler should check
442 * for this condition.
445 sigaddset(&set, SIGHUP);
446 sigaddset(&set, SIGINT);
447 sigaddset(&set, SIGQUIT);
448 sigaddset(&set, SIGTERM);
449 pthread_sigmask(SIG_BLOCK, &set, NULL);
453 * Loop forever, until told to exit.
459 * Wait to be signalled.
461 DEBUG2("Thread %d waiting to be assigned a request",
464 if (sem_wait(&thread_pool.semaphore) != 0) {
466 * Interrupted system call. Go back to
467 * waiting, but DON'T print out any more
470 if (errno == EINTR) {
471 DEBUG2("Re-wait %d", self->thread_num);
474 radlog(L_ERR, "Thread %d failed waiting for semaphore: %s: Exiting\n",
475 self->thread_num, strerror(errno));
479 DEBUG2("Thread %d got semaphore", self->thread_num);
482 * Try to grab a request from the queue.
484 * It may be empty, in which case we fail
487 if (!request_dequeue(&self->request, &fun)) continue;
489 self->request->child_pid = self->pthread_id;
490 self->request_count++;
492 DEBUG2("Thread %d handling request %d, (%d handled so far)",
493 self->thread_num, self->request->number,
494 self->request_count);
497 * Respond, and reset request->child_pid
499 finished = rad_respond(self->request, fun);
502 * Update the active threads.
504 pthread_mutex_lock(&thread_pool.queue_mutex);
507 * We haven't replied to the client, but we HAVE
508 * sent a proxied packet, and we have NOT
509 * received a proxy response. In that case, send
510 * the proxied packet now. Doing this in the mutex
511 * avoids race conditions.
513 * FIXME: this work should really depend on a
514 * "state", and "next handler", rather than
515 * horrid hacks like thise.
517 if (!self->request->reply->data &&
518 self->request->proxy && self->request->proxy->data
519 && !self->request->proxy_reply)
520 self->request->proxy_listener->send(self->request->proxy_listener,
521 (char *)self->request->proxysecret);
523 self->request->child_pid = NO_SUCH_CHILD_PID;
524 self->request->finished = finished;
525 self->request = NULL;
527 rad_assert(thread_pool.active_threads > 0);
528 thread_pool.active_threads--;
529 pthread_mutex_unlock(&thread_pool.queue_mutex);
530 } while (self->status != THREAD_CANCELLED);
532 DEBUG2("Thread %d exiting...", self->thread_num);
534 #ifdef HAVE_OPENSSL_ERR_H
536 * If we linked with OpenSSL, the application
537 * must remove the thread's error queue before
538 * exiting to prevent memory leaks.
544 * Do this as the LAST thing before exiting.
546 self->status = THREAD_EXITED;
552 * Take a THREAD_HANDLE, delete it from the thread pool and
553 * free its resources.
555 * This function is called ONLY from the main server thread,
556 * ONLY after the thread has exited.
558 static void delete_thread(THREAD_HANDLE *handle)
563 rad_assert(handle->request == NULL);
565 DEBUG2("Deleting thread %d", handle->thread_num);
569 rad_assert(thread_pool.total_threads > 0);
570 thread_pool.total_threads--;
573 * Remove the handle from the list.
576 rad_assert(thread_pool.head == handle);
577 thread_pool.head = next;
583 rad_assert(thread_pool.tail == handle);
584 thread_pool.tail = prev;
590 * Free the handle, now that it's no longer referencable.
597 * Spawn a new thread, and place it in the thread pool.
599 * The thread is started initially in the blocked state, waiting
602 static THREAD_HANDLE *spawn_thread(time_t now)
605 THREAD_HANDLE *handle;
609 * Ensure that we don't spawn too many threads.
611 if (thread_pool.total_threads >= thread_pool.max_threads) {
612 DEBUG2("Thread spawn failed. Maximum number of threads (%d) already running.", thread_pool.max_threads);
617 * Allocate a new thread handle.
619 handle = (THREAD_HANDLE *) rad_malloc(sizeof(THREAD_HANDLE));
620 memset(handle, 0, sizeof(THREAD_HANDLE));
623 handle->pthread_id = NO_SUCH_CHILD_PID;
624 handle->thread_num = thread_pool.max_thread_num++;
625 handle->request_count = 0;
626 handle->status = THREAD_RUNNING;
627 handle->timestamp = time(NULL);
630 * Initialize the thread's attributes to detached.
632 * We could call pthread_detach() later, but if the thread
633 * exits between the create & detach calls, it will need to
634 * be joined, which will never happen.
636 pthread_attr_init(&attr);
637 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
640 * Create the thread detached, so that it cleans up it's
641 * own memory when it exits.
643 * Note that the function returns non-zero on error, NOT
644 * -1. The return code is the error, and errno isn't set.
646 rcode = pthread_create(&handle->pthread_id, &attr,
647 request_handler_thread, handle);
649 radlog(L_ERR|L_CONS, "FATAL: Thread create failed: %s",
653 pthread_attr_destroy(&attr);
656 * One more thread to go into the list.
658 thread_pool.total_threads++;
659 DEBUG2("Thread spawned new child %d. Total threads in pool: %d",
660 handle->thread_num, thread_pool.total_threads);
663 * Add the thread handle to the tail of the thread pool list.
665 if (thread_pool.tail) {
666 thread_pool.tail->next = handle;
667 handle->prev = thread_pool.tail;
668 thread_pool.tail = handle;
670 rad_assert(thread_pool.head == NULL);
671 thread_pool.head = thread_pool.tail = handle;
675 * Update the time we last spawned a thread.
677 thread_pool.time_last_spawned = now;
680 * And return the new handle to the caller.
686 * Temporary function to prevent server from executing a SIGHUP
687 * until all threads are finished handling requests. This returns
688 * the number of active threads to 'radiusd.c'.
690 int total_active_threads(void)
693 * We don't acquire the mutex, so this is just an estimate.
694 * We can't return with the lock held, so there's no point
695 * in getting the guaranteed correct value; by the time
696 * the caller sees it, it can be wrong again.
698 return thread_pool.active_threads;
702 static uint32_t pid_hash(const void *data)
704 const thread_fork_t *tf = data;
706 return lrad_hash(&tf->pid, sizeof(tf->pid));
709 static int pid_cmp(const void *one, const void *two)
711 const thread_fork_t *a = one;
712 const thread_fork_t *b = two;
714 return (a->pid - b->pid);
718 * Allocate the thread pool, and seed it with an initial number
721 * FIXME: What to do on a SIGHUP???
723 int thread_pool_init(int spawn_flag)
726 CONF_SECTION *pool_cf;
729 DEBUG("Initializing the thread pool...");
733 * After a SIGHUP, we don't over-write the previous values.
735 if (!pool_initialized) {
737 * Initialize the thread pool to some reasonable values.
739 memset(&thread_pool, 0, sizeof(THREAD_POOL));
740 thread_pool.head = NULL;
741 thread_pool.tail = NULL;
742 thread_pool.total_threads = 0;
743 thread_pool.max_thread_num = 1;
744 thread_pool.cleanup_delay = 5;
745 thread_pool.spawn_flag = spawn_flag;
747 if ((pthread_mutex_init(&thread_pool.wait_mutex,NULL) != 0)) {
748 radlog(L_ERR, "FATAL: Failed to initialize wait mutex: %s",
754 * Create the hash table of child PID's
756 thread_pool.waiters = lrad_hash_table_create(pid_hash,
759 if (!thread_pool.waiters) {
760 radlog(L_ERR, "FATAL: Failed to set up wait hash");
766 * We're not spawning new threads, don't do
769 if (!spawn_flag) return 0;
771 pool_cf = cf_section_find("thread");
772 if (pool_cf != NULL) {
774 * FIXME: Check for errors?
776 cf_section_parse(pool_cf, NULL, thread_config);
780 * The pool has already been initialized. Don't spawn
781 * new threads, and don't forget about forked children,
783 if (pool_initialized) {
788 * Initialize the queue of requests.
790 memset(&thread_pool.semaphore, 0, sizeof(thread_pool.semaphore));
791 rcode = sem_init(&thread_pool.semaphore, 0, SEMAPHORE_LOCKED);
793 radlog(L_ERR|L_CONS, "FATAL: Failed to initialize semaphore: %s",
798 rcode = pthread_mutex_init(&thread_pool.queue_mutex,NULL);
800 radlog(L_ERR, "FATAL: Failed to initialize queue mutex: %s",
806 * Allocate multiple fifos.
808 for (i = 0; i < NUM_FIFOS; i++) {
809 thread_pool.fifo[i] = lrad_fifo_create(65536, NULL);
810 if (!thread_pool.fifo[i]) {
811 radlog(L_ERR, "FATAL: Failed to set up request fifo");
816 #ifdef HAVE_OPENSSL_CRYPTO_H
818 * If we're linking with OpenSSL too, then we need
819 * to set up the mutexes and enable the thread callbacks.
821 if (!setup_ssl_mutexes()) {
822 radlog(L_ERR, "FATAL: Failed to set up SSL mutexes");
829 * Create a number of waiting threads.
831 * If we fail while creating them, do something intelligent.
833 for (i = 0; i < thread_pool.start_threads; i++) {
834 if (spawn_thread(now) == NULL) {
839 DEBUG2("Thread pool initialized");
840 pool_initialized = TRUE;
846 * Assign a new request to a free thread.
848 * If there isn't a free thread, then try to create a new one,
849 * up to the configured limits.
851 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
854 * We've been told not to spawn threads, so don't.
856 if (!thread_pool.spawn_flag) {
857 request->finished = rad_respond(request, fun);
862 * Add the new request to the queue.
864 if (!request_enqueue(request, fun)) return 0;
867 * If the thread pool is busy handling requests, then
868 * try to spawn another one. We don't acquire the mutex
869 * before reading active_threads, so our thread count is
870 * just an estimate. It's fine to go ahead and spawn an
871 * extra thread in that case.
872 * NOTE: the log message may be in error since active_threads
873 * is an estimate, but it's only in error about the thread
874 * count, not about the fact that we can't create a new one.
876 if (thread_pool.active_threads == thread_pool.total_threads) {
877 if (spawn_thread(request->timestamp) == NULL) {
879 "The maximum number of threads (%d) are active, cannot spawn new thread to handle request",
880 thread_pool.max_threads);
889 * Check the min_spare_threads and max_spare_threads.
891 * If there are too many or too few threads waiting, then we
892 * either create some more, or delete some.
894 int thread_pool_clean(time_t now)
898 THREAD_HANDLE *handle, *next;
900 static time_t last_cleaned = 0;
903 * Loop over the thread pool deleting exited threads.
905 for (handle = thread_pool.head; handle; handle = next) {
909 * Maybe we've asked the thread to exit, and it
912 if (handle->status == THREAD_EXITED) {
913 delete_thread(handle);
918 * We don't need a mutex lock here, as we're reading
919 * active_threads, and not modifying it. We want a close
920 * approximation of the number of active threads, and this
923 active_threads = thread_pool.active_threads;
924 spare = thread_pool.total_threads - active_threads;
926 static int old_total = -1;
927 static int old_active = -1;
929 if ((old_total != thread_pool.total_threads) ||
930 (old_active != active_threads)) {
931 DEBUG2("Threads: total/active/spare threads = %d/%d/%d",
932 thread_pool.total_threads, active_threads, spare);
933 old_total = thread_pool.total_threads;
934 old_active = active_threads;
939 * If there are too few spare threads, create some more.
941 if (spare < thread_pool.min_spare_threads) {
942 total = thread_pool.min_spare_threads - spare;
944 DEBUG2("Threads: Spawning %d spares", total);
946 * Create a number of spare threads.
948 for (i = 0; i < total; i++) {
949 handle = spawn_thread(now);
950 if (handle == NULL) {
956 * And exit, as there can't be too many spare threads.
962 * Only delete spare threads if we haven't already done
965 if (now == last_cleaned) {
971 * Only delete the spare threads if sufficient time has
972 * passed since we last created one. This helps to minimize
973 * the amount of create/delete cycles.
975 if ((now - thread_pool.time_last_spawned) < thread_pool.cleanup_delay) {
980 * If there are too many spare threads, delete one.
982 * Note that we only delete ONE at a time, instead of
983 * wiping out many. This allows the excess servers to
984 * be slowly reaped, just in case the load spike comes again.
986 if (spare > thread_pool.max_spare_threads) {
988 spare -= thread_pool.max_spare_threads;
990 DEBUG2("Threads: deleting 1 spare out of %d spares", spare);
993 * Walk through the thread pool, deleting the
994 * first idle thread we come across.
996 for (handle = thread_pool.head; (handle != NULL) && (spare > 0) ; handle = next) {
1000 * If the thread is not handling a
1001 * request, but still live, then tell it
1004 * It will eventually wake up, and realize
1005 * it's been told to commit suicide.
1007 if ((handle->request == NULL) &&
1008 (handle->status == THREAD_RUNNING)) {
1009 handle->status = THREAD_CANCELLED;
1011 * Post an extra semaphore, as a
1012 * signal to wake up, and exit.
1014 sem_post(&thread_pool.semaphore);
1022 * If the thread has handled too many requests, then make it
1025 if (thread_pool.max_requests_per_thread > 0) {
1026 for (handle = thread_pool.head; handle; handle = next) {
1027 next = handle->next;
1030 * Not handling a request, but otherwise
1031 * live, we can kill it.
1033 if ((handle->request == NULL) &&
1034 (handle->status == THREAD_RUNNING) &&
1035 (handle->request_count > thread_pool.max_requests_per_thread)) {
1036 handle->status = THREAD_CANCELLED;
1037 sem_post(&thread_pool.semaphore);
1043 * Otherwise everything's kosher. There are not too few,
1044 * or too many spare threads. Exit happily.
1051 * Thread wrapper for fork().
1053 pid_t rad_fork(int exec_wait)
1057 if (exec_wait) return fork();
1059 reap_children(); /* be nice to non-wait thingies */
1061 if (lrad_hash_table_num_elements(thread_pool.waiters) >= 1024) {
1066 * Fork & save the PID for later reaping.
1069 if (child_pid > 0) {
1073 tf = rad_malloc(sizeof(*tf));
1074 memset(tf, 0, sizeof(*tf));
1076 tf->pid = child_pid;
1081 pthread_mutex_lock(&thread_pool.wait_mutex);
1083 rcode = lrad_hash_table_insert(thread_pool.waiters, tf);
1088 pthread_mutex_unlock(&thread_pool.wait_mutex);
1091 radlog(L_ERR, "Failed to store PID, creating what will be a zombie process %d",
1097 * Return whatever we were told.
1103 * We may not need this any more...
1105 pid_t rad_waitpid(pid_t pid, int *status, int options)
1107 thread_fork_t mytf, *tf;
1109 reap_children(); /* be nice to non-wait thingies */
1111 if (pid <= 0) return -1;
1113 if ((options & WNOHANG) == 0) return -1;
1117 pthread_mutex_lock(&thread_pool.wait_mutex);
1118 tf = lrad_hash_table_finddata(thread_pool.waiters, &mytf);
1120 if (!tf) { /* not found. It's a problem... */
1121 pthread_mutex_unlock(&thread_pool.wait_mutex);
1122 return waitpid(pid, status, options);
1126 *status = tf->status;
1127 lrad_hash_table_delete(thread_pool.waiters, &mytf);
1128 pthread_mutex_unlock(&thread_pool.wait_mutex);
1133 * Don't wait, and it hasn't exited. Return.
1135 pthread_mutex_unlock(&thread_pool.wait_mutex);
1139 #else /* HAVE_PTHREAD_H */
1141 * "thread" code when we don't have threads.
1143 int thread_pool_init(int spawn_flag)
1149 * call "radrespond".
1151 int thread_pool_addrequest(REQUEST *request, RAD_REQUEST_FUNP fun)
1153 rad_respond(request, fun);
1157 #endif /* HAVE_PTHREAD_H */