4 * Citadel "system dependent" stuff.
5 * See COPYING for copyright information.
7 * Here's where we have the Citadel thread implimentation
14 #include <sys/types.h>
16 #include <sys/socket.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
32 #include <libcitadel.h>
35 #include "ctdl_module.h"
36 #include "modules_init.h"
37 #include "housekeeping.h"
39 #include "citserver.h"
40 #include "sysdep_decls.h"
43 * define this to use the new worker_thread method of handling connections
48 * New thread interface.
49 * To create a thread you must call one of the create thread functions.
50 * You must pass it the address of (a pointer to a CtdlThreadNode initialised to NULL) like this
51 * struct CtdlThreadNode *node = NULL;
53 * If the thread is created *node will point to the thread control structure for the created thread.
54 * If the thread creation fails *node remains NULL
55 * Do not free the memory pointed to by *node, it doesn't belong to you.
56 * This new interface duplicates much of the eCrash stuff. We should go for closer integration since that would
57 * remove the need for the calls to eCrashRegisterThread and friends
60 static int num_threads = 0; /* Current number of threads */
61 static int num_workers = 0; /* Current number of worker threads */
63 CtdlThreadNode *CtdlThreadList = NULL;
64 CtdlThreadNode *CtdlThreadSchedList = NULL;
66 static CtdlThreadNode *GC_thread = NULL;
67 static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE];
68 double CtdlThreadLoadAvg = 0;
69 double CtdlThreadWorkerAvg = 0;
70 citthread_key_t ThreadKey;
72 citthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
76 void InitialiseSemaphores(void)
80 /* Set up a bunch of semaphores to be used for critical sections */
81 for (i=0; i<MAX_SEMAPHORES; ++i) {
82 citthread_mutex_init(&Critters[i], NULL);
90 * Obtain a semaphore lock to begin a critical section.
91 * but only if no one else has one
93 int try_critical_section(int which_one)
95 /* For all types of critical sections except those listed here,
96 * ensure nobody ever tries to do a critical section within a
97 * transaction; this could lead to deadlock.
99 if ( (which_one != S_FLOORCACHE)
100 #ifdef DEBUG_MEMORY_LEAKS
101 && (which_one != S_DEBUGMEMLEAKS)
103 && (which_one != S_RPLIST)
107 return (citthread_mutex_trylock(&Critters[which_one]));
112 * Obtain a semaphore lock to begin a critical section.
114 void begin_critical_section(int which_one)
116 /* CtdlLogPrintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
118 /* For all types of critical sections except those listed here,
119 * ensure nobody ever tries to do a critical section within a
120 * transaction; this could lead to deadlock.
122 if ( (which_one != S_FLOORCACHE)
123 #ifdef DEBUG_MEMORY_LEAKS
124 && (which_one != S_DEBUGMEMLEAKS)
126 && (which_one != S_RPLIST)
130 citthread_mutex_lock(&Critters[which_one]);
134 * Release a semaphore lock to end a critical section.
136 void end_critical_section(int which_one)
138 citthread_mutex_unlock(&Critters[which_one]);
143 * A function to destroy the TSD
145 static void ctdl_thread_internal_dest_tsd(void *arg)
155 * A function to initialise the thread TSD
157 void ctdl_thread_internal_init_tsd(void)
161 if ((ret = citthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
162 CtdlLogPrintf(CTDL_EMERG, "citthread_key_create: %s\n", strerror(ret));
168 * Ensure that we have a key for thread-specific data.
170 * This should be called immediately after startup by any thread
173 void CtdlThreadAllocTSD(void)
177 if (citthread_getspecific(ThreadKey) != NULL)
180 tsd = malloc(sizeof(ThreadTSD));
184 memset(tsd->cursors, 0, sizeof tsd->cursors);
187 citthread_setspecific(ThreadKey, tsd);
191 void ctdl_thread_internal_free_tsd(void)
193 ctdl_thread_internal_dest_tsd(citthread_getspecific(ThreadKey));
194 citthread_setspecific(ThreadKey, NULL);
198 void ctdl_thread_internal_cleanup(void)
201 CtdlThreadNode *this_thread, *that_thread;
203 for (i=0; i<CTDL_THREAD_LAST_STATE; i++)
205 free (CtdlThreadStates[i]);
208 /* Clean up the scheduled thread list */
209 this_thread = CtdlThreadSchedList;
212 that_thread = this_thread;
213 this_thread = this_thread->next;
214 citthread_mutex_destroy(&that_thread->ThreadMutex);
215 citthread_cond_destroy(&that_thread->ThreadCond);
216 citthread_mutex_destroy(&that_thread->SleepMutex);
217 citthread_cond_destroy(&that_thread->SleepCond);
218 citthread_attr_destroy(&that_thread->attr);
221 ctdl_thread_internal_free_tsd();
224 void ctdl_thread_internal_init(void)
226 CtdlThreadNode *this_thread;
229 CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread");
230 CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread");
231 CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created");
232 CtdlThreadStates[CTDL_THREAD_CANCELLED] = strdup("Thread Cancelled");
233 CtdlThreadStates[CTDL_THREAD_EXITED] = strdup("Thread Exited");
234 CtdlThreadStates[CTDL_THREAD_STOPPING] = strdup("Thread Stopping");
235 CtdlThreadStates[CTDL_THREAD_STOP_REQ] = strdup("Thread Stop Requested");
236 CtdlThreadStates[CTDL_THREAD_SLEEPING] = strdup("Thread Sleeping");
237 CtdlThreadStates[CTDL_THREAD_RUNNING] = strdup("Thread Running");
238 CtdlThreadStates[CTDL_THREAD_BLOCKED] = strdup("Thread Blocked");
240 /* Get ourself a thread entry */
241 this_thread = malloc(sizeof(CtdlThreadNode));
242 if (this_thread == NULL) {
243 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
246 // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
247 memset (this_thread, 0, sizeof(CtdlThreadNode));
249 citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
250 citthread_cond_init (&(this_thread->ThreadCond), NULL);
251 citthread_mutex_init (&(this_thread->SleepMutex), NULL);
252 citthread_cond_init (&(this_thread->SleepCond), NULL);
254 /* We are garbage collector so create us as running */
255 this_thread->state = CTDL_THREAD_RUNNING;
257 if ((ret = citthread_attr_init(&this_thread->attr))) {
258 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
263 this_thread->name = "Garbage Collection Thread";
265 this_thread->tid = citthread_self();
266 GC_thread = this_thread;
269 num_threads++; // Increase the count of threads in the system.
271 this_thread->next = CtdlThreadList;
272 CtdlThreadList = this_thread;
273 if (this_thread->next)
274 this_thread->next->prev = this_thread;
275 /* Set up start times */
276 gettimeofday(&this_thread->start_time, NULL); /* Time this thread started */
277 memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval)); /* Changed state so mark it. */
282 * A function to update a threads load averages
284 void ctdl_thread_internal_update_avgs(CtdlThreadNode *this_thread)
286 struct timeval now, result;
287 double last_duration;
289 gettimeofday(&now, NULL);
290 timersub(&now, &(this_thread->last_state_change), &result);
291 /* I don't think these mutex's are needed here */
292 citthread_mutex_lock(&this_thread->ThreadMutex);
293 // result now has a timeval for the time we spent in the last state since we last updated
294 last_duration = (double)result.tv_sec + ((double)result.tv_usec / (double) 1000000);
295 if (this_thread->state == CTDL_THREAD_SLEEPING)
296 this_thread->avg_sleeping += last_duration;
297 if (this_thread->state == CTDL_THREAD_RUNNING)
298 this_thread->avg_running += last_duration;
299 if (this_thread->state == CTDL_THREAD_BLOCKED)
300 this_thread->avg_blocked += last_duration;
301 memcpy (&this_thread->last_state_change, &now, sizeof (struct timeval));
302 citthread_mutex_unlock(&this_thread->ThreadMutex);
306 * A function to chenge the state of a thread
308 void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlThreadState new_state)
311 * Wether we change state or not we need update the load values
313 ctdl_thread_internal_update_avgs(this_thread);
314 /* This mutex not needed here? */
315 citthread_mutex_lock(&this_thread->ThreadMutex); /* To prevent race condition of a sleeping thread */
316 if ((new_state == CTDL_THREAD_STOP_REQ) && (this_thread->state > CTDL_THREAD_STOP_REQ))
317 this_thread->state = new_state;
318 if (((new_state == CTDL_THREAD_SLEEPING) || (new_state == CTDL_THREAD_BLOCKED)) && (this_thread->state == CTDL_THREAD_RUNNING))
319 this_thread->state = new_state;
320 if ((new_state == CTDL_THREAD_RUNNING) && ((this_thread->state == CTDL_THREAD_SLEEPING) || (this_thread->state == CTDL_THREAD_BLOCKED)))
321 this_thread->state = new_state;
322 citthread_mutex_unlock(&this_thread->ThreadMutex);
327 * A function to tell all threads to exit
329 void CtdlThreadStopAll(void)
331 /* First run any registered shutdown hooks. This probably doesn't belong here. */
332 PerformSessionHooks(EVT_SHUTDOWN);
334 //FIXME: The signalling of the condition should not be in the critical_section
335 // We need to build a list of threads we are going to signal and then signal them afterwards
337 CtdlThreadNode *this_thread;
339 begin_critical_section(S_THREAD_LIST);
340 this_thread = CtdlThreadList;
341 // Ask the GC thread to stop first so everything knows we are shutting down.
342 GC_thread->state = CTDL_THREAD_STOP_REQ;
345 #ifdef THREADS_USESIGNALS
346 if (!citthread_equal(this_thread->tid, GC_thread->tid))
347 citthread_kill(this_thread->tid, SIGHUP);
349 ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
350 citthread_cond_signal(&this_thread->ThreadCond);
351 citthread_cond_signal(&this_thread->SleepCond);
352 this_thread->stop_ticker = time(NULL);
353 CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (0x%08lx).\n",
354 this_thread->name, this_thread->tid);
355 this_thread = this_thread->next;
357 end_critical_section(S_THREAD_LIST);
362 * A function to wake up all sleeping threads
364 void CtdlThreadWakeAll(void)
366 CtdlThreadNode *this_thread;
368 CtdlLogPrintf(CTDL_DEBUG, "Thread system waking all threads.\n");
370 begin_critical_section(S_THREAD_LIST);
371 this_thread = CtdlThreadList;
374 if (!this_thread->thread_func)
376 citthread_cond_signal(&this_thread->ThreadCond);
377 citthread_cond_signal(&this_thread->SleepCond);
379 this_thread = this_thread->next;
381 end_critical_section(S_THREAD_LIST);
386 * A function to return the number of threads running in the system
388 int CtdlThreadGetCount(void)
393 int CtdlThreadGetWorkers(void)
398 double CtdlThreadGetWorkerAvg(void)
402 begin_critical_section(S_THREAD_LIST);
403 ret = CtdlThreadWorkerAvg;
404 end_critical_section(S_THREAD_LIST);
408 double CtdlThreadGetLoadAvg(void)
412 begin_critical_section(S_THREAD_LIST);
413 ret = CtdlThreadLoadAvg;
414 end_critical_section(S_THREAD_LIST);
422 * A function to rename a thread
423 * Returns a const char *
425 const char *CtdlThreadName(const char *name)
427 const char *old_name;
431 CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
442 * A function to force a thread to exit
444 void CtdlThreadCancel(CtdlThreadNode *thread)
446 CtdlThreadNode *this_thread;
451 this_thread = thread;
454 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
459 if (!this_thread->thread_func)
461 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() the garbage collector.\n");
466 ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED);
467 citthread_cancel(this_thread->tid);
472 * A function for a thread to check if it has been asked to stop
474 int CtdlThreadCheckStop(void)
480 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
487 #ifdef THREADS_USESIGNALS
490 CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
494 if(state == CTDL_THREAD_STOP_REQ)
496 CT->state = CTDL_THREAD_STOPPING;
499 else if((state < CTDL_THREAD_STOP_REQ) && (state > CTDL_THREAD_CREATE))
508 * A function to ask a thread to exit
509 * The thread must call CtdlThreadCheckStop() periodically to determine if it should exit
511 void CtdlThreadStop(CtdlThreadNode *thread)
513 CtdlThreadNode *this_thread;
518 this_thread = thread;
521 if (!(this_thread->thread_func))
522 return; // Don't stop garbage collector
523 #ifdef THREADS_USESIGNALS
524 if (!citthread_equal(this_thread->tid, GC_thread->tid))
525 citthread_kill(this_thread->tid, SIGHUP);
527 ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
528 citthread_cond_signal(&this_thread->ThreadCond);
529 citthread_cond_signal(&this_thread->SleepCond);
530 this_thread->stop_ticker = time(NULL);
534 * So we now have a sleep command that works with threads but it is in seconds
536 void CtdlThreadSleep(int secs)
538 struct timespec wake_time;
539 struct timeval time_now;
544 CtdlLogPrintf(CTDL_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
548 memset (&wake_time, 0, sizeof(struct timespec));
549 gettimeofday(&time_now, NULL);
550 wake_time.tv_sec = time_now.tv_sec + secs;
551 wake_time.tv_nsec = time_now.tv_usec * 10;
553 ctdl_thread_internal_change_state (CT, CTDL_THREAD_SLEEPING);
555 citthread_mutex_lock(&CT->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
556 citthread_cond_timedwait(&CT->SleepCond, &CT->ThreadMutex, &wake_time);
557 citthread_mutex_unlock(&CT->ThreadMutex);
559 ctdl_thread_internal_change_state (CT, CTDL_THREAD_RUNNING);
564 * Routine to clean up our thread function on exit
566 static void ctdl_internal_thread_cleanup(void *arg)
569 * In here we were called by the current thread because it is exiting
570 * NB. WE ARE THE CURRENT THREAD
572 CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", CT->name, CT->tid);
574 #ifdef HAVE_BACKTRACE
575 eCrash_UnregisterThread();
578 citthread_mutex_lock(&CT->ThreadMutex);
579 CT->state = CTDL_THREAD_EXITED; // needs to be last thing else house keeping will unlink us too early
580 citthread_mutex_unlock(&CT->ThreadMutex);
584 * A quick function to show the load averages
586 void ctdl_thread_internal_calc_loadavg(void)
588 CtdlThreadNode *that_thread;
589 double load_avg, worker_avg;
592 that_thread = CtdlThreadList;
597 /* Update load averages */
598 ctdl_thread_internal_update_avgs(that_thread);
599 citthread_mutex_lock(&that_thread->ThreadMutex);
600 that_thread->load_avg = (that_thread->avg_sleeping + that_thread->avg_running) / (that_thread->avg_sleeping + that_thread->avg_running + that_thread->avg_blocked) * 100;
601 that_thread->avg_sleeping /= 2;
602 that_thread->avg_running /= 2;
603 that_thread->avg_blocked /= 2;
604 load_avg += that_thread->load_avg;
605 if (that_thread->flags & CTDLTHREAD_WORKER)
607 worker_avg += that_thread->load_avg;
610 #ifdef WITH_THREADLOG
611 CtdlLogPrintf(CTDL_DEBUG, "CtdlThread, \"%s\" (%lu) \"%s\" %.2f %.2f %.2f %.2f\n",
614 CtdlThreadStates[that_thread->state],
615 that_thread->avg_sleeping,
616 that_thread->avg_running,
617 that_thread->avg_blocked,
618 that_thread->load_avg);
620 citthread_mutex_unlock(&that_thread->ThreadMutex);
621 that_thread = that_thread->next;
623 CtdlThreadLoadAvg = load_avg/num_threads;
624 CtdlThreadWorkerAvg = worker_avg/workers;
625 #ifdef WITH_THREADLOG
626 CtdlLogPrintf(CTDL_INFO, "System load average %.2f, workers averag %.2f, threads %d, workers %d, sessions %d\n", CtdlThreadLoadAvg, CtdlThreadWorkerAvg, num_threads, num_workers, num_sessions);
632 * Garbage collection routine.
633 * Gets called by main() in a loop to clean up the thread list periodically.
635 void CtdlThreadGC (void)
637 CtdlThreadNode *this_thread, *that_thread;
638 int workers = 0, sys_workers;
641 begin_critical_section(S_THREAD_LIST);
643 /* Handle exiting of garbage collector thread */
645 CtdlThreadList->state = CTDL_THREAD_EXITED;
647 #ifdef WITH_THREADLOG
648 CtdlLogPrintf(CTDL_DEBUG, "Thread system running garbage collection.\n");
651 * Woke up to do garbage collection
653 this_thread = CtdlThreadList;
656 that_thread = this_thread;
657 this_thread = this_thread->next;
659 if ((that_thread->state == CTDL_THREAD_STOP_REQ || that_thread->state == CTDL_THREAD_STOPPING)
660 && (!citthread_equal(that_thread->tid, citthread_self())))
661 CtdlLogPrintf(CTDL_DEBUG, "Waiting for thread %s (0x%08lx) to exit.\n", that_thread->name, that_thread->tid);
665 * Catch the situation where a worker was asked to stop but couldn't and we are not
668 that_thread->stop_ticker = 0;
671 if (that_thread->stop_ticker + 5 == time(NULL))
673 CtdlLogPrintf(CTDL_DEBUG, "Thread System: The thread \"%s\" (0x%08lx) failed to self terminate within 5 ticks. It would be cancelled now.\n", that_thread->name, that_thread->tid);
674 if ((that_thread->flags & CTDLTHREAD_WORKER) == 0)
675 CtdlLogPrintf(CTDL_INFO, "Thread System: A non worker thread would have been canceled this may cause message loss.\n");
676 // that_thread->state = CTDL_THREAD_CANCELLED;
677 that_thread->stop_ticker++;
678 // citthread_cancel(that_thread->tid);
682 /* Do we need to clean up this thread? */
683 if ((that_thread->state != CTDL_THREAD_EXITED) && (that_thread->state != CTDL_THREAD_CANCELLED))
685 if(that_thread->flags & CTDLTHREAD_WORKER)
686 workers++; /* Sanity check on number of worker threads */
690 if (citthread_equal(that_thread->tid, citthread_self()) && that_thread->thread_func)
692 end_critical_section(S_THREAD_LIST);
693 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, a thread is trying to clean up after itself.\n");
698 if (num_threads <= 0)
700 end_critical_section(S_THREAD_LIST);
701 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, num_threads <= 0 and trying to do Garbage Collection.\n");
706 if(that_thread->flags & CTDLTHREAD_WORKER)
707 num_workers--; /* This is a wroker thread so reduce the count. */
709 /* If we are unlinking the list head then the next becomes the list head */
710 if(that_thread->prev)
711 that_thread->prev->next = that_thread->next;
713 CtdlThreadList = that_thread->next;
714 if(that_thread->next)
715 that_thread->next->prev = that_thread->prev;
717 citthread_cond_signal(&that_thread->ThreadCond);
718 citthread_cond_signal(&that_thread->SleepCond); // Make sure this thread is awake
719 citthread_mutex_lock(&that_thread->ThreadMutex); // Make sure it has done what its doing
720 citthread_mutex_unlock(&that_thread->ThreadMutex);
722 * Join on the thread to do clean up and prevent memory leaks
723 * Also makes sure the thread has cleaned up after itself before we remove it from the list
724 * We can join on the garbage collector thread the join should just return EDEADLCK
726 ret = citthread_join (that_thread->tid, NULL);
728 CtdlLogPrintf(CTDL_DEBUG, "Garbage collection on own thread.\n");
729 else if (ret == EINVAL)
730 CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, that thread already joined on.\n");
731 else if (ret == ESRCH)
732 CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, no thread to join on.\n");
734 CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, citthread_join returned an unknown error(%d).\n", ret);
736 * Now we own that thread entry
738 CtdlLogPrintf(CTDL_INFO, "Garbage Collection for thread \"%s\" (0x%08lx).\n",
739 that_thread->name, that_thread->tid);
740 citthread_mutex_destroy(&that_thread->ThreadMutex);
741 citthread_cond_destroy(&that_thread->ThreadCond);
742 citthread_mutex_destroy(&that_thread->SleepMutex);
743 citthread_cond_destroy(&that_thread->SleepCond);
744 citthread_attr_destroy(&that_thread->attr);
747 sys_workers = num_workers;
748 end_critical_section(S_THREAD_LIST);
750 /* Sanity check number of worker threads */
751 if (workers != sys_workers)
753 CtdlLogPrintf(CTDL_EMERG,
754 "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
765 * Runtime function for a Citadel Thread.
766 * This initialises the threads environment and then calls the user supplied thread function
767 * Note that this is the REAL thread function and wraps the users thread function.
769 static void *ctdl_internal_thread_func (void *arg)
771 CtdlThreadNode *this_thread;
774 /* lock and unlock the thread list.
775 * This causes this thread to wait until all its creation stuff has finished before it
776 * can continue its execution.
778 begin_critical_section(S_THREAD_LIST);
779 this_thread = (CtdlThreadNode *) arg;
780 gettimeofday(&this_thread->start_time, NULL); /* Time this thread started */
781 // citthread_mutex_lock(&this_thread->ThreadMutex);
783 // Register the cleanup function to take care of when we exit.
784 citthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
785 // Get our thread data structure
786 CtdlThreadAllocTSD();
788 this_thread->pid = getpid();
789 memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval)); /* Changed state so mark it. */
790 /* Only change to running state if we weren't asked to stop during the create cycle
791 * Other wise there is a window to allow this threads creation to continue to full grown and
792 * therby prevent a shutdown of the server.
794 // citthread_mutex_unlock(&this_thread->ThreadMutex);
796 if (!CtdlThreadCheckStop())
798 citthread_mutex_lock(&this_thread->ThreadMutex);
799 this_thread->state = CTDL_THREAD_RUNNING;
800 citthread_mutex_unlock(&this_thread->ThreadMutex);
802 end_critical_section(S_THREAD_LIST);
804 // Register for tracing
805 #ifdef HAVE_BACKTRACE
806 eCrash_RegisterThread(this_thread->name, 0);
809 // Tell the world we are here
810 CtdlLogPrintf(CTDL_NOTICE, "Created a new thread \"%s\" (0x%08lx).\n",
811 this_thread->name, this_thread->tid);
814 * run the thread to do the work but only if we haven't been asked to stop
816 if (!CtdlThreadCheckStop())
817 ret = (this_thread->thread_func)(this_thread->user_args);
820 * Our thread is exiting either because it wanted to end or because the server is stopping
821 * We need to clean up
823 citthread_cleanup_pop(1); // Execute our cleanup routine and remove it
832 * Function to initialise an empty thread structure
834 CtdlThreadNode *ctdl_internal_init_thread_struct(CtdlThreadNode *this_thread, long flags)
838 // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
839 memset (this_thread, 0, sizeof(CtdlThreadNode));
841 /* Create the mutex's early so we can use them */
842 citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
843 citthread_cond_init (&(this_thread->ThreadCond), NULL);
844 citthread_mutex_init (&(this_thread->SleepMutex), NULL);
845 citthread_cond_init (&(this_thread->SleepCond), NULL);
847 this_thread->state = CTDL_THREAD_CREATE;
849 if ((ret = citthread_attr_init(&this_thread->attr))) {
850 citthread_mutex_unlock(&this_thread->ThreadMutex);
851 citthread_mutex_destroy(&(this_thread->ThreadMutex));
852 citthread_cond_destroy(&(this_thread->ThreadCond));
853 citthread_mutex_destroy(&(this_thread->SleepMutex));
854 citthread_cond_destroy(&(this_thread->SleepCond));
855 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
860 /* Our per-thread stacks need to be bigger than the default size,
861 * otherwise the MIME parser crashes on FreeBSD, and the IMAP service
862 * crashes on 64-bit Linux.
864 if (flags & CTDLTHREAD_BIGSTACK)
866 #ifdef WITH_THREADLOG
867 CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n");
869 if ((ret = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
870 citthread_mutex_unlock(&this_thread->ThreadMutex);
871 citthread_mutex_destroy(&(this_thread->ThreadMutex));
872 citthread_cond_destroy(&(this_thread->ThreadCond));
873 citthread_mutex_destroy(&(this_thread->SleepMutex));
874 citthread_cond_destroy(&(this_thread->SleepCond));
875 citthread_attr_destroy(&this_thread->attr);
876 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_setstacksize: %s\n",
883 /* Set this new thread with an avg_blocked of 2. We do this so that its creation affects the
884 * load average for the system. If we don't do this then we create a mass of threads at the same time
885 * because the creation didn't affect the load average.
887 this_thread->avg_blocked = 2;
889 return (this_thread);
896 * Internal function to create a thread.
898 CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args)
901 CtdlThreadNode *this_thread;
903 if (num_threads >= 32767)
905 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
909 this_thread = malloc(sizeof(CtdlThreadNode));
910 if (this_thread == NULL) {
911 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
915 /* Initialise the thread structure */
916 if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
919 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
923 * If we got here we are going to create the thread so we must initilise the structure
924 * first because most implimentations of threading can't create it in a stopped state
925 * and it might want to do things with its structure that aren't initialised otherwise.
929 this_thread->name = name;
933 this_thread->name = "Un-named Thread";
936 this_thread->flags = flags;
937 this_thread->thread_func = thread_func;
938 this_thread->user_args = args;
940 // citthread_mutex_lock(&this_thread->ThreadMutex);
942 begin_critical_section(S_THREAD_LIST);
944 * We pass this_thread into the thread as its args so that it can find out information
945 * about itself and it has a bit of storage space for itself, not to mention that the REAL
946 * thread function needs to finish off the setup of the structure
948 if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
950 end_critical_section(S_THREAD_LIST);
951 CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
953 citthread_mutex_unlock(&this_thread->ThreadMutex);
954 citthread_mutex_destroy(&(this_thread->ThreadMutex));
955 citthread_cond_destroy(&(this_thread->ThreadCond));
956 citthread_mutex_destroy(&(this_thread->SleepMutex));
957 citthread_cond_destroy(&(this_thread->SleepCond));
958 citthread_attr_destroy(&this_thread->attr);
963 num_threads++; // Increase the count of threads in the system.
964 if(this_thread->flags & CTDLTHREAD_WORKER)
967 this_thread->next = CtdlThreadList;
968 CtdlThreadList = this_thread;
969 if (this_thread->next)
970 this_thread->next->prev = this_thread;
971 ctdl_thread_internal_calc_loadavg();
973 // citthread_mutex_unlock(&this_thread->ThreadMutex);
974 end_critical_section(S_THREAD_LIST);
980 * Wrapper function to create a thread
981 * ensures the critical section and other protections are in place.
982 * char *name = name to give to thread, if NULL, use generic name
983 * int flags = flags to determine type of thread and standard facilities
985 CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args)
987 CtdlThreadNode *ret = NULL;
989 ret = ctdl_internal_create_thread(name, flags, thread_func, args);
996 * Internal function to schedule a thread.
997 * Must be called from within a S_THREAD_LIST critical section
999 CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) (void *arg), void *args, time_t when)
1001 CtdlThreadNode *this_thread;
1003 if (num_threads >= 32767)
1005 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
1009 this_thread = malloc(sizeof(CtdlThreadNode));
1010 if (this_thread == NULL) {
1011 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
1014 /* Initialise the thread structure */
1015 if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
1018 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
1023 * If we got here we are going to create the thread so we must initilise the structure
1024 * first because most implimentations of threading can't create it in a stopped state
1025 * and it might want to do things with its structure that aren't initialised otherwise.
1029 this_thread->name = name;
1033 this_thread->name = "Un-named Thread";
1036 this_thread->flags = flags;
1037 this_thread->thread_func = thread_func;
1038 this_thread->user_args = args;
1041 * When to start this thread
1043 this_thread->when = when;
1045 begin_critical_section(S_SCHEDULE_LIST);
1046 this_thread->next = CtdlThreadSchedList;
1047 CtdlThreadSchedList = this_thread;
1048 if (this_thread->next)
1049 this_thread->next->prev = this_thread;
1050 end_critical_section(S_SCHEDULE_LIST);
1057 CtdlThreadNode *ctdl_thread_internal_start_scheduled (CtdlThreadNode *this_thread)
1061 // citthread_mutex_lock(&that_thread->ThreadMutex);
1062 begin_critical_section(S_THREAD_LIST);
1064 * We pass this_thread into the thread as its args so that it can find out information
1065 * about itself and it has a bit of storage space for itself, not to mention that the REAL
1066 * thread function needs to finish off the setup of the structure
1068 if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
1070 end_critical_section(S_THREAD_LIST);
1071 CtdlLogPrintf(CTDL_DEBUG, "Failed to start scheduled thread \"%s\": %s\n", this_thread->name, strerror(ret));
1072 // citthread_mutex_unlock(&this_thread->ThreadMutex);
1073 citthread_mutex_destroy(&(this_thread->ThreadMutex));
1074 citthread_cond_destroy(&(this_thread->ThreadCond));
1075 citthread_mutex_destroy(&(this_thread->SleepMutex));
1076 citthread_cond_destroy(&(this_thread->SleepCond));
1077 citthread_attr_destroy(&this_thread->attr);
1083 num_threads++; // Increase the count of threads in the system.
1084 if(this_thread->flags & CTDLTHREAD_WORKER)
1087 this_thread->next = CtdlThreadList;
1088 CtdlThreadList = this_thread;
1089 if (this_thread->next)
1090 this_thread->next->prev = this_thread;
1091 // citthread_mutex_unlock(&that_thread->ThreadMutex);
1093 ctdl_thread_internal_calc_loadavg();
1094 end_critical_section(S_THREAD_LIST);
1102 void ctdl_thread_internal_check_scheduled(void)
1104 CtdlThreadNode *this_thread, *that_thread;
1107 /* Don't start scheduled threads if the system wants single user mode */
1108 if (CtdlWantSingleUser())
1111 if (try_critical_section(S_SCHEDULE_LIST))
1112 return; /* If this list is locked we wait till the next chance */
1116 #ifdef WITH_THREADLOG
1117 CtdlLogPrintf(CTDL_DEBUG, "Checking for scheduled threads to start.\n");
1120 this_thread = CtdlThreadSchedList;
1123 that_thread = this_thread;
1124 this_thread = this_thread->next;
1126 if (now > that_thread->when)
1128 /* Unlink from schedule list */
1129 if (that_thread->prev)
1130 that_thread->prev->next = that_thread->next;
1132 CtdlThreadSchedList = that_thread->next;
1133 if (that_thread->next)
1134 that_thread->next->prev = that_thread->prev;
1136 that_thread->next = that_thread->prev = NULL;
1137 #ifdef WITH_THREADLOG
1138 CtdlLogPrintf(CTDL_DEBUG, "About to start scheduled thread \"%s\".\n", that_thread->name);
1140 if (CT->state > CTDL_THREAD_STOP_REQ)
1141 { /* Only start it if the system is not stopping */
1142 if (ctdl_thread_internal_start_scheduled (that_thread))
1144 #ifdef WITH_THREADLOG
1145 CtdlLogPrintf(CTDL_INFO, "Thread system, Started a scheduled thread \"%s\" (0x%08lx).\n",
1146 that_thread->name, that_thread->tid);
1151 #ifdef WITH_THREADLOG
1154 CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" will start in %ld seconds.\n",
1155 that_thread->name, that_thread->when - time(NULL));
1159 end_critical_section(S_SCHEDULE_LIST);
1164 * A warapper function for select so we can show a thread as blocked
1166 int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
1170 ctdl_thread_internal_change_state(CT, CTDL_THREAD_BLOCKED);
1171 if (!CtdlThreadCheckStop())
1172 ret = select(n, readfds, writefds, exceptfds, timeout);
1174 * If the select returned <= 0 then it failed due to an error
1175 * or timeout so this thread could stop if asked to do so.
1176 * Anything else means it needs to continue unless the system is shutting down
1181 * select says nothing to do so we can change to running if we haven't been asked to stop.
1183 ctdl_thread_internal_change_state(CT, CTDL_THREAD_RUNNING);
1188 * The select says this thread needs to do something useful.
1189 * This thread was in an idle state so it may have been asked to stop
1190 * but if the system isn't shutting down this thread is no longer
1191 * idle and select has given it a task to do so it must not stop
1192 * In this condition we need to force it into the running state.
1193 * CtdlThreadGC will clear its ticker for us.
1195 * FIXME: there is still a small hole here. It is possible for the sequence of locking
1196 * to allow the state to get changed to STOP_REQ just after this code if the other thread
1197 * has decided to change the state before this lock, it there fore has to wait till the lock
1198 * completes but it will continue to change the state. We need something a bit better here.
1200 citthread_mutex_lock(&CT->ThreadMutex); /* To prevent race condition of a sleeping thread */
1201 if (GC_thread->state > CTDL_THREAD_STOP_REQ && CT->state <= CTDL_THREAD_STOP_REQ)
1203 CtdlLogPrintf(CTDL_DEBUG, "Thread %s (0x%08lx) refused stop request.\n", CT->name, CT->tid);
1204 CT->state = CTDL_THREAD_RUNNING;
1206 citthread_mutex_unlock(&CT->ThreadMutex);
1214 void *new_worker_thread(void *arg);
1215 extern void close_masters (void);
1219 void go_threading(void)
1222 CtdlThreadNode *last_worker;
1225 * Initialise the thread system
1227 ctdl_thread_internal_init();
1229 /* Second call to module init functions now that threading is up */
1230 initialise_modules(1);
1233 * This thread is now used for garbage collection of other threads in the thread list
1235 CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", citthread_self());
1238 * We do a lot of locking and unlocking of the thread list in here.
1239 * We do this so that we can repeatedly release time for other threads
1240 * that may be waiting on the thread list.
1241 * We are a low priority thread so we can afford to do this
1244 while (CtdlThreadGetCount())
1247 exit_signal = CT->signal;
1250 CtdlThreadStopAll();
1253 check_sched_shutdown();
1254 if (CT->state > CTDL_THREAD_STOP_REQ)
1256 begin_critical_section(S_THREAD_LIST);
1257 ctdl_thread_internal_calc_loadavg();
1258 end_critical_section(S_THREAD_LIST);
1260 ctdl_thread_internal_check_scheduled(); /* start scheduled threads */
1263 /* Reduce the size of the worker thread pool if necessary. */
1264 if ((CtdlThreadGetWorkers() > config.c_min_workers + 1) && (CtdlThreadWorkerAvg < 20) && (CT->state > CTDL_THREAD_STOP_REQ))
1266 /* Ask a worker thread to stop as we no longer need it */
1267 begin_critical_section(S_THREAD_LIST);
1268 last_worker = CtdlThreadList;
1271 citthread_mutex_lock(&last_worker->ThreadMutex);
1272 if (last_worker->flags & CTDLTHREAD_WORKER && (last_worker->state > CTDL_THREAD_STOPPING) && (last_worker->Context == NULL))
1274 citthread_mutex_unlock(&last_worker->ThreadMutex);
1277 citthread_mutex_unlock(&last_worker->ThreadMutex);
1278 last_worker = last_worker->next;
1280 end_critical_section(S_THREAD_LIST);
1283 #ifdef WITH_THREADLOG
1284 CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (0x%08lx).\n",
1289 CtdlThreadStop(last_worker);
1294 * If all our workers are working hard, start some more to help out
1297 /* FIXME: come up with a better way to dynamically alter the number of threads
1298 * based on the system load
1301 if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() <= num_sessions) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
1303 if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
1304 #endif /* NEW_WORKER */
1306 for (i=0; i<5 ; i++)
1309 CtdlThreadCreate("Worker Thread (new)",
1310 CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1315 CtdlThreadCreate("Worker Thread",
1316 CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1320 #endif /* NEW_WORKER */
1326 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
1331 #ifdef THREADS_USESIGNALS
1332 if (CtdlThreadGetCount() && CT->state > CTDL_THREAD_STOP_REQ)
1334 if (CtdlThreadGetCount())
1339 * If the above loop exits we must be shutting down since we obviously have no threads
1341 ctdl_thread_internal_cleanup();
1348 * Starting a new implimentation of a worker thread.
1349 * This new implimentation will be faster and do more work per thread.
1353 * Select on master socket.
1354 * First worker thread in here acquires the lock and builds an FDSET of master sockets.
1355 * then it goes into a loop selecting on the master sockets timing out every few milliseconds.
1356 * If it times out it rebiulds its list and loops.
1357 * If the select succeeds it creates a new context and returns.
1358 * During this time the other workers are selecting on existing contexts or sleeping.
1360 void select_on_master(void)
1363 struct ServiceFunctionHook *serviceptr;
1364 int ssock; /* Descriptor for client socket */
1369 struct CitContext *con;
1370 const char *old_name;
1374 old_name = CtdlThreadName("select_on_master");
1376 /* Initialize the fdset. */
1380 /* First, add the various master sockets to the fdset. */
1381 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1382 m = serviceptr->msock;
1383 FD_SET(m, &readfds);
1389 tv.tv_sec = 1; /* wake up every 1 sec if no input */
1391 retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1393 /* Select got an error or we are shutting down so get out */
1394 if (retval == 0 || CtdlThreadCheckStop()) {
1395 CtdlThreadName(old_name);
1399 /* Select says something happened on one of our master sockets so now we handle it */
1400 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1401 if (FD_ISSET(serviceptr->msock, &readfds)) {
1402 ssock = accept(serviceptr->msock, NULL, 0);
1404 CtdlLogPrintf(CTDL_DEBUG, "New client socket %d\n", ssock);
1405 /* The master socket is non-blocking but the client
1406 * sockets need to be blocking, otherwise certain
1407 * operations barf on FreeBSD. Not a fatal error.
1409 if (fcntl(ssock, F_SETFL, 0) < 0) {
1410 CtdlLogPrintf(CTDL_EMERG,
1411 "citserver: Can't set socket to blocking: %s\n",
1415 /* New context will be created already
1416 * set up in the CON_EXECUTING state.
1418 con = CreateNewContext();
1421 /* Assign our new socket number to it. */
1422 con->client_socket = ssock;
1423 con->h_command_function = serviceptr->h_command_function;
1424 con->h_async_function = serviceptr->h_async_function;
1425 con->ServiceName = serviceptr->ServiceName;
1426 /* Determine whether it's a local socket */
1427 if (serviceptr->sockpath != NULL)
1428 con->is_local_socket = 1;
1430 /* Set the SO_REUSEADDR socket option */
1432 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
1434 become_session(con);
1436 serviceptr->h_greeting_function();
1437 become_session(NULL);
1438 con->state = CON_IDLE;
1444 CtdlThreadName(old_name);
1448 * Select on client socket.
1449 * First worker thread in here acquires the lock and builds an FDSET of client sockets.
1450 * then it selects on the client sockets timing out after 1 second.
1451 * If it times out the thread goes off to check on housekeeping etc.
1452 * If the select succeeds the thread goes off to handle the client request.
1453 * If the list of client connections is empty the threads all sleep for one second
1455 struct CitContext *select_on_client(void)
1461 const char *old_name;
1464 old_name = CtdlThreadName("select_on_client");
1466 /* Initialise the fdset */
1468 FD_SET(CT->Context->client_socket, &readfds);
1469 highest = CT->Context->client_socket;
1470 /* Now we can select on any connections that are waiting */
1472 if (!CtdlThreadCheckStop())
1474 tv.tv_sec = config.c_sleeping; /* wake up every second if no input */
1476 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
1478 else /* Shutting down? */
1480 CtdlThreadName(old_name);
1485 /* Now figure out who made this select() unblock.
1486 * First, check for an error or exit condition.
1489 if (errno == EBADF) {
1490 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1493 if (errno != EINTR) {
1494 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1495 CtdlThreadStopAll();
1496 } else if (!CtdlThreadCheckStop()) {
1497 CtdlLogPrintf(CTDL_DEBUG, "Un handled select failure.\n");
1499 CtdlThreadName(old_name);
1502 else if(retval == 0)
1504 CtdlThreadName(old_name);
1505 CT->Context->kill_me = 1;
1510 CT->Context->state = CON_EXECUTING;
1511 CT->Context->input_waiting = 1;
1513 CtdlThreadName(old_name);
1514 return (CT->Context);
1520 * Do the worker threads work when needed
1522 int execute_session(struct CitContext *bind_me)
1526 become_session(bind_me);
1528 /* If the client has sent a command, execute it. */
1529 if (CC->input_waiting) {
1530 CC->h_command_function();
1531 CC->input_waiting = 0;
1534 /* If there are asynchronous messages waiting and the
1535 * client supports it, do those now */
1536 if ((CC->is_async) && (CC->async_waiting)
1537 && (CC->h_async_function != NULL)) {
1538 CC->h_async_function();
1539 CC->async_waiting = 0;
1542 force_purge = CC->kill_me;
1545 become_session(NULL);
1546 bind_me->state = CON_IDLE;
1552 extern void dead_session_purge(int force);
1555 * A new worker_thread loop.
1558 void *new_worker_thread(void *arg)
1560 struct CitContext *bind_me;
1563 while (!CtdlThreadCheckStop()) {
1565 /* make doubly sure we're not holding any stale db handles
1566 * which might cause a deadlock.
1568 cdb_check_handles();
1570 bind_me = NULL; /* Which session shall we handle? */
1572 if (CT->Context == NULL)
1574 if (CtdlThreadCheckStop())
1578 bind_me = select_on_client();
1579 if (CtdlThreadCheckStop())
1583 force_purge = execute_session(bind_me);
1585 dead_session_purge(force_purge);
1586 if (CtdlThreadCheckStop())