a1497d98c35907ce25c6198f95a1ec9e407cb146
[citadel.git] / citadel / threads.c
1 /*
2  * $Id: sysdep.c 5882 2007-12-13 19:46:05Z davew $
3  *
4  * Citadel "system dependent" stuff.
5  * See copyright.txt for copyright information.
6  *
7  * Here's where we have the Citadel thread implimentation
8  *
9  */
10
11 #include <sys/types.h>
12 #include <errno.h>
13 #include <sys/socket.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16
17 #if TIME_WITH_SYS_TIME
18 # include <sys/time.h>
19 # include <time.h>
20 #else
21 # if HAVE_SYS_TIME_H
22 #  include <sys/time.h>
23 # else
24 #  include <time.h>
25 # endif
26 #endif
27
28 #include "threads.h"
29 #include "ctdl_module.h"
30 #include "modules_init.h"
31 #include "housekeeping.h"
32 #include "config.h"
33 #include "citserver.h"
34 #include "sysdep_decls.h"
35
36 /*
37  * define this to use the new worker_thread method of handling connections
38  */
39 //#define NEW_WORKER
40
41 /*
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;
46  * pass in &node
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
52  */
53
54 static int num_threads = 0;                     /* Current number of threads */
55 static int num_workers = 0;                     /* Current number of worker threads */
56
57 CtdlThreadNode *CtdlThreadList = NULL;
58 CtdlThreadNode *CtdlThreadSchedList = NULL;
59
60 static CtdlThreadNode *GC_thread = NULL;
61 static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE];
62 double CtdlThreadLoadAvg = 0;
63 double CtdlThreadWorkerAvg = 0;
64 citthread_key_t ThreadKey;
65
66 citthread_mutex_t Critters[MAX_SEMAPHORES];     /* Things needing locking */
67
68
69
70 void InitialiseSemaphores(void)
71 {
72         int i;
73
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);
77         }
78 }
79
80
81
82
83 /*
84  * Obtain a semaphore lock to begin a critical section.
85  * but only if no one else has one
86  */
87 int try_critical_section(int which_one)
88 {
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.
92          */
93         if (    (which_one != S_FLOORCACHE)
94 #ifdef DEBUG_MEMORY_LEAKS
95                 && (which_one != S_DEBUGMEMLEAKS)
96 #endif
97                 && (which_one != S_RPLIST)
98         ) {
99                 cdb_check_handles();
100         }
101         return (citthread_mutex_trylock(&Critters[which_one]));
102 }
103
104
105 /*
106  * Obtain a semaphore lock to begin a critical section.
107  */
108 void begin_critical_section(int which_one)
109 {
110         /* CtdlLogPrintf(CTDL_DEBUG, "begin_critical_section(%d)\n", which_one); */
111
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.
115          */
116         if (    (which_one != S_FLOORCACHE)
117 #ifdef DEBUG_MEMORY_LEAKS
118                 && (which_one != S_DEBUGMEMLEAKS)
119 #endif
120                 && (which_one != S_RPLIST)
121         ) {
122                 cdb_check_handles();
123         }
124         citthread_mutex_lock(&Critters[which_one]);
125 }
126
127 /*
128  * Release a semaphore lock to end a critical section.
129  */
130 void end_critical_section(int which_one)
131 {
132         citthread_mutex_unlock(&Critters[which_one]);
133 }
134
135
136 /*
137  * A function to destroy the TSD
138  */
139 static void ctdl_thread_internal_dest_tsd(void *arg)
140 {
141         if (arg != NULL) {
142                 check_handles(arg);
143                 free(arg);
144         }
145 }
146
147
148 /*
149  * A function to initialise the thread TSD
150  */
151 void ctdl_thread_internal_init_tsd(void)
152 {
153         int ret;
154         
155         if ((ret = citthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
156                 CtdlLogPrintf(CTDL_EMERG, "citthread_key_create: %s\n", strerror(ret));
157                 exit(CTDLEXIT_DB);
158         }
159 }
160
161 /*
162  * Ensure that we have a key for thread-specific data. 
163  *
164  * This should be called immediately after startup by any thread 
165  * 
166  */
167 void CtdlThreadAllocTSD(void)
168 {
169         ThreadTSD *tsd;
170
171         if (citthread_getspecific(ThreadKey) != NULL)
172                 return;
173
174         tsd = malloc(sizeof(ThreadTSD));
175
176         tsd->tid = NULL;
177
178         memset(tsd->cursors, 0, sizeof tsd->cursors);
179         tsd->self = NULL;
180         
181         citthread_setspecific(ThreadKey, tsd);
182 }
183
184
185 void ctdl_thread_internal_free_tsd(void)
186 {
187         ctdl_thread_internal_dest_tsd(citthread_getspecific(ThreadKey));
188         citthread_setspecific(ThreadKey, NULL);
189 }
190
191
192 void ctdl_thread_internal_cleanup(void)
193 {
194         int i;
195         CtdlThreadNode *this_thread, *that_thread;
196         
197         for (i=0; i<CTDL_THREAD_LAST_STATE; i++)
198         {
199                 free (CtdlThreadStates[i]);
200         }
201         
202         /* Clean up the scheduled thread list */
203         this_thread = CtdlThreadSchedList;
204         while (this_thread)
205         {
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);
213                 free(that_thread);
214         }
215         ctdl_thread_internal_free_tsd();
216 }
217
218 void ctdl_thread_internal_init(void)
219 {
220         CtdlThreadNode *this_thread;
221         int ret = 0;
222         
223         CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread");
224         CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread");
225         CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created");
226         CtdlThreadStates[CTDL_THREAD_CANCELLED] = strdup("Thread Cancelled");
227         CtdlThreadStates[CTDL_THREAD_EXITED] = strdup("Thread Exited");
228         CtdlThreadStates[CTDL_THREAD_STOPPING] = strdup("Thread Stopping");
229         CtdlThreadStates[CTDL_THREAD_STOP_REQ] = strdup("Thread Stop Requested");
230         CtdlThreadStates[CTDL_THREAD_SLEEPING] = strdup("Thread Sleeping");
231         CtdlThreadStates[CTDL_THREAD_RUNNING] = strdup("Thread Running");
232         CtdlThreadStates[CTDL_THREAD_BLOCKED] = strdup("Thread Blocked");
233         
234         /* Get ourself a thread entry */
235         this_thread = malloc(sizeof(CtdlThreadNode));
236         if (this_thread == NULL) {
237                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
238                 return;
239         }
240         // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
241         memset (this_thread, 0, sizeof(CtdlThreadNode));
242         
243         citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
244         citthread_cond_init (&(this_thread->ThreadCond), NULL);
245         citthread_mutex_init (&(this_thread->SleepMutex), NULL);
246         citthread_cond_init (&(this_thread->SleepCond), NULL);
247         
248         /* We are garbage collector so create us as running */
249         this_thread->state = CTDL_THREAD_RUNNING;
250         
251         if ((ret = citthread_attr_init(&this_thread->attr))) {
252                 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
253                 free(this_thread);
254                 return;
255         }
256
257         this_thread->name = "Garbage Collection Thread";
258         
259         this_thread->tid = citthread_self();
260         GC_thread = this_thread;
261         CT = this_thread;
262         
263         num_threads++;  // Increase the count of threads in the system.
264
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. */
272 }
273
274
275 /*
276  * A function to update a threads load averages
277  */
278  void ctdl_thread_internal_update_avgs(CtdlThreadNode *this_thread)
279  {
280         struct timeval now, result;
281         double last_duration;
282
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);
297 }
298
299 /*
300  * A function to chenge the state of a thread
301  */
302 void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlThreadState new_state)
303 {
304         /*
305          * Wether we change state or not we need update the load values
306          */
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);
317 }
318
319
320 /*
321  * A function to tell all threads to exit
322  */
323 void CtdlThreadStopAll(void)
324 {
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
327         
328         CtdlThreadNode *this_thread;
329         
330         begin_critical_section(S_THREAD_LIST);
331         this_thread = CtdlThreadList;
332         while(this_thread)
333         {
334 #ifdef THREADS_USESIGNALS
335                 citthread_killl(this_thread->tid, SIGHUP);
336 #endif
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;
343         }
344         end_critical_section(S_THREAD_LIST);
345 }
346
347
348 /*
349  * A function to wake up all sleeping threads
350  */
351 void CtdlThreadWakeAll(void)
352 {
353         CtdlThreadNode *this_thread;
354         
355         CtdlLogPrintf(CTDL_DEBUG, "Thread system waking all threads.\n");
356         
357         begin_critical_section(S_THREAD_LIST);
358         this_thread = CtdlThreadList;
359         while(this_thread)
360         {
361                 if (!this_thread->thread_func)
362                 {
363                         citthread_cond_signal(&this_thread->ThreadCond);
364                         citthread_cond_signal(&this_thread->SleepCond);
365                 }
366                 this_thread = this_thread->next;
367         }
368         end_critical_section(S_THREAD_LIST);
369 }
370
371
372 /*
373  * A function to return the number of threads running in the system
374  */
375 int CtdlThreadGetCount(void)
376 {
377         return  num_threads;
378 }
379
380 int CtdlThreadGetWorkers(void)
381 {
382         return  num_workers;
383 }
384
385 double CtdlThreadGetWorkerAvg(void)
386 {
387         double ret;
388         
389         begin_critical_section(S_THREAD_LIST);
390         ret =  CtdlThreadWorkerAvg;
391         end_critical_section(S_THREAD_LIST);
392         return ret;
393 }
394
395 double CtdlThreadGetLoadAvg(void)
396 {
397         double ret;
398         
399         begin_critical_section(S_THREAD_LIST);
400         ret =  CtdlThreadLoadAvg;
401         end_critical_section(S_THREAD_LIST);
402         return ret;
403 }
404
405
406
407
408 /*
409  * A function to rename a thread
410  * Returns a const char *
411  */
412 const char *CtdlThreadName(const char *name)
413 {
414         const char *old_name;
415         
416         if (!CT)
417         {
418                 CtdlLogPrintf(CTDL_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
419                 return NULL;
420         }
421         old_name = CT->name;
422         if (name)
423                 CT->name = name;
424         return (old_name);
425 }       
426
427
428 /*
429  * A function to force a thread to exit
430  */
431 void CtdlThreadCancel(CtdlThreadNode *thread)
432 {
433         CtdlThreadNode *this_thread;
434         
435         if (!thread)
436                 this_thread = CT;
437         else
438                 this_thread = thread;
439         if (!this_thread)
440         {
441                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
442                 CtdlThreadStopAll();
443                 return;
444         }
445         
446         if (!this_thread->thread_func)
447         {
448                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() the garbage collector.\n");
449                 CtdlThreadStopAll();
450                 return;
451         }
452         
453         ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED);
454         citthread_cancel(this_thread->tid);
455 }
456
457
458 /*
459  * A function for a thread to check if it has been asked to stop
460  */
461 int CtdlThreadCheckStop(void)
462 {
463         int state;
464         
465         if (!CT)
466         {
467                 CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
468                 CtdlThreadStopAll();
469                 return -1;
470         }
471         
472         state = CT->state;
473
474 #ifdef THREADS_USESIGNALS
475         if (CT->signal)
476                 CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
477 #endif
478         if(state == CTDL_THREAD_STOP_REQ)
479         {
480                 CT->state = CTDL_THREAD_STOPPING;
481                 return -1;
482         }
483         else if((state < CTDL_THREAD_STOP_REQ) && (state > CTDL_THREAD_CREATE))
484         {
485                 return -1;
486         }
487         return 0;
488 }
489
490
491 /*
492  * A function to ask a thread to exit
493  * The thread must call CtdlThreadCheckStop() periodically to determine if it should exit
494  */
495 void CtdlThreadStop(CtdlThreadNode *thread)
496 {
497         CtdlThreadNode *this_thread;
498         
499         if (!thread)
500                 this_thread = CT;
501         else
502                 this_thread = thread;
503         if (!this_thread)
504                 return;
505         if (!(this_thread->thread_func))
506                 return;         // Don't stop garbage collector
507 #ifdef THREADS_USESIGNALS
508         citthread_kill(this_thread->tid, SIGHUP);       
509 #endif
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);
513 }
514
515 /*
516  * So we now have a sleep command that works with threads but it is in seconds
517  */
518 void CtdlThreadSleep(int secs)
519 {
520         struct timespec wake_time;
521         struct timeval time_now;
522         
523         
524         if (!CT)
525         {
526                 CtdlLogPrintf(CTDL_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
527                 return;
528         }
529         
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;
534
535         ctdl_thread_internal_change_state (CT, CTDL_THREAD_SLEEPING);
536         
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);
540         
541         ctdl_thread_internal_change_state (CT, CTDL_THREAD_RUNNING);
542 }
543
544
545 /*
546  * Routine to clean up our thread function on exit
547  */
548 static void ctdl_internal_thread_cleanup(void *arg)
549 {
550         /*
551          * In here we were called by the current thread because it is exiting
552          * NB. WE ARE THE CURRENT THREAD
553          */
554         CtdlLogPrintf(CTDL_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", CT->name, CT->tid);
555         
556         #ifdef HAVE_BACKTRACE
557         eCrash_UnregisterThread();
558         #endif
559         
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);
563 }
564
565 /*
566  * A quick function to show the load averages
567  */
568 void ctdl_thread_internal_calc_loadavg(void)
569 {
570         CtdlThreadNode *that_thread;
571         double load_avg, worker_avg;
572         int workers = 0;
573
574         that_thread = CtdlThreadList;
575         load_avg = 0;
576         worker_avg = 0;
577         while(that_thread)
578         {
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)
588                 {
589                         worker_avg += that_thread->load_avg;
590                         workers++;
591                 }
592 #ifdef WITH_THREADLOG
593                 CtdlLogPrintf(CTDL_DEBUG, "CtdlThread, \"%s\" (%lu) \"%s\" %.2f %.2f %.2f %.2f\n",
594                         that_thread->name,
595                         that_thread->tid,
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);
601 #endif
602                 citthread_mutex_unlock(&that_thread->ThreadMutex);
603                 that_thread = that_thread->next;
604         }
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);
609 #endif
610 }
611
612
613 /*
614  * Garbage collection routine.
615  * Gets called by main() in a loop to clean up the thread list periodically.
616  */
617 void CtdlThreadGC (void)
618 {
619         CtdlThreadNode *this_thread, *that_thread;
620         int workers = 0, sys_workers;
621         int ret=0;
622         
623         begin_critical_section(S_THREAD_LIST);
624         
625         /* Handle exiting of garbage collector thread */
626         if(num_threads == 1)
627                 CtdlThreadList->state = CTDL_THREAD_EXITED;
628         
629 #ifdef WITH_THREADLOG
630         CtdlLogPrintf(CTDL_DEBUG, "Thread system running garbage collection.\n");
631 #endif
632         /*
633          * Woke up to do garbage collection
634          */
635         this_thread = CtdlThreadList;
636         while(this_thread)
637         {
638                 that_thread = this_thread;
639                 this_thread = this_thread->next;
640                 
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++;
644                 else
645                 {
646                         /**
647                          * Catch the situation where a worker was asked to stop but couldn't and we are not
648                          * shutting down.
649                          */
650                         that_thread->stop_ticker = 0;
651                 }
652                 
653                 if (that_thread->stop_ticker == 5)
654                 {
655                         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);
656                         if ((that_thread->flags & CTDLTHREAD_WORKER) == 0)
657                                 CtdlLogPrintf(CTDL_INFO, "Thread System: A non worker thread would have been canceled this may cause message loss.\n");
658 //                      that_thread->state = CTDL_THREAD_CANCELLED;
659                         that_thread->stop_ticker++;
660 //                      citthread_cancel(that_thread->tid);
661 //                      continue;
662                 }
663                 
664                 /* Do we need to clean up this thread? */
665                 if ((that_thread->state != CTDL_THREAD_EXITED) && (that_thread->state != CTDL_THREAD_CANCELLED))
666                 {
667                         if(that_thread->flags & CTDLTHREAD_WORKER)
668                                 workers++;      /* Sanity check on number of worker threads */
669                         continue;
670                 }
671                 
672                 if (citthread_equal(that_thread->tid, citthread_self()) && that_thread->thread_func)
673                 {       /* Sanity check */
674                         end_critical_section(S_THREAD_LIST);
675                         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, a thread is trying to clean up after itself.\n");
676                         abort();
677                         return;
678                 }
679                 
680                 if (num_threads <= 0)
681                 {       /* Sanity check */
682                         end_critical_section(S_THREAD_LIST);
683                         CtdlLogPrintf(CTDL_EMERG, "Thread system PANIC, num_threads <= 0 and trying to do Garbage Collection.\n");
684                         abort();
685                         return;
686                 }
687
688                 if(that_thread->flags & CTDLTHREAD_WORKER)
689                         num_workers--;  /* This is a wroker thread so reduce the count. */
690                 num_threads--;
691                 /* If we are unlinking the list head then the next becomes the list head */
692                 if(that_thread->prev)
693                         that_thread->prev->next = that_thread->next;
694                 else
695                         CtdlThreadList = that_thread->next;
696                 if(that_thread->next)
697                         that_thread->next->prev = that_thread->prev;
698                 
699                 citthread_cond_signal(&that_thread->ThreadCond);
700                 citthread_cond_signal(&that_thread->SleepCond); // Make sure this thread is awake
701                 citthread_mutex_lock(&that_thread->ThreadMutex);        // Make sure it has done what its doing
702                 citthread_mutex_unlock(&that_thread->ThreadMutex);
703                 /*
704                  * Join on the thread to do clean up and prevent memory leaks
705                  * Also makes sure the thread has cleaned up after itself before we remove it from the list
706                  * We can join on the garbage collector thread the join should just return EDEADLCK
707                  */
708                 ret = citthread_join (that_thread->tid, NULL);
709                 if (ret == EDEADLK)
710                         CtdlLogPrintf(CTDL_DEBUG, "Garbage collection on own thread.\n");
711                 else if (ret == EINVAL)
712                         CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, that thread already joined on.\n");
713                 else if (ret == ESRCH)
714                         CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, no thread to join on.\n");
715                 else if (ret != 0)
716                         CtdlLogPrintf(CTDL_DEBUG, "Garbage collection, citthread_join returned an unknown error(%d).\n", ret);
717                 /*
718                  * Now we own that thread entry
719                  */
720                 CtdlLogPrintf(CTDL_INFO, "Garbage Collection for thread \"%s\" (0x%08lx).\n",
721                         that_thread->name, that_thread->tid);
722                 citthread_mutex_destroy(&that_thread->ThreadMutex);
723                 citthread_cond_destroy(&that_thread->ThreadCond);
724                 citthread_mutex_destroy(&that_thread->SleepMutex);
725                 citthread_cond_destroy(&that_thread->SleepCond);
726                 citthread_attr_destroy(&that_thread->attr);
727                 free(that_thread);
728         }
729         sys_workers = num_workers;
730         end_critical_section(S_THREAD_LIST);
731         
732         /* Sanity check number of worker threads */
733         if (workers != sys_workers)
734         {
735                 CtdlLogPrintf(CTDL_EMERG,
736                         "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
737                         workers, sys_workers
738                         );
739                 abort();
740         }
741 }
742
743
744
745  
746 /*
747  * Runtime function for a Citadel Thread.
748  * This initialises the threads environment and then calls the user supplied thread function
749  * Note that this is the REAL thread function and wraps the users thread function.
750  */ 
751 static void *ctdl_internal_thread_func (void *arg)
752 {
753         CtdlThreadNode *this_thread;
754         void *ret = NULL;
755
756         /* lock and unlock the thread list.
757          * This causes this thread to wait until all its creation stuff has finished before it
758          * can continue its execution.
759          */
760         begin_critical_section(S_THREAD_LIST);
761         this_thread = (CtdlThreadNode *) arg;
762         gettimeofday(&this_thread->start_time, NULL);           /* Time this thread started */
763 //      citthread_mutex_lock(&this_thread->ThreadMutex);
764         
765         // Register the cleanup function to take care of when we exit.
766         citthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
767         // Get our thread data structure
768         CtdlThreadAllocTSD();
769         CT = this_thread;
770         this_thread->pid = getpid();
771         memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval));     /* Changed state so mark it. */
772         /* Only change to running state if we weren't asked to stop during the create cycle
773          * Other wise there is a window to allow this threads creation to continue to full grown and
774          * therby prevent a shutdown of the server.
775          */
776 //      citthread_mutex_unlock(&this_thread->ThreadMutex);
777                 
778         if (!CtdlThreadCheckStop())
779         {
780                 citthread_mutex_lock(&this_thread->ThreadMutex);
781                 this_thread->state = CTDL_THREAD_RUNNING;
782                 citthread_mutex_unlock(&this_thread->ThreadMutex);
783         }
784         end_critical_section(S_THREAD_LIST);
785         
786         // Register for tracing
787         #ifdef HAVE_BACKTRACE
788         eCrash_RegisterThread(this_thread->name, 0);
789         #endif
790         
791         // Tell the world we are here
792         CtdlLogPrintf(CTDL_NOTICE, "Created a new thread \"%s\" (0x%08lx).\n",
793                 this_thread->name, this_thread->tid);
794         
795         /*
796          * run the thread to do the work but only if we haven't been asked to stop
797          */
798         if (!CtdlThreadCheckStop())
799                 ret = (this_thread->thread_func)(this_thread->user_args);
800         
801         /*
802          * Our thread is exiting either because it wanted to end or because the server is stopping
803          * We need to clean up
804          */
805         citthread_cleanup_pop(1);       // Execute our cleanup routine and remove it
806         
807         return(ret);
808 }
809
810
811
812
813 /*
814  * Function to initialise an empty thread structure
815  */
816 CtdlThreadNode *ctdl_internal_init_thread_struct(CtdlThreadNode *this_thread, long flags)
817 {
818         int ret = 0;
819         
820         // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
821         memset (this_thread, 0, sizeof(CtdlThreadNode));
822         
823         /* Create the mutex's early so we can use them */
824         citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
825         citthread_cond_init (&(this_thread->ThreadCond), NULL);
826         citthread_mutex_init (&(this_thread->SleepMutex), NULL);
827         citthread_cond_init (&(this_thread->SleepCond), NULL);
828         
829         this_thread->state = CTDL_THREAD_CREATE;
830         
831         if ((ret = citthread_attr_init(&this_thread->attr))) {
832                 citthread_mutex_unlock(&this_thread->ThreadMutex);
833                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
834                 citthread_cond_destroy(&(this_thread->ThreadCond));
835                 citthread_mutex_destroy(&(this_thread->SleepMutex));
836                 citthread_cond_destroy(&(this_thread->SleepCond));
837                 CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
838                 free(this_thread);
839                 return NULL;
840         }
841
842         /* Our per-thread stacks need to be bigger than the default size,
843          * otherwise the MIME parser crashes on FreeBSD, and the IMAP service
844          * crashes on 64-bit Linux.
845          */
846         if (flags & CTDLTHREAD_BIGSTACK)
847         {
848 #ifdef WITH_THREADLOG
849                 CtdlLogPrintf(CTDL_INFO, "Thread system. Creating BIG STACK thread.\n");
850 #endif
851                 if ((ret = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
852                         citthread_mutex_unlock(&this_thread->ThreadMutex);
853                         citthread_mutex_destroy(&(this_thread->ThreadMutex));
854                         citthread_cond_destroy(&(this_thread->ThreadCond));
855                         citthread_mutex_destroy(&(this_thread->SleepMutex));
856                         citthread_cond_destroy(&(this_thread->SleepCond));
857                         citthread_attr_destroy(&this_thread->attr);
858                         CtdlLogPrintf(CTDL_EMERG, "Thread system, citthread_attr_setstacksize: %s\n",
859                                 strerror(ret));
860                         free(this_thread);
861                         return NULL;
862                 }
863         }
864
865         /* Set this new thread with an avg_blocked of 2. We do this so that its creation affects the
866          * load average for the system. If we don't do this then we create a mass of threads at the same time 
867          * because the creation didn't affect the load average.
868          */
869         this_thread->avg_blocked = 2;
870         
871         return (this_thread);
872 }
873
874
875
876  
877 /*
878  * Internal function to create a thread.
879  */ 
880 CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args)
881 {
882         int ret = 0;
883         CtdlThreadNode *this_thread;
884
885         if (num_threads >= 32767)
886         {
887                 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
888                 return NULL;
889         }
890                 
891         this_thread = malloc(sizeof(CtdlThreadNode));
892         if (this_thread == NULL) {
893                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
894                 return NULL;
895         }
896         
897         /* Initialise the thread structure */
898         if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
899         {
900                 free(this_thread);
901                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
902                 return NULL;
903         }
904         /*
905          * If we got here we are going to create the thread so we must initilise the structure
906          * first because most implimentations of threading can't create it in a stopped state
907          * and it might want to do things with its structure that aren't initialised otherwise.
908          */
909         if(name)
910         {
911                 this_thread->name = name;
912         }
913         else
914         {
915                 this_thread->name = "Un-named Thread";
916         }
917         
918         this_thread->flags = flags;
919         this_thread->thread_func = thread_func;
920         this_thread->user_args = args;
921         
922 //      citthread_mutex_lock(&this_thread->ThreadMutex);
923         
924         begin_critical_section(S_THREAD_LIST);
925         /*
926          * We pass this_thread into the thread as its args so that it can find out information
927          * about itself and it has a bit of storage space for itself, not to mention that the REAL
928          * thread function needs to finish off the setup of the structure
929          */
930         if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
931         {
932                 end_critical_section(S_THREAD_LIST);
933                 CtdlLogPrintf(CTDL_ALERT, "Thread system, Can't create thread: %s\n",
934                         strerror(ret));
935                 citthread_mutex_unlock(&this_thread->ThreadMutex);
936                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
937                 citthread_cond_destroy(&(this_thread->ThreadCond));
938                 citthread_mutex_destroy(&(this_thread->SleepMutex));
939                 citthread_cond_destroy(&(this_thread->SleepCond));
940                 citthread_attr_destroy(&this_thread->attr);
941                 free(this_thread);
942                 return NULL;
943         }
944         
945         num_threads++;  // Increase the count of threads in the system.
946         if(this_thread->flags & CTDLTHREAD_WORKER)
947                 num_workers++;
948
949         this_thread->next = CtdlThreadList;
950         CtdlThreadList = this_thread;
951         if (this_thread->next)
952                 this_thread->next->prev = this_thread;
953         ctdl_thread_internal_calc_loadavg();
954         
955 //      citthread_mutex_unlock(&this_thread->ThreadMutex);
956         end_critical_section(S_THREAD_LIST);
957         
958         return this_thread;
959 }
960
961 /*
962  * Wrapper function to create a thread
963  * ensures the critical section and other protections are in place.
964  * char *name = name to give to thread, if NULL, use generic name
965  * int flags = flags to determine type of thread and standard facilities
966  */
967 CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args)
968 {
969         CtdlThreadNode *ret = NULL;
970         
971         ret = ctdl_internal_create_thread(name, flags, thread_func, args);
972         return ret;
973 }
974
975
976
977 /*
978  * Internal function to schedule a thread.
979  * Must be called from within a S_THREAD_LIST critical section
980  */ 
981 CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) (void *arg), void *args, time_t when)
982 {
983         CtdlThreadNode *this_thread;
984
985         if (num_threads >= 32767)
986         {
987                 CtdlLogPrintf(CTDL_EMERG, "Thread system. Thread list full.\n");
988                 return NULL;
989         }
990                 
991         this_thread = malloc(sizeof(CtdlThreadNode));
992         if (this_thread == NULL) {
993                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
994                 return NULL;
995         }
996         /* Initialise the thread structure */
997         if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
998         {
999                 free(this_thread);
1000                 CtdlLogPrintf(CTDL_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
1001                 return NULL;
1002         }
1003
1004         /*
1005          * If we got here we are going to create the thread so we must initilise the structure
1006          * first because most implimentations of threading can't create it in a stopped state
1007          * and it might want to do things with its structure that aren't initialised otherwise.
1008          */
1009         if(name)
1010         {
1011                 this_thread->name = name;
1012         }
1013         else
1014         {
1015                 this_thread->name = "Un-named Thread";
1016         }
1017         
1018         this_thread->flags = flags;
1019         this_thread->thread_func = thread_func;
1020         this_thread->user_args = args;
1021         
1022         /*
1023          * When to start this thread
1024          */
1025         this_thread->when = when;
1026
1027         begin_critical_section(S_SCHEDULE_LIST);
1028         this_thread->next = CtdlThreadSchedList;
1029         CtdlThreadSchedList = this_thread;
1030         if (this_thread->next)
1031                 this_thread->next->prev = this_thread;
1032         end_critical_section(S_SCHEDULE_LIST);
1033         
1034         return this_thread;
1035 }
1036
1037
1038
1039 CtdlThreadNode *ctdl_thread_internal_start_scheduled (CtdlThreadNode *this_thread)
1040 {
1041         int ret = 0;
1042         
1043 //      citthread_mutex_lock(&that_thread->ThreadMutex);
1044         begin_critical_section(S_THREAD_LIST);
1045         /*
1046          * We pass this_thread into the thread as its args so that it can find out information
1047          * about itself and it has a bit of storage space for itself, not to mention that the REAL
1048          * thread function needs to finish off the setup of the structure
1049          */
1050         if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
1051         {
1052                 end_critical_section(S_THREAD_LIST);
1053                 CtdlLogPrintf(CTDL_DEBUG, "Failed to start scheduled thread \"%s\": %s\n", this_thread->name, strerror(ret));
1054 //              citthread_mutex_unlock(&this_thread->ThreadMutex);
1055                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
1056                 citthread_cond_destroy(&(this_thread->ThreadCond));
1057                 citthread_mutex_destroy(&(this_thread->SleepMutex));
1058                 citthread_cond_destroy(&(this_thread->SleepCond));
1059                 citthread_attr_destroy(&this_thread->attr);
1060                 free(this_thread);
1061                 return NULL;
1062         }
1063         
1064         
1065         num_threads++;  // Increase the count of threads in the system.
1066         if(this_thread->flags & CTDLTHREAD_WORKER)
1067                 num_workers++;
1068
1069         this_thread->next = CtdlThreadList;
1070         CtdlThreadList = this_thread;
1071         if (this_thread->next)
1072                 this_thread->next->prev = this_thread;
1073 //      citthread_mutex_unlock(&that_thread->ThreadMutex);
1074         
1075         ctdl_thread_internal_calc_loadavg();
1076         end_critical_section(S_THREAD_LIST);
1077         
1078         
1079         return this_thread;
1080 }
1081
1082
1083
1084 void ctdl_thread_internal_check_scheduled(void)
1085 {
1086         CtdlThreadNode *this_thread, *that_thread;
1087         time_t now;
1088         
1089         if (try_critical_section(S_SCHEDULE_LIST))
1090                 return; /* If this list is locked we wait till the next chance */
1091         
1092         now = time(NULL);
1093         
1094 #ifdef WITH_THREADLOG
1095         CtdlLogPrintf(CTDL_DEBUG, "Checking for scheduled threads to start.\n");
1096 #endif
1097
1098         this_thread = CtdlThreadSchedList;
1099         while(this_thread)
1100         {
1101                 that_thread = this_thread;
1102                 this_thread = this_thread->next;
1103                 
1104                 if (now > that_thread->when)
1105                 {
1106                         /* Unlink from schedule list */
1107                         if (that_thread->prev)
1108                                 that_thread->prev->next = that_thread->next;
1109                         else
1110                                 CtdlThreadSchedList = that_thread->next;
1111                         if (that_thread->next)
1112                                 that_thread->next->prev = that_thread->prev;
1113                                 
1114                         that_thread->next = that_thread->prev = NULL;
1115 #ifdef WITH_THREADLOG
1116                         CtdlLogPrintf(CTDL_DEBUG, "About to start scheduled thread \"%s\".\n", that_thread->name);
1117 #endif
1118                         if (CT->state > CTDL_THREAD_STOP_REQ)
1119                         {       /* Only start it if the system is not stopping */
1120                                 if (ctdl_thread_internal_start_scheduled (that_thread))
1121                                 {
1122 #ifdef WITH_THREADLOG
1123                                         CtdlLogPrintf(CTDL_INFO, "Thread system, Started a scheduled thread \"%s\" (%ud).\n",
1124                                                 that_thread->name, that_thread->tid);
1125 #endif
1126                                 }
1127                         }
1128                 }
1129 #ifdef WITH_THREADLOG
1130                 else
1131                 {
1132                         CtdlLogPrintf(CTDL_DEBUG, "Thread \"%s\" will start in %ld seconds.\n",
1133                                 that_thread->name, that_thread->when - time(NULL));
1134                 }
1135 #endif
1136         }
1137         end_critical_section(S_SCHEDULE_LIST);
1138 }
1139
1140
1141 /*
1142  * A warapper function for select so we can show a thread as blocked
1143  */
1144 int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
1145 {
1146         int ret;
1147         
1148         ctdl_thread_internal_change_state(CT, CTDL_THREAD_BLOCKED);
1149         ret = select(n, readfds, writefds, exceptfds, timeout);
1150         /**
1151          * If the select returned <= 0 then it failed due to an error
1152          * or timeout so this thread could stop if asked to do so.
1153          * Anything else means it needs to continue unless the system is shutting down
1154          */
1155         if (ret <= 0)
1156         {
1157                 /**
1158                  * select says nothing to do so we can change to running if we haven't been asked to stop.
1159                  */
1160                 ctdl_thread_internal_change_state(CT, CTDL_THREAD_RUNNING);
1161         }
1162         else
1163         {
1164                 /**
1165                  * The select says this thread needs to do something useful.
1166                  * This thread was in an idle state so it may have been asked to stop
1167                  * but if the system isn't shutting down this thread is no longer
1168                  * idle and select has given it a task to do so it must not stop
1169                  * In this condition we need to force it into the running state.
1170                  * CtdlThreadGC will clear its ticker for us.
1171                  */
1172                 if (GC_thread->state > CTDL_THREAD_STOP_REQ)
1173                 {
1174                         citthread_mutex_lock(&CT->ThreadMutex); /* To prevent race condition of a sleeping thread */
1175                         CT->state = CTDL_THREAD_RUNNING;
1176                         citthread_mutex_unlock(&CT->ThreadMutex);
1177                 }
1178         }
1179
1180         return ret;
1181 }
1182
1183
1184
1185 void *new_worker_thread(void *arg);
1186 extern void close_masters (void);
1187
1188
1189
1190 void go_threading(void)
1191 {
1192         int i;
1193         CtdlThreadNode *last_worker;
1194         
1195         /*
1196          * Initialise the thread system
1197          */
1198         ctdl_thread_internal_init();
1199
1200         /* Second call to module init functions now that threading is up */
1201         initialise_modules(1);
1202
1203         /*
1204          * This thread is now used for garbage collection of other threads in the thread list
1205          */
1206         CtdlLogPrintf(CTDL_INFO, "Startup thread %d becoming garbage collector,\n", citthread_self());
1207
1208         /*
1209          * We do a lot of locking and unlocking of the thread list in here.
1210          * We do this so that we can repeatedly release time for other threads
1211          * that may be waiting on the thread list.
1212          * We are a low priority thread so we can afford to do this
1213          */
1214         
1215         while (CtdlThreadGetCount())
1216         {
1217                 if (CT->signal)
1218                         exit_signal = CT->signal;
1219                 if (exit_signal)
1220                 {
1221                         CtdlThreadStopAll();
1222 //                      close_masters();
1223                 }
1224                 check_sched_shutdown();
1225                 if (CT->state > CTDL_THREAD_STOP_REQ)
1226                 {
1227                         begin_critical_section(S_THREAD_LIST);
1228                         ctdl_thread_internal_calc_loadavg();
1229                         end_critical_section(S_THREAD_LIST);
1230                         
1231                         ctdl_thread_internal_check_scheduled(); /* start scheduled threads */
1232                 }
1233                 
1234                 /* Reduce the size of the worker thread pool if necessary. */
1235                 if ((CtdlThreadGetWorkers() > config.c_min_workers + 1) && (CtdlThreadWorkerAvg < 20) && (CT->state > CTDL_THREAD_STOP_REQ))
1236                 {
1237                         /* Ask a worker thread to stop as we no longer need it */
1238                         begin_critical_section(S_THREAD_LIST);
1239                         last_worker = CtdlThreadList;
1240                         while (last_worker)
1241                         {
1242                                 citthread_mutex_lock(&last_worker->ThreadMutex);
1243                                 if (last_worker->flags & CTDLTHREAD_WORKER && (last_worker->state > CTDL_THREAD_STOPPING) && (last_worker->Context == NULL))
1244                                 {
1245                                         citthread_mutex_unlock(&last_worker->ThreadMutex);
1246                                         break;
1247                                 }
1248                                 citthread_mutex_unlock(&last_worker->ThreadMutex);
1249                                 last_worker = last_worker->next;
1250                         }
1251                         end_critical_section(S_THREAD_LIST);
1252                         if (last_worker)
1253                         {
1254 #ifdef WITH_THREADLOG
1255                                 CtdlLogPrintf(CTDL_DEBUG, "Thread system, stopping excess worker thread \"%s\" (0x%08lx).\n",
1256                                         last_worker->name,
1257                                         last_worker->tid
1258                                         );
1259 #endif
1260                                 CtdlThreadStop(last_worker);
1261                         }
1262                 }
1263         
1264                 /*
1265                  * If all our workers are working hard, start some more to help out
1266                  * with things
1267                  */
1268                 /* FIXME: come up with a better way to dynamically alter the number of threads
1269                  * based on the system load
1270                  */
1271 #ifdef NEW_WORKER
1272                 if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkers() <= num_sessions) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
1273 #else
1274                 if ((((CtdlThreadGetWorkers() < config.c_max_workers) && (CtdlThreadGetWorkerAvg() > 60) && (CtdlThreadGetLoadAvg() < 90) ) || CtdlThreadGetWorkers() < config.c_min_workers) && (CT->state > CTDL_THREAD_STOP_REQ))
1275 #endif /* NEW_WORKER */
1276                 {
1277                         for (i=0; i<5 ; i++)
1278                         {
1279 #ifdef NEW_WORKER
1280                                 CtdlThreadCreate("Worker Thread (new)",
1281                                         CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1282                                         new_worker_thread,
1283                                         NULL
1284                                         );
1285 #else
1286                                 CtdlThreadCreate("Worker Thread",
1287                                         CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1288                                         worker_thread,
1289                                         NULL
1290                                         );
1291 #endif /* NEW_WORKER */
1292                         }
1293                 }
1294                 
1295                 CtdlThreadGC();
1296                 
1297                 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
1298                 {
1299                         CtdlThreadGC();
1300                 }
1301                 
1302                 if (CtdlThreadGetCount())
1303                         CtdlThreadSleep(1);
1304         }
1305         /*
1306          * If the above loop exits we must be shutting down since we obviously have no threads
1307          */
1308         ctdl_thread_internal_cleanup();
1309 }
1310
1311
1312
1313
1314 /*
1315  * Starting a new implimentation of a worker thread.
1316  * This new implimentation will be faster and do more work per thread.
1317  */
1318  
1319 /*
1320  * Select on master socket.
1321  * First worker thread in here acquires the lock and builds an FDSET of master sockets.
1322  * then it goes into a loop selecting on the master sockets timing out every few milliseconds.
1323  * If it times out it rebiulds its list and loops.
1324  * If the select succeeds it creates a new context and returns.
1325  * During this time the other workers are selecting on existing contexts or sleeping.
1326  */
1327 void select_on_master(void)
1328 {
1329         fd_set readfds;
1330         struct ServiceFunctionHook *serviceptr;
1331         int ssock;                      /* Descriptor for client socket */
1332         int highest;
1333         int m, i;
1334         int retval = 0;
1335         struct timeval tv;
1336         struct CitContext *con;
1337         const char *old_name;
1338
1339
1340
1341         old_name = CtdlThreadName("select_on_master");
1342
1343         /* Initialize the fdset. */
1344         FD_ZERO(&readfds);
1345         highest = 0;
1346
1347         /* First, add the various master sockets to the fdset. */
1348         for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1349                 m = serviceptr->msock;
1350                 FD_SET(m, &readfds);
1351                 if (m > highest) {
1352                         highest = m;
1353                 }
1354         }
1355
1356         tv.tv_sec = 1;          /* wake up every 1 sec if no input */
1357         tv.tv_usec = 0;
1358         retval = CtdlThreadSelect(highest + 1, &readfds, NULL, NULL, &tv);
1359
1360         /* Select got an error or we are shutting down so get out */
1361         if (retval == 0 || CtdlThreadCheckStop()) {
1362                 CtdlThreadName(old_name);
1363                 return;
1364         }
1365
1366         /* Select says something happened on one of our master sockets so now we handle it */
1367         for (serviceptr = ServiceHookTable; serviceptr != NULL; serviceptr = serviceptr->next ) {
1368                 if (FD_ISSET(serviceptr->msock, &readfds)) {
1369                         ssock = accept(serviceptr->msock, NULL, 0);
1370                         if (ssock >= 0) {
1371                                 CtdlLogPrintf(CTDL_DEBUG, "New client socket %d\n", ssock);
1372                                 /* The master socket is non-blocking but the client
1373                                  * sockets need to be blocking, otherwise certain
1374                                  * operations barf on FreeBSD.  Not a fatal error.
1375                                  */
1376                                 if (fcntl(ssock, F_SETFL, 0) < 0) {
1377                                         CtdlLogPrintf(CTDL_EMERG,
1378                                                       "citserver: Can't set socket to blocking: %s\n",
1379                                                       strerror(errno));
1380                                 }
1381
1382                                 /* New context will be created already
1383                                  * set up in the CON_EXECUTING state.
1384                                  */
1385                                 con = CreateNewContext();
1386                                 CT->Context = con;
1387
1388                                 /* Assign our new socket number to it. */
1389                                 con->client_socket = ssock;
1390                                 con->h_command_function = serviceptr->h_command_function;
1391                                 con->h_async_function = serviceptr->h_async_function;
1392                                 con->ServiceName = serviceptr->ServiceName;
1393                                 /* Determine whether it's a local socket */
1394                                 if (serviceptr->sockpath != NULL)
1395                                         con->is_local_socket = 1;
1396
1397                                 /* Set the SO_REUSEADDR socket option */
1398                                 i = 1;
1399                                 setsockopt(ssock, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i));
1400
1401                                 become_session(con);
1402                                 begin_session(con);
1403                                 serviceptr->h_greeting_function();
1404                                 become_session(NULL);
1405                                 con->state = CON_IDLE;
1406                                 break;
1407                         }
1408                 }
1409         }
1410
1411         CtdlThreadName(old_name);
1412 }
1413
1414 /*
1415  * Select on client socket.
1416  * First worker thread in here acquires the lock and builds an FDSET of client sockets.
1417  * then it selects on the client sockets timing out after 1 second.
1418  * If it times out the thread goes off to check on housekeeping etc.
1419  * If the select succeeds the thread goes off to handle the client request.
1420  * If the list of client connections is empty the threads all sleep for one second
1421  */
1422 struct CitContext *select_on_client(void)
1423 {
1424         fd_set readfds;
1425         struct timeval tv;
1426         int retval = 0;
1427         int highest=0;
1428         const char *old_name;
1429         
1430         
1431         old_name = CtdlThreadName("select_on_client");
1432         
1433         /* Initialise the fdset */
1434         FD_ZERO(&readfds);
1435         FD_SET(CT->Context->client_socket, &readfds);
1436         highest = CT->Context->client_socket;   
1437         /* Now we can select on any connections that are waiting */
1438         
1439         if (!CtdlThreadCheckStop())
1440         {
1441                 tv.tv_sec = config.c_sleeping;          /* wake up every second if no input */
1442                 tv.tv_usec = 0;
1443                 retval = select(highest + 1, &readfds, NULL, NULL, &tv);
1444         }
1445         else    /* Shutting down? */
1446         {
1447                 CtdlThreadName(old_name);
1448                 return(NULL);
1449         }
1450                 
1451
1452         /* Now figure out who made this select() unblock.
1453          * First, check for an error or exit condition.
1454          */
1455         if (retval < 0) {
1456                 if (errno == EBADF) {
1457                         CtdlLogPrintf(CTDL_NOTICE, "select() failed: (%s)\n",
1458                                 strerror(errno));
1459                 }
1460                 if (errno != EINTR) {
1461                         CtdlLogPrintf(CTDL_EMERG, "Exiting (%s)\n", strerror(errno));
1462                         CtdlThreadStopAll();
1463                 } else if (!CtdlThreadCheckStop()) {
1464                         CtdlLogPrintf(CTDL_DEBUG, "Un handled select failure.\n");
1465                 }
1466                 CtdlThreadName(old_name);
1467                 return NULL;
1468         }
1469         else if(retval == 0)
1470         {
1471                 CtdlThreadName(old_name);
1472                 CT->Context->kill_me = 1;
1473                 CT->Context = NULL;
1474                 return CT->Context;
1475         }
1476         
1477         CT->Context->state = CON_EXECUTING;
1478         CT->Context->input_waiting = 1;
1479         
1480         CtdlThreadName(old_name);
1481         return (CT->Context);
1482 }
1483
1484
1485
1486 /*
1487  * Do the worker threads work when needed
1488  */
1489 int execute_session(struct CitContext *bind_me)
1490 {
1491         int force_purge;
1492         
1493         become_session(bind_me);
1494
1495         /* If the client has sent a command, execute it. */
1496         if (CC->input_waiting) {
1497                 CC->h_command_function();
1498                 CC->input_waiting = 0;
1499         }
1500
1501         /* If there are asynchronous messages waiting and the
1502          * client supports it, do those now */
1503         if ((CC->is_async) && (CC->async_waiting)
1504            && (CC->h_async_function != NULL)) {
1505                 CC->h_async_function();
1506                 CC->async_waiting = 0;
1507         }
1508                 
1509         force_purge = CC->kill_me;
1510         if (force_purge)
1511                 CT->Context = NULL;
1512         become_session(NULL);
1513         bind_me->state = CON_IDLE;
1514         return force_purge;
1515 }
1516
1517
1518
1519 extern void dead_session_purge(int force);
1520
1521 /*
1522  * A new worker_thread loop.
1523  */
1524  
1525 void *new_worker_thread(void *arg)
1526 {
1527         struct CitContext *bind_me;
1528         int force_purge;
1529         
1530         while (!CtdlThreadCheckStop()) {
1531
1532                 /* make doubly sure we're not holding any stale db handles
1533                  * which might cause a deadlock.
1534                  */
1535                 cdb_check_handles();
1536                 force_purge = 0;
1537                 bind_me = NULL;         /* Which session shall we handle? */
1538                         
1539                 if (CT->Context == NULL)
1540                         select_on_master();
1541                 if (CtdlThreadCheckStop())
1542                         break;
1543                         
1544                 if (CT->Context)
1545                         bind_me = select_on_client();
1546                 if (CtdlThreadCheckStop())
1547                         break;
1548                         
1549                 if (bind_me)
1550                         force_purge = execute_session(bind_me);
1551                         
1552                 dead_session_purge(force_purge);
1553                 if (CtdlThreadCheckStop())
1554                         break;
1555                         
1556                 do_housekeeping();
1557         }
1558         return NULL;
1559 }