2 * $Id: sysdep.c 5882 2007-12-13 19:46:05Z davew $
4 * Citadel "system dependent" stuff.
5 * See copyright.txt for copyright information.
7 * Here's where we have the Citadel thread implimentation
11 #include <sys/types.h>
13 #include <sys/socket.h>
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
22 # include <sys/time.h>
29 #include "ctdl_module.h"
30 #include "modules_init.h"
31 #include "housekeeping.h"
33 #include "citserver.h"
34 #include "sysdep_decls.h"
37 * define this to use the new worker_thread method of handling connections
42 * New thread interface.
43 * To create a thread you must call one of the create thread functions.
44 * You must pass it the address of (a pointer to a CtdlThreadNode initialised to NULL) like this
45 * struct CtdlThreadNode *node = NULL;
47 * If the thread is created *node will point to the thread control structure for the created thread.
48 * If the thread creation fails *node remains NULL
49 * Do not free the memory pointed to by *node, it doesn't belong to you.
50 * This new interface duplicates much of the eCrash stuff. We should go for closer integration since that would
51 * remove the need for the calls to eCrashRegisterThread and friends
54 static int num_threads = 0; /* Current number of threads */
55 static int num_workers = 0; /* Current number of worker threads */
57 CtdlThreadNode *CtdlThreadList = NULL;
58 CtdlThreadNode *CtdlThreadSchedList = NULL;
60 static citthread_t GC_thread;
61 static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE];
62 double CtdlThreadLoadAvg = 0;
63 double CtdlThreadWorkerAvg = 0;
64 citthread_key_t ThreadKey;
66 citthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
70 void InitialiseSemaphores(void)
74 /* Set up a bunch of semaphores to be used for critical sections */
75 for (i=0; i<MAX_SEMAPHORES; ++i) {
76 citthread_mutex_init(&Critters[i], NULL);
84 * Obtain a semaphore lock to begin a critical section.
85 * but only if no one else has one
87 int try_critical_section(int which_one)
89 /* For all types of critical sections except those listed here,
90 * ensure nobody ever tries to do a critical section within a
91 * transaction; this could lead to deadlock.
93 if ( (which_one != S_FLOORCACHE)
94 #ifdef DEBUG_MEMORY_LEAKS
95 && (which_one != S_DEBUGMEMLEAKS)
97 && (which_one != S_RPLIST)
101 return (citthread_mutex_trylock(&Critters[which_one]));
106 * Obtain a semaphore lock to begin a critical section.
108 void begin_critical_section(int which_one)
110 /* CtdlLogPrintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
112 /* For all types of critical sections except those listed here,
113 * ensure nobody ever tries to do a critical section within a
114 * transaction; this could lead to deadlock.
116 if ( (which_one != S_FLOORCACHE)
117 #ifdef DEBUG_MEMORY_LEAKS
118 && (which_one != S_DEBUGMEMLEAKS)
120 && (which_one != S_RPLIST)
124 citthread_mutex_lock(&Critters[which_one]);
128 * Release a semaphore lock to end a critical section.
130 void end_critical_section(int which_one)
132 citthread_mutex_unlock(&Critters[which_one]);
137 * A function to destroy the TSD
139 static void ctdl_thread_internal_dest_tsd(void *arg)
149 * A function to initialise the thread TSD
151 void ctdl_thread_internal_init_tsd(void)
155 if ((ret = citthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
156 lprintf(CTDL_EMERG, "citthread_key_create: %s\n", strerror(ret));
162 * Ensure that we have a key for thread-specific data.
164 * This should be called immediately after startup by any thread
167 void CtdlThreadAllocTSD(void)
171 if (citthread_getspecific(ThreadKey) != NULL)
174 tsd = malloc(sizeof(ThreadTSD));
178 memset(tsd->cursors, 0, sizeof tsd->cursors);
181 citthread_setspecific(ThreadKey, tsd);
185 void ctdl_thread_internal_free_tsd(void)
187 ctdl_thread_internal_dest_tsd(citthread_getspecific(ThreadKey));
188 citthread_setspecific(ThreadKey, NULL);
192 void ctdl_thread_internal_cleanup(void)
195 CtdlThreadNode *this_thread, *that_thread;
197 for (i=0; i<CTDL_THREAD_LAST_STATE; i++)
199 free (CtdlThreadStates[i]);
202 /* Clean up the scheduled thread list */
203 this_thread = CtdlThreadSchedList;
206 that_thread = this_thread;
207 this_thread = this_thread->next;
208 citthread_mutex_destroy(&that_thread->ThreadMutex);
209 citthread_cond_destroy(&that_thread->ThreadCond);
210 citthread_mutex_destroy(&that_thread->SleepMutex);
211 citthread_cond_destroy(&that_thread->SleepCond);
212 citthread_attr_destroy(&that_thread->attr);
215 ctdl_thread_internal_free_tsd();
218 void ctdl_thread_internal_init(void)
220 CtdlThreadNode *this_thread;
223 GC_thread = citthread_self();
224 CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread");
225 CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread");
226 CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created");
227 CtdlThreadStates[CTDL_THREAD_CANCELLED] = strdup("Thread Cancelled");
228 CtdlThreadStates[CTDL_THREAD_EXITED] = strdup("Thread Exited");
229 CtdlThreadStates[CTDL_THREAD_STOPPING] = strdup("Thread Stopping");
230 CtdlThreadStates[CTDL_THREAD_STOP_REQ] = strdup("Thread Stop Requested");
231 CtdlThreadStates[CTDL_THREAD_SLEEPING] = strdup("Thread Sleeping");
232 CtdlThreadStates[CTDL_THREAD_RUNNING] = strdup("Thread Running");
233 CtdlThreadStates[CTDL_THREAD_BLOCKED] = strdup("Thread Blocked");
235 /* Get ourself a thread entry */
236 this_thread = malloc(sizeof(CtdlThreadNode));
237 if (this_thread == NULL) {
238 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
241 // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
242 memset (this_thread, 0, sizeof(CtdlThreadNode));
244 citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
245 citthread_cond_init (&(this_thread->ThreadCond), NULL);
246 citthread_mutex_init (&(this_thread->SleepMutex), NULL);
247 citthread_cond_init (&(this_thread->SleepCond), NULL);
249 /* We are garbage collector so create us as running */
250 this_thread->state = CTDL_THREAD_RUNNING;
252 if ((ret = citthread_attr_init(&this_thread->attr))) {
253 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
258 this_thread->name = "Garbage Collection Thread";
260 this_thread->tid = GC_thread;
263 num_threads++; // Increase the count of threads in the system.
265 this_thread->next = CtdlThreadList;
266 CtdlThreadList = this_thread;
267 if (this_thread->next)
268 this_thread->next->prev = this_thread;
269 /* Set up start times */
270 gettimeofday(&this_thread->start_time, NULL); /* Time this thread started */
271 memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval)); /* Changed state so mark it. */
276 * A function to update a threads load averages
278 void ctdl_thread_internal_update_avgs(CtdlThreadNode *this_thread)
280 struct timeval now, result;
281 double last_duration;
283 gettimeofday(&now, NULL);
284 timersub(&now, &(this_thread->last_state_change), &result);
285 /* I don't think these mutex's are needed here */
286 citthread_mutex_lock(&this_thread->ThreadMutex);
287 // result now has a timeval for the time we spent in the last state since we last updated
288 last_duration = (double)result.tv_sec + ((double)result.tv_usec / (double) 1000000);
289 if (this_thread->state == CTDL_THREAD_SLEEPING)
290 this_thread->avg_sleeping += last_duration;
291 if (this_thread->state == CTDL_THREAD_RUNNING)
292 this_thread->avg_running += last_duration;
293 if (this_thread->state == CTDL_THREAD_BLOCKED)
294 this_thread->avg_blocked += last_duration;
295 memcpy (&this_thread->last_state_change, &now, sizeof (struct timeval));
296 citthread_mutex_unlock(&this_thread->ThreadMutex);
300 * A function to chenge the state of a thread
302 void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlThreadState new_state)
305 * Wether we change state or not we need update the load values
307 ctdl_thread_internal_update_avgs(this_thread);
308 /* This mutex not needed here? */
309 citthread_mutex_lock(&this_thread->ThreadMutex); /* To prevent race condition of a sleeping thread */
310 if ((new_state == CTDL_THREAD_STOP_REQ) && (this_thread->state > CTDL_THREAD_STOP_REQ))
311 this_thread->state = new_state;
312 if (((new_state == CTDL_THREAD_SLEEPING) || (new_state == CTDL_THREAD_BLOCKED)) && (this_thread->state == CTDL_THREAD_RUNNING))
313 this_thread->state = new_state;
314 if ((new_state == CTDL_THREAD_RUNNING) && ((this_thread->state == CTDL_THREAD_SLEEPING) || (this_thread->state == CTDL_THREAD_BLOCKED)))
315 this_thread->state = new_state;
316 citthread_mutex_unlock(&this_thread->ThreadMutex);
321 * A function to tell all threads to exit
323 void CtdlThreadStopAll(void)
325 //FIXME: The signalling of the condition should not be in the critical_section
326 // We need to build a list of threads we are going to signal and then signal them afterwards
328 CtdlThreadNode *this_thread;
330 begin_critical_section(S_THREAD_LIST);
331 this_thread = CtdlThreadList;
334 #ifdef THREADS_USESIGNALS
335 citthread_killl(this_thread->tid, SIGHUP);
337 ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
338 citthread_cond_signal(&this_thread->ThreadCond);
339 citthread_cond_signal(&this_thread->SleepCond);
340 CtdlLogPrintf(CTDL_DEBUG, "Thread system stopping thread \"%s\" (0x%08lx).\n",
341 this_thread->name, this_thread->tid);
342 this_thread = this_thread->next;
344 end_critical_section(S_THREAD_LIST);
349 * A function to wake up all sleeping threads
351 void CtdlThreadWakeAll(void)
353 CtdlThreadNode *this_thread;
355 CtdlLogPrintf(CTDL_DEBUG, "Thread system waking all threads.\n");
357 begin_critical_section(S_THREAD_LIST);
358 this_thread = CtdlThreadList;
361 if (!this_thread->thread_func)
363 citthread_cond_signal(&this_thread->ThreadCond);
364 citthread_cond_signal(&this_thread->SleepCond);
366 this_thread = this_thread->next;
368 end_critical_section(S_THREAD_LIST);
373 * A function to return the number of threads running in the system
375 int CtdlThreadGetCount(void)
380 int CtdlThreadGetWorkers(void)
385 double CtdlThreadGetWorkerAvg(void)
389 begin_critical_section(S_THREAD_LIST);
390 ret = CtdlThreadWorkerAvg;
391 end_critical_section(S_THREAD_LIST);
395 double CtdlThreadGetLoadAvg(void)
399 begin_critical_section(S_THREAD_LIST);
400 ret = CtdlThreadLoadAvg;
401 end_critical_section(S_THREAD_LIST);
409 * A function to rename a thread
410 * Returns a const char *
412 const char *CtdlThreadName(const char *name)
414 const char *old_name;
418 CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
429 * A function to force a thread to exit
431 void CtdlThreadCancel(CtdlThreadNode *thread)
433 CtdlThreadNode *this_thread;
438 this_thread = thread;
441 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
446 if (!this_thread->thread_func)
448 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() the garbage collector.\n");
453 ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED);
454 citthread_cancel(this_thread->tid);
459 * A function for a thread to check if it has been asked to stop
461 int CtdlThreadCheckStop(void)
467 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
474 #ifdef THREADS_USERSIGNALS
476 CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
478 if(state == CTDL_THREAD_STOP_REQ)
480 CT->state = CTDL_THREAD_STOPPING;
483 else if((state < CTDL_THREAD_STOP_REQ) && (state > CTDL_THREAD_CREATE))
492 * A function to ask a thread to exit
493 * The thread must call CtdlThreadCheckStop() periodically to determine if it should exit
495 void CtdlThreadStop(CtdlThreadNode *thread)
497 CtdlThreadNode *this_thread;
502 this_thread = thread;
505 if (!(this_thread->thread_func))
506 return; // Don't stop garbage collector
507 #ifdef THREADS_USESIGNALS
508 citthread_kill(this_thread->tid, SIGHUP);
510 ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
511 citthread_cond_signal(&this_thread->ThreadCond);
512 citthread_cond_signal(&this_thread->SleepCond);
516 * So we now have a sleep command that works with threads but it is in seconds
518 void CtdlThreadSleep(int secs)
520 struct timespec wake_time;
521 struct timeval time_now;
526 CtdlLogPrintf(CTDL_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
530 memset (&wake_time, 0, sizeof(struct timespec));
531 gettimeofday(&time_now, NULL);
532 wake_time.tv_sec = time_now.tv_sec + secs;
533 wake_time.tv_nsec = time_now.tv_usec * 10;
535 ctdl_thread_internal_change_state (CT, CTDL_THREAD_SLEEPING);
537 citthread_mutex_lock(&CT->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
538 citthread_cond_timedwait(&CT->SleepCond, &CT->ThreadMutex, &wake_time);
539 citthread_mutex_unlock(&CT->ThreadMutex);
541 ctdl_thread_internal_change_state (CT, CTDL_THREAD_RUNNING);
546 * Routine to clean up our thread function on exit
548 static void ctdl_internal_thread_cleanup(void *arg)
551 * In here we were called by the current thread because it is exiting
552 * NB. WE ARE THE CURRENT THREAD
554 CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", CT->name, CT->tid);
556 #ifdef HAVE_BACKTRACE
557 eCrash_UnregisterThread();
560 citthread_mutex_lock(&CT->ThreadMutex);
561 CT->state = CTDL_THREAD_EXITED; // needs to be last thing else house keeping will unlink us too early
562 citthread_mutex_unlock(&CT->ThreadMutex);
566 * A quick function to show the load averages
568 void ctdl_thread_internal_calc_loadavg(void)
570 CtdlThreadNode *that_thread;
571 double load_avg, worker_avg;
574 that_thread = CtdlThreadList;
579 /* Update load averages */
580 ctdl_thread_internal_update_avgs(that_thread);
581 citthread_mutex_lock(&that_thread->ThreadMutex);
582 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;
583 that_thread->avg_sleeping /= 2;
584 that_thread->avg_running /= 2;
585 that_thread->avg_blocked /= 2;
586 load_avg += that_thread->load_avg;
587 if (that_thread->flags & CTDLTHREAD_WORKER)
589 worker_avg += that_thread->load_avg;
592 #ifdef WITH_THREADLOG
593 CtdlLogPrintf(CTDL_DEBUG, "CtdlThread, \"%s\" (%lu) \"%s\" %.2f %.2f %.2f %.2f\n",
596 CtdlThreadStates[that_thread->state],
597 that_thread->avg_sleeping,
598 that_thread->avg_running,
599 that_thread->avg_blocked,
600 that_thread->load_avg);
602 citthread_mutex_unlock(&that_thread->ThreadMutex);
603 that_thread = that_thread->next;
605 CtdlThreadLoadAvg = load_avg/num_threads;
606 CtdlThreadWorkerAvg = worker_avg/workers;
607 #ifdef WITH_THREADLOG
608 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);
614 * Garbage collection routine.
615 * Gets called by main() in a loop to clean up the thread list periodically.
617 void CtdlThreadGC (void)
619 CtdlThreadNode *this_thread, *that_thread;
620 int workers = 0, sys_workers;
623 begin_critical_section(S_THREAD_LIST);
625 /* Handle exiting of garbage collector thread */
627 CtdlThreadList->state = CTDL_THREAD_EXITED;
629 #ifdef WITH_THREADLOG
630 CtdlLogPrintf(CTDL_DEBUG, "Thread system running garbage collection.\n");
633 * Woke up to do garbage collection
635 this_thread = CtdlThreadList;
638 that_thread = this_thread;
639 this_thread = this_thread->next;
641 if ((that_thread->state == CTDL_THREAD_STOP_REQ || that_thread->state == CTDL_THREAD_STOPPING)
642 && (!citthread_equal(that_thread->tid, citthread_self())))
643 that_thread->stop_ticker++;
645 if (that_thread->stop_ticker == 5)
647 CtdlLogPrintf(CTDL_DEBUG, "Thread System: The thread \"%s\" (0x%08lx) failed to self terminate withing 5 ticks. Canceling it.\n", that_thread->name, that_thread->tid);
648 if ((that_thread->flags & CTDLTHREAD_WORKER) == 0)
649 CtdlLogPrintf(CTDL_INFO, "Thread System: A non worker thread was canceled this may cause message loss.\n");
650 that_thread->state = CTDL_THREAD_CANCELLED;
651 that_thread->stop_ticker++;
652 citthread_cancel(that_thread->tid);
656 /* Do we need to clean up this thread? */
657 if ((that_thread->state != CTDL_THREAD_EXITED) && (that_thread->state != CTDL_THREAD_CANCELLED))
659 if(that_thread->flags & CTDLTHREAD_WORKER)
660 workers++; /* Sanity check on number of worker threads */
664 if (citthread_equal(that_thread->tid, citthread_self()) && that_thread->thread_func)
666 end_critical_section(S_THREAD_LIST);
667 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, a thread is trying to clean up after itself.\n");
672 if (num_threads <= 0)
674 end_critical_section(S_THREAD_LIST);
675 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, num_threads <= 0 and trying to do Garbage Collection.\n");
680 if(that_thread->flags & CTDLTHREAD_WORKER)
681 num_workers--; /* This is a wroker thread so reduce the count. */
683 /* If we are unlinking the list head then the next becomes the list head */
684 if(that_thread->prev)
685 that_thread->prev->next = that_thread->next;
687 CtdlThreadList = that_thread->next;
688 if(that_thread->next)
689 that_thread->next->prev = that_thread->prev;
691 citthread_cond_signal(&that_thread->ThreadCond);
692 citthread_cond_signal(&that_thread->SleepCond); // Make sure this thread is awake
693 citthread_mutex_lock(&that_thread->ThreadMutex); // Make sure it has done what its doing
694 citthread_mutex_unlock(&that_thread->ThreadMutex);
696 * Join on the thread to do clean up and prevent memory leaks
697 * Also makes sure the thread has cleaned up after itself before we remove it from the list
698 * We can join on the garbage collector thread the join should just return EDEADLCK
700 ret = citthread_join (that_thread->tid, NULL);
702 CtdlLogPrintf(CTDL_DEBUG, "Garbage collection on own thread.\n");
703 else if (ret == EINVAL)
704 CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, that thread already joined on.\n");
705 else if (ret == ESRCH)
706 CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, no thread to join on.\n");
708 CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, citthread_join returned an unknown error(%d).\n", ret);
710 * Now we own that thread entry
712 CtdlLogPrintf(CTDL_INFO, "Garbage Collection for thread \"%s\" (0x%08lx).\n",
713 that_thread->name, that_thread->tid);
714 citthread_mutex_destroy(&that_thread->ThreadMutex);
715 citthread_cond_destroy(&that_thread->ThreadCond);
716 citthread_mutex_destroy(&that_thread->SleepMutex);
717 citthread_cond_destroy(&that_thread->SleepCond);
718 citthread_attr_destroy(&that_thread->attr);
721 sys_workers = num_workers;
722 end_critical_section(S_THREAD_LIST);
724 /* Sanity check number of worker threads */
725 if (workers != sys_workers)
727 CtdlLogPrintf(CTDL_EMERG,
728 "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
739 * Runtime function for a Citadel Thread.
740 * This initialises the threads environment and then calls the user supplied thread function
741 * Note that this is the REAL thread function and wraps the users thread function.
743 static void *ctdl_internal_thread_func (void *arg)
745 CtdlThreadNode *this_thread;
748 /* lock and unlock the thread list.
749 * This causes this thread to wait until all its creation stuff has finished before it
750 * can continue its execution.
752 begin_critical_section(S_THREAD_LIST);
753 this_thread = (CtdlThreadNode *) arg;
754 gettimeofday(&this_thread->start_time, NULL); /* Time this thread started */
755 // citthread_mutex_lock(&this_thread->ThreadMutex);
757 // Register the cleanup function to take care of when we exit.
758 citthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
759 // Get our thread data structure
760 CtdlThreadAllocTSD();
762 this_thread->pid = getpid();
763 memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval)); /* Changed state so mark it. */
764 /* Only change to running state if we weren't asked to stop during the create cycle
765 * Other wise there is a window to allow this threads creation to continue to full grown and
766 * therby prevent a shutdown of the server.
768 // citthread_mutex_unlock(&this_thread->ThreadMutex);
770 if (!CtdlThreadCheckStop())
772 citthread_mutex_lock(&this_thread->ThreadMutex);
773 this_thread->state = CTDL_THREAD_RUNNING;
774 citthread_mutex_unlock(&this_thread->ThreadMutex);
776 end_critical_section(S_THREAD_LIST);
778 // Register for tracing
779 #ifdef HAVE_BACKTRACE
780 eCrash_RegisterThread(this_thread->name, 0);
783 // Tell the world we are here
784 CtdlLogPrintf(CTDL_NOTICE, "Created a new thread \"%s\" (0x%08lx).\n",
785 this_thread->name, this_thread->tid);
788 * run the thread to do the work but only if we haven't been asked to stop
790 if (!CtdlThreadCheckStop())
791 ret = (this_thread->thread_func)(this_thread->user_args);
794 * Our thread is exiting either because it wanted to end or because the server is stopping
795 * We need to clean up
797 citthread_cleanup_pop(1); // Execute our cleanup routine and remove it
806 * Function to initialise an empty thread structure
808 CtdlThreadNode *ctdl_internal_init_thread_struct(CtdlThreadNode *this_thread, long flags)
812 // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
813 memset (this_thread, 0, sizeof(CtdlThreadNode));
815 /* Create the mutex's early so we can use them */
816 citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
817 citthread_cond_init (&(this_thread->ThreadCond), NULL);
818 citthread_mutex_init (&(this_thread->SleepMutex), NULL);
819 citthread_cond_init (&(this_thread->SleepCond), NULL);
821 this_thread->state = CTDL_THREAD_CREATE;
823 if ((ret = citthread_attr_init(&this_thread->attr))) {
824 citthread_mutex_unlock(&this_thread->ThreadMutex);
825 citthread_mutex_destroy(&(this_thread->ThreadMutex));
826 citthread_cond_destroy(&(this_thread->ThreadCond));
827 citthread_mutex_destroy(&(this_thread->SleepMutex));
828 citthread_cond_destroy(&(this_thread->SleepCond));
829 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
834 /* Our per-thread stacks need to be bigger than the default size,
835 * otherwise the MIME parser crashes on FreeBSD, and the IMAP service
836 * crashes on 64-bit Linux.
838 if (flags & CTDLTHREAD_BIGSTACK)
840 #ifdef WITH_THREADLOG
841 CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n");
843 if ((ret = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
844 citthread_mutex_unlock(&this_thread->ThreadMutex);
845 citthread_mutex_destroy(&(this_thread->ThreadMutex));
846 citthread_cond_destroy(&(this_thread->ThreadCond));
847 citthread_mutex_destroy(&(this_thread->SleepMutex));
848 citthread_cond_destroy(&(this_thread->SleepCond));
849 citthread_attr_destroy(&this_thread->attr);
850 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_setstacksize: %s\n",
857 /* Set this new thread with an avg_blocked of 2. We do this so that its creation affects the
858 * load average for the system. If we don't do this then we create a mass of threads at the same time
859 * because the creation didn't affect the load average.
861 this_thread->avg_blocked = 2;
863 return (this_thread);
870 * Internal function to create a thread.
872 CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args)
875 CtdlThreadNode *this_thread;
877 if (num_threads >= 32767)
879 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
883 this_thread = malloc(sizeof(CtdlThreadNode));
884 if (this_thread == NULL) {
885 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
889 /* Initialise the thread structure */
890 if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
893 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
897 * If we got here we are going to create the thread so we must initilise the structure
898 * first because most implimentations of threading can't create it in a stopped state
899 * and it might want to do things with its structure that aren't initialised otherwise.
903 this_thread->name = name;
907 this_thread->name = "Un-named Thread";
910 this_thread->flags = flags;
911 this_thread->thread_func = thread_func;
912 this_thread->user_args = args;
914 // citthread_mutex_lock(&this_thread->ThreadMutex);
916 begin_critical_section(S_THREAD_LIST);
918 * We pass this_thread into the thread as its args so that it can find out information
919 * about itself and it has a bit of storage space for itself, not to mention that the REAL
920 * thread function needs to finish off the setup of the structure
922 if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
924 end_critical_section(S_THREAD_LIST);
925 CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
927 citthread_mutex_unlock(&this_thread->ThreadMutex);
928 citthread_mutex_destroy(&(this_thread->ThreadMutex));
929 citthread_cond_destroy(&(this_thread->ThreadCond));
930 citthread_mutex_destroy(&(this_thread->SleepMutex));
931 citthread_cond_destroy(&(this_thread->SleepCond));
932 citthread_attr_destroy(&this_thread->attr);
937 num_threads++; // Increase the count of threads in the system.
938 if(this_thread->flags & CTDLTHREAD_WORKER)
941 this_thread->next = CtdlThreadList;
942 CtdlThreadList = this_thread;
943 if (this_thread->next)
944 this_thread->next->prev = this_thread;
945 ctdl_thread_internal_calc_loadavg();
947 // citthread_mutex_unlock(&this_thread->ThreadMutex);
948 end_critical_section(S_THREAD_LIST);
954 * Wrapper function to create a thread
955 * ensures the critical section and other protections are in place.
956 * char *name = name to give to thread, if NULL, use generic name
957 * int flags = flags to determine type of thread and standard facilities
959 CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args)
961 CtdlThreadNode *ret = NULL;
963 ret = ctdl_internal_create_thread(name, flags, thread_func, args);
970 * Internal function to schedule a thread.
971 * Must be called from within a S_THREAD_LIST critical section
973 CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) (void *arg), void *args, time_t when)
975 CtdlThreadNode *this_thread;
977 if (num_threads >= 32767)
979 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
983 this_thread = malloc(sizeof(CtdlThreadNode));
984 if (this_thread == NULL) {
985 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
988 /* Initialise the thread structure */
989 if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
992 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
997 * If we got here we are going to create the thread so we must initilise the structure
998 * first because most implimentations of threading can't create it in a stopped state
999 * and it might want to do things with its structure that aren't initialised otherwise.
1003 this_thread->name = name;
1007 this_thread->name = "Un-named Thread";
1010 this_thread->flags = flags;
1011 this_thread->thread_func = thread_func;
1012 this_thread->user_args = args;
1015 * When to start this thread
1017 this_thread->when = when;
1019 begin_critical_section(S_SCHEDULE_LIST);
1020 this_thread->next = CtdlThreadSchedList;
1021 CtdlThreadSchedList = this_thread;
1022 if (this_thread->next)
1023 this_thread->next->prev = this_thread;
1024 end_critical_section(S_SCHEDULE_LIST);
1031 CtdlThreadNode *ctdl_thread_internal_start_scheduled (CtdlThreadNode *this_thread)
1035 // citthread_mutex_lock(&that_thread->ThreadMutex);
1036 begin_critical_section(S_THREAD_LIST);
1038 * We pass this_thread into the thread as its args so that it can find out information
1039 * about itself and it has a bit of storage space for itself, not to mention that the REAL
1040 * thread function needs to finish off the setup of the structure
1042 if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
1044 end_critical_section(S_THREAD_LIST);
1045 CtdlLogPrintf(CTDL_DEBUG, "Failed to start scheduled thread \"%s\": %s\n", this_thread->name, strerror(ret));
1046 // citthread_mutex_unlock(&this_thread->ThreadMutex);
1047 citthread_mutex_destroy(&(this_thread->ThreadMutex));
1048 citthread_cond_destroy(&(this_thread->ThreadCond));
1049 citthread_mutex_destroy(&(this_thread->SleepMutex));
1050 citthread_cond_destroy(&(this_thread->SleepCond));
1051 citthread_attr_destroy(&this_thread->attr);
1057 num_threads++; // Increase the count of threads in the system.
1058 if(this_thread->flags & CTDLTHREAD_WORKER)
1061 this_thread->next = CtdlThreadList;
1062 CtdlThreadList = this_thread;
1063 if (this_thread->next)
1064 this_thread->next->prev = this_thread;
1065 // citthread_mutex_unlock(&that_thread->ThreadMutex);
1067 ctdl_thread_internal_calc_loadavg();
1068 end_critical_section(S_THREAD_LIST);
1076 void ctdl_thread_internal_check_scheduled(void)
1078 CtdlThreadNode *this_thread, *that_thread;
1081 if (try_critical_section(S_SCHEDULE_LIST))
1082 return; /* If this list is locked we wait till the next chance */
1086 #ifdef WITH_THREADLOG
1087 CtdlLogPrintf(CTDL_DEBUG, "Checking for scheduled threads to start.\n");
1090 this_thread = CtdlThreadSchedList;
1093 that_thread = this_thread;
1094 this_thread = this_thread->next;
1096 if (now > that_thread->when)
1098 /* Unlink from schedule list */
1099 if (that_thread->prev)
1100 that_thread->prev->next = that_thread->next;
1102 CtdlThreadSchedList = that_thread->next;
1103 if (that_thread->next)
1104 that_thread->next->prev = that_thread->prev;
1106 that_thread->next = that_thread->prev = NULL;
1107 #ifdef WITH_THREADLOG
1108 CtdlLogPrintf(CTDL_DEBUG, "About to start scheduled thread \"%s\".\n", that_thread->name);
1110 if (CT->state > CTDL_THREAD_STOP_REQ)
1111 { /* Only start it if the system is not stopping */
1112 if (ctdl_thread_internal_start_scheduled (that_thread))
1114 #ifdef WITH_THREADLOG
1115 CtdlLogPrintf(CTDL_INFO, "Thread system, Started a scheduled thread \"%s\" (%ud).\n",
1116 that_thread->name, that_thread->tid);
1121 #ifdef WITH_THREADLOG
1124 CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" will start in %ld seconds.\n",
1125 that_thread->name, that_thread->when - time(NULL));
1129 end_critical_section(S_SCHEDULE_LIST);
1134 * A warapper function for select so we can show a thread as blocked
1136 int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
1140 ctdl_thread_internal_change_state(CT, CTDL_THREAD_BLOCKED);
1141 ret = select(n, readfds, writefds, exceptfds, timeout);
1142 ctdl_thread_internal_change_state(CT, CTDL_THREAD_RUNNING);
1148 void *new_worker_thread(void *arg);
1149 extern void close_masters (void);
1153 void go_threading(void)
1156 CtdlThreadNode *last_worker;
1159 * Initialise the thread system
1161 ctdl_thread_internal_init();
1163 /* Second call to module init functions now that threading is up */
1164 initialise_modules(1);
1167 * This thread is now used for garbage collection of other threads in the thread list
1169 CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", citthread_self());
1172 * We do a lot of locking and unlocking of the thread list in here.
1173 * We do this so that we can repeatedly release time for other threads
1174 * that may be waiting on the thread list.
1175 * We are a low priority thread so we can afford to do this
1178 while (CtdlThreadGetCount())
1181 exit_signal = CT->signal;
1184 CtdlThreadStopAll();
1187 check_sched_shutdown();
1188 if (CT->state > CTDL_THREAD_STOP_REQ)
1190 begin_critical_section(S_THREAD_LIST);
1191 ctdl_thread_internal_calc_loadavg();
1192 end_critical_section(S_THREAD_LIST);
1194 ctdl_thread_internal_check_scheduled(); /* start scheduled threads */
1197 /* Reduce the size of the worker thread pool if necessary. */
1198 if ((CtdlThreadGetWorkers() > config.c_min_workers + 1) && (CtdlThreadWorkerAvg < 20) && (CT->state > CTDL_THREAD_STOP_REQ))
1200 /* Ask a worker thread to stop as we no longer need it */
1201 begin_critical_section(S_THREAD_LIST);
1202 last_worker = CtdlThreadList;
1205 citthread_mutex_lock(&last_worker->ThreadMutex);
1206 if (last_worker->flags & CTDLTHREAD_WORKER && (last_worker->state > CTDL_THREAD_STOPPING) && (last_worker->Context == NULL))
1208 citthread_mutex_unlock(&last_worker->ThreadMutex);
1211 citthread_mutex_unlock(&last_worker->ThreadMutex);
1212 last_worker = last_worker->next;
1214 end_critical_section(S_THREAD_LIST);
1217 #ifdef WITH_THREADLOG
1218 CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (0x%08lx).\n",
1223 CtdlThreadStop(last_worker);
1228 * If all our workers are working hard, start some more to help out
1231 /* FIXME: come up with a better way to dynamically alter the number of threads
1232 * based on the system load
1235 if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() <= num_sessions) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
1237 if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
1238 #endif /* NEW_WORKER */
1240 for (i=0; i<5 ; i++)
1243 CtdlThreadCreate("Worker Thread (new)",
1244 CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1249 CtdlThreadCreate("Worker Thread",
1250 CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1254 #endif /* NEW_WORKER */
1260 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
1265 if (CtdlThreadGetCount())
1269 * If the above loop exits we must be shutting down since we obviously have no threads
1271 ctdl_thread_internal_cleanup();
1278 * Starting a new implimentation of a worker thread.
1279 * This new implimentation will be faster and do more work per thread.
1283 * Select on master socket.
1284 * First worker thread in here acquires the lock and builds an FDSET of master sockets.
1285 * then it goes into a loop selecting on the master sockets timing out every few milliseconds.
1286 * If it times out it rebiulds its list and loops.
1287 * If the select succeeds it creates a new context and returns.
1288 * During this time the other workers are selecting on existing contexts or sleeping.
1290 void select_on_master(void)
1293 struct ServiceFunctionHook *serviceptr;
1294 int ssock; /* Descriptor for client socket */
1299 struct CitContext *con;
1300 const char *old_name;
1304 old_name = CtdlThreadName("select_on_master");
1306 /* Initialize the fdset. */
1310 /* First, add the various master sockets to the fdset. */
1311 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1312 m = serviceptr->msock;
1313 FD_SET(m, &readfds);
1319 tv.tv_sec = 1; /* wake up every 1 sec if no input */
1321 retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1323 /* Select got an error or we are shutting down so get out */
1324 if (retval == 0 || CtdlThreadCheckStop()) {
1325 CtdlThreadName(old_name);
1329 /* Select says something happened on one of our master sockets so now we handle it */
1330 for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1331 if (FD_ISSET(serviceptr->msock, &readfds)) {
1332 ssock = accept(serviceptr->msock, NULL, 0);
1334 CtdlLogPrintf(CTDL_DEBUG, "New client socket %d\n", ssock);
1335 /* The master socket is non-blocking but the client
1336 * sockets need to be blocking, otherwise certain
1337 * operations barf on FreeBSD. Not a fatal error.
1339 if (fcntl(ssock, F_SETFL, 0) < 0) {
1340 CtdlLogPrintf(CTDL_EMERG,
1341 "citserver: Can't set socket to blocking: %s\n",
1345 /* New context will be created already
1346 * set up in the CON_EXECUTING state.
1348 con = CreateNewContext();
1351 /* Assign our new socket number to it. */
1352 con->client_socket = ssock;
1353 con->h_command_function = serviceptr->h_command_function;
1354 con->h_async_function = serviceptr->h_async_function;
1355 con->ServiceName = serviceptr->ServiceName;
1356 /* Determine whether it's a local socket */
1357 if (serviceptr->sockpath != NULL)
1358 con->is_local_socket = 1;
1360 /* Set the SO_REUSEADDR socket option */
1362 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
1364 become_session(con);
1366 serviceptr->h_greeting_function();
1367 become_session(NULL);
1368 con->state = CON_IDLE;
1374 CtdlThreadName(old_name);
1378 * Select on client socket.
1379 * First worker thread in here acquires the lock and builds an FDSET of client sockets.
1380 * then it selects on the client sockets timing out after 1 second.
1381 * If it times out the thread goes off to check on housekeeping etc.
1382 * If the select succeeds the thread goes off to handle the client request.
1383 * If the list of client connections is empty the threads all sleep for one second
1385 struct CitContext *select_on_client(void)
1391 const char *old_name;
1394 old_name = CtdlThreadName("select_on_client");
1396 /* Initialise the fdset */
1398 FD_SET(CT->Context->client_socket, &readfds);
1399 highest = CT->Context->client_socket;
1400 /* Now we can select on any connections that are waiting */
1402 if (!CtdlThreadCheckStop())
1404 tv.tv_sec = config.c_sleeping; /* wake up every second if no input */
1406 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
1408 else /* Shutting down? */
1410 CtdlThreadName(old_name);
1415 /* Now figure out who made this select() unblock.
1416 * First, check for an error or exit condition.
1419 if (errno == EBADF) {
1420 CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1423 if (errno != EINTR) {
1424 CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1425 CtdlThreadStopAll();
1426 } else if (!CtdlThreadCheckStop()) {
1427 CtdlLogPrintf(CTDL_DEBUG, "Un handled select failure.\n");
1429 CtdlThreadName(old_name);
1432 else if(retval == 0)
1434 CtdlThreadName(old_name);
1435 CT->Context->kill_me = 1;
1440 CT->Context->state = CON_EXECUTING;
1441 CT->Context->input_waiting = 1;
1443 CtdlThreadName(old_name);
1444 return (CT->Context);
1450 * Do the worker threads work when needed
1452 int execute_session(struct CitContext *bind_me)
1456 become_session(bind_me);
1458 /* If the client has sent a command, execute it. */
1459 if (CC->input_waiting) {
1460 CC->h_command_function();
1461 CC->input_waiting = 0;
1464 /* If there are asynchronous messages waiting and the
1465 * client supports it, do those now */
1466 if ((CC->is_async) && (CC->async_waiting)
1467 && (CC->h_async_function != NULL)) {
1468 CC->h_async_function();
1469 CC->async_waiting = 0;
1472 force_purge = CC->kill_me;
1475 become_session(NULL);
1476 bind_me->state = CON_IDLE;
1482 extern void dead_session_purge(int force);
1485 * A new worker_thread loop.
1488 void *new_worker_thread(void *arg)
1490 struct CitContext *bind_me;
1493 while (!CtdlThreadCheckStop()) {
1495 /* make doubly sure we're not holding any stale db handles
1496 * which might cause a deadlock.
1498 cdb_check_handles();
1500 bind_me = NULL; /* Which session shall we handle? */
1502 if (CT->Context == NULL)
1504 if (CtdlThreadCheckStop())
1508 bind_me = select_on_client();
1509 if (CtdlThreadCheckStop())
1513 force_purge = execute_session(bind_me);
1515 dead_session_purge(force_purge);
1516 if (CtdlThreadCheckStop())