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