]> code.citadel.org Git - citadel.git/blob - citadel/threads.c
f2fb0e000177a99c32a30310310872c55fa89d41
[citadel.git] / citadel / threads.c
1 /*
2  * Thread handling stuff for Citadel server
3  *
4  * Copyright (c) 1987-2011 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <syslog.h>
31
32 #include "sysdep.h"
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #ifdef HAVE_SYSCALL_H
45 # include <syscall.h>
46 #else 
47 # if HAVE_SYS_SYSCALL_H
48 #  include <sys/syscall.h>
49 # endif
50 #endif
51
52 #include <libcitadel.h>
53
54 #include "threads.h"
55 #include "ctdl_module.h"
56 #include "modules_init.h"
57 #include "housekeeping.h"
58 #include "config.h"
59 #include "citserver.h"
60 #include "sysdep_decls.h"
61 #include "context.h"
62
63
64 /*
65  * To create a thread you must call one of the create thread functions.
66  * You must pass it the address of (a pointer to a CtdlThreadNode initialised to NULL) like this
67  * struct CtdlThreadNode *node = NULL;
68  * pass in &node
69  * If the thread is created *node will point to the thread control structure for the created thread.
70  * If the thread creation fails *node remains NULL
71  * Do not free the memory pointed to by *node, it doesn't belong to you.
72  * This new interface duplicates much of the eCrash stuff. We should go for closer integration since that would
73  * remove the need for the calls to eCrashRegisterThread and friends
74  */
75
76 static int num_threads = 0;                     /* Current number of threads */
77 static int num_workers = 0;                     /* Current number of worker threads */
78
79 CtdlThreadNode *CtdlThreadList = NULL;
80 CtdlThreadNode *CtdlThreadSchedList = NULL;
81
82 static CtdlThreadNode *GC_thread = NULL;
83 static char *CtdlThreadStates[CTDL_THREAD_LAST_STATE];
84 double CtdlThreadLoadAvg = 0;
85 double CtdlThreadWorkerAvg = 0;
86 citthread_key_t ThreadKey;
87
88 citthread_mutex_t Critters[MAX_SEMAPHORES];     /* Things needing locking */
89
90
91
92 void InitialiseSemaphores(void)
93 {
94         int i;
95
96         /* Set up a bunch of semaphores to be used for critical sections */
97         for (i=0; i<MAX_SEMAPHORES; ++i) {
98                 citthread_mutex_init(&Critters[i], NULL);
99         }
100 }
101
102
103
104
105 /*
106  * Obtain a semaphore lock to begin a critical section.
107  * but only if no one else has one
108  */
109 int try_critical_section(int which_one)
110 {
111         /* For all types of critical sections except those listed here,
112          * ensure nobody ever tries to do a critical section within a
113          * transaction; this could lead to deadlock.
114          */
115         if (    (which_one != S_FLOORCACHE)
116                 && (which_one != S_RPLIST)
117         ) {
118                 cdb_check_handles();
119         }
120         return (citthread_mutex_trylock(&Critters[which_one]));
121 }
122
123
124 /*
125  * Obtain a semaphore lock to begin a critical section.
126  */
127 void begin_critical_section(int which_one)
128 {
129         /* syslog(LOG_DEBUG, "begin_critical_section(%d)\n", which_one); */
130
131         /* For all types of critical sections except those listed here,
132          * ensure nobody ever tries to do a critical section within a
133          * transaction; this could lead to deadlock.
134          */
135         if (    (which_one != S_FLOORCACHE)
136                 && (which_one != S_RPLIST)
137         ) {
138                 cdb_check_handles();
139         }
140         citthread_mutex_lock(&Critters[which_one]);
141 }
142
143 /*
144  * Release a semaphore lock to end a critical section.
145  */
146 void end_critical_section(int which_one)
147 {
148         citthread_mutex_unlock(&Critters[which_one]);
149 }
150
151
152 /*
153  * A function to destroy the TSD
154  */
155 static void ctdl_thread_internal_dest_tsd(void *arg)
156 {
157         if (arg != NULL) {
158                 check_handles(arg);
159                 free(arg);
160         }
161 }
162
163
164 /*
165  * A function to initialise the thread TSD
166  */
167 void ctdl_thread_internal_init_tsd(void)
168 {
169         int ret;
170         
171         if ((ret = citthread_key_create(&ThreadKey, ctdl_thread_internal_dest_tsd))) {
172                 syslog(LOG_EMERG, "citthread_key_create: %s\n", strerror(ret));
173                 exit(CTDLEXIT_DB);
174         }
175 }
176
177 /*
178  * Ensure that we have a key for thread-specific data. 
179  *
180  * This should be called immediately after startup by any thread 
181  * 
182  */
183 void CtdlThreadAllocTSD(void)
184 {
185         ThreadTSD *tsd;
186
187         if (citthread_getspecific(ThreadKey) != NULL)
188                 return;
189
190         tsd = malloc(sizeof(ThreadTSD));
191
192         tsd->tid = NULL;
193
194         memset(tsd->cursors, 0, sizeof tsd->cursors);
195         tsd->self = NULL;
196         
197         citthread_setspecific(ThreadKey, tsd);
198 }
199
200
201 void ctdl_thread_internal_free_tsd(void)
202 {
203         ctdl_thread_internal_dest_tsd(citthread_getspecific(ThreadKey));
204         citthread_setspecific(ThreadKey, NULL);
205 }
206
207
208 void ctdl_thread_internal_cleanup(void)
209 {
210         int i;
211         CtdlThreadNode *this_thread, *that_thread;
212         
213         for (i=0; i<CTDL_THREAD_LAST_STATE; i++)
214         {
215                 free (CtdlThreadStates[i]);
216         }
217         
218         /* Clean up the scheduled thread list */
219         this_thread = CtdlThreadSchedList;
220         while (this_thread)
221         {
222                 that_thread = this_thread;
223                 this_thread = this_thread->next;
224                 citthread_mutex_destroy(&that_thread->ThreadMutex);
225                 citthread_cond_destroy(&that_thread->ThreadCond);
226                 citthread_mutex_destroy(&that_thread->SleepMutex);
227                 citthread_cond_destroy(&that_thread->SleepCond);
228                 citthread_attr_destroy(&that_thread->attr);
229                 free(that_thread);
230         }
231         ctdl_thread_internal_free_tsd();
232 }
233
234 void ctdl_thread_internal_init(void)
235 {
236         CtdlThreadNode *this_thread;
237         int ret = 0;
238         
239         CtdlThreadStates[CTDL_THREAD_INVALID] = strdup ("Invalid Thread");
240         CtdlThreadStates[CTDL_THREAD_VALID] = strdup("Valid Thread");
241         CtdlThreadStates[CTDL_THREAD_CREATE] = strdup("Thread being Created");
242         CtdlThreadStates[CTDL_THREAD_CANCELLED] = strdup("Thread Cancelled");
243         CtdlThreadStates[CTDL_THREAD_EXITED] = strdup("Thread Exited");
244         CtdlThreadStates[CTDL_THREAD_STOPPING] = strdup("Thread Stopping");
245         CtdlThreadStates[CTDL_THREAD_STOP_REQ] = strdup("Thread Stop Requested");
246         CtdlThreadStates[CTDL_THREAD_SLEEPING] = strdup("Thread Sleeping");
247         CtdlThreadStates[CTDL_THREAD_RUNNING] = strdup("Thread Running");
248         CtdlThreadStates[CTDL_THREAD_BLOCKED] = strdup("Thread Blocked");
249         
250         /* Get ourself a thread entry */
251         this_thread = malloc(sizeof(CtdlThreadNode));
252         if (this_thread == NULL) {
253                 syslog(LOG_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
254                 return;
255         }
256         // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
257         memset (this_thread, 0, sizeof(CtdlThreadNode));
258         
259         citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
260         citthread_cond_init (&(this_thread->ThreadCond), NULL);
261         citthread_mutex_init (&(this_thread->SleepMutex), NULL);
262         citthread_cond_init (&(this_thread->SleepCond), NULL);
263         
264         /* We are garbage collector so create us as running */
265         this_thread->state = CTDL_THREAD_RUNNING;
266         
267         if ((ret = citthread_attr_init(&this_thread->attr))) {
268                 syslog(LOG_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
269                 free(this_thread);
270                 return;
271         }
272
273         this_thread->name = "Garbage Collection Thread";
274         
275         this_thread->tid = citthread_self();
276         GC_thread = this_thread;
277         CT = this_thread;
278         
279         num_threads++;  // Increase the count of threads in the system.
280
281         this_thread->next = CtdlThreadList;
282         CtdlThreadList = this_thread;
283         if (this_thread->next)
284                 this_thread->next->prev = this_thread;
285         /* Set up start times */
286         gettimeofday(&this_thread->start_time, NULL);           /* Time this thread started */
287         memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval));     /* Changed state so mark it. */
288 }
289
290
291 /*
292  * A function to chenge the state of a thread
293  */
294 void ctdl_thread_internal_change_state (CtdlThreadNode *this_thread, enum CtdlThreadState new_state)
295 {
296         /*
297          * Wether we change state or not we need update the load values
298          */
299         /* This mutex not needed here? */
300         citthread_mutex_lock(&this_thread->ThreadMutex); /* To prevent race condition of a sleeping thread */
301         if ((new_state == CTDL_THREAD_STOP_REQ) && (this_thread->state > CTDL_THREAD_STOP_REQ))
302                 this_thread->state = new_state;
303         if (((new_state == CTDL_THREAD_SLEEPING) || (new_state == CTDL_THREAD_BLOCKED)) && (this_thread->state == CTDL_THREAD_RUNNING))
304                 this_thread->state = new_state;
305         if ((new_state == CTDL_THREAD_RUNNING) && ((this_thread->state == CTDL_THREAD_SLEEPING) || (this_thread->state == CTDL_THREAD_BLOCKED)))
306                 this_thread->state = new_state;
307         citthread_mutex_unlock(&this_thread->ThreadMutex);
308 }
309
310
311 /*
312  * A function to tell all threads to exit
313  */
314 void CtdlThreadStopAll(void)
315 {
316         /* First run any registered shutdown hooks.  This probably doesn't belong here. */
317         PerformSessionHooks(EVT_SHUTDOWN);
318         
319         /* then close all tcp ports so nobody else can talk to us anymore. */
320         CtdlShutdownServiceHooks();
321         //FIXME: The signalling of the condition should not be in the critical_section
322         // We need to build a list of threads we are going to signal and then signal them afterwards
323         
324         CtdlThreadNode *this_thread;
325         
326         begin_critical_section(S_THREAD_LIST);
327         this_thread = CtdlThreadList;
328         // Ask the GC thread to stop first so everything knows we are shutting down.
329         GC_thread->state = CTDL_THREAD_STOP_REQ;
330         while(this_thread)
331         {
332                 if (!citthread_equal(this_thread->tid, GC_thread->tid))
333                         citthread_kill(this_thread->tid, SIGHUP);
334
335                 ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
336                 citthread_cond_signal(&this_thread->ThreadCond);
337                 citthread_cond_signal(&this_thread->SleepCond);
338                 this_thread->stop_ticker = time(NULL);
339                 syslog(LOG_DEBUG, "Thread system stopping thread \"%s\" (0x%08lx).\n",
340                         this_thread->name, this_thread->tid);
341                 this_thread = this_thread->next;
342         }
343         end_critical_section(S_THREAD_LIST);
344 }
345
346
347 /*
348  * A function to wake up all sleeping threads
349  */
350 void CtdlThreadWakeAll(void)
351 {
352         CtdlThreadNode *this_thread;
353         
354         syslog(LOG_DEBUG, "Thread system waking all threads.\n");
355         
356         begin_critical_section(S_THREAD_LIST);
357         this_thread = CtdlThreadList;
358         while(this_thread)
359         {
360                 if (!this_thread->thread_func)
361                 {
362                         citthread_cond_signal(&this_thread->ThreadCond);
363                         citthread_cond_signal(&this_thread->SleepCond);
364                 }
365                 this_thread = this_thread->next;
366         }
367         end_critical_section(S_THREAD_LIST);
368 }
369
370
371 /*
372  * A function to return the number of threads running in the system
373  */
374 int CtdlThreadGetCount(void)
375 {
376         return  num_threads;
377 }
378
379 int CtdlThreadGetWorkers(void)
380 {
381         return  num_workers;
382 }
383
384 double CtdlThreadGetWorkerAvg(void)
385 {
386         double ret;
387         
388         begin_critical_section(S_THREAD_LIST);
389         ret =  CtdlThreadWorkerAvg;
390         end_critical_section(S_THREAD_LIST);
391         return ret;
392 }
393
394 double CtdlThreadGetLoadAvg(void)
395 {
396         double load_avg[3] = {0.0, 0.0, 0.0};
397
398         int ret = 0;
399         int smp_num_cpus;
400
401         /* Borrowed this straight from procps */
402         smp_num_cpus = sysconf(_SC_NPROCESSORS_ONLN);
403         if(smp_num_cpus<1) smp_num_cpus=1; /* SPARC glibc is buggy */
404
405 #ifdef HAVE_GETLOADAVG
406         ret = getloadavg(load_avg, 3);
407 #endif
408         if (ret < 0)
409                 return 0;
410         return load_avg[0] / smp_num_cpus;
411 /*
412  * This old chunk of code return a value that indicated the load on citserver
413  * This value could easily reach 100 % even when citserver was doing very little and
414  * hence the machine has much more spare capacity.
415  * Because this value was used to determine if the machine was under heavy load conditions
416  * from other processes in the system then citserver could be strangled un-necesarily
417  * What we are actually trying to achieve is to strangle citserver if the machine is heavily loaded.
418  * So we have changed this.
419
420         begin_critical_section(S_THREAD_LIST);
421         ret =  CtdlThreadLoadAvg;
422         end_critical_section(S_THREAD_LIST);
423         return ret;
424 */
425 }
426
427
428
429
430 /*
431  * A function to rename a thread
432  * Returns a const char *
433  */
434 const char *CtdlThreadName(const char *name)
435 {
436         const char *old_name;
437         
438         if (!CT)
439         {
440                 syslog(LOG_WARNING, "Thread system WARNING. Attempt to CtdlThreadRename() a non thread. %s\n", name);
441                 return NULL;
442         }
443         old_name = CT->name;
444         if (name)
445                 CT->name = name;
446         return (old_name);
447 }       
448
449
450 /*
451  * A function to force a thread to exit
452  */
453 void CtdlThreadCancel(CtdlThreadNode *thread)
454 {
455         CtdlThreadNode *this_thread;
456         
457         if (!thread)
458                 this_thread = CT;
459         else
460                 this_thread = thread;
461         if (!this_thread)
462         {
463                 syslog(LOG_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() a non thread.\n");
464                 CtdlThreadStopAll();
465                 return;
466         }
467         
468         if (!this_thread->thread_func)
469         {
470                 syslog(LOG_EMERG, "Thread system PANIC. Attempt to CtdlThreadCancel() the garbage collector.\n");
471                 CtdlThreadStopAll();
472                 return;
473         }
474         
475         ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_CANCELLED);
476         citthread_cancel(this_thread->tid);
477 }
478
479
480 /*
481  * A function for a thread to check if it has been asked to stop
482  */
483 int CtdlThreadCheckStop(void)
484 {
485         int state;
486         
487         if (!CT)
488         {
489                 syslog(LOG_EMERG, "Thread system PANIC, CtdlThreadCheckStop() called by a non thread.\n");
490                 CtdlThreadStopAll();
491                 return -1;
492         }
493         
494         state = CT->state;
495
496         if (CT->signal)
497         {
498                 syslog(LOG_DEBUG, "Thread \"%s\" caught signal %d.\n", CT->name, CT->signal);
499                 if (CT->signal == SIGHUP)
500                         CT->state = CTDL_THREAD_STOP_REQ;
501                 CT->signal = 0;
502         }
503         if(state == CTDL_THREAD_STOP_REQ)
504         {
505                 CT->state = CTDL_THREAD_STOPPING;
506                 return -1;
507         }
508         else if((state < CTDL_THREAD_STOP_REQ) && (state > CTDL_THREAD_CREATE))
509         {
510                 return -1;
511         }
512         return 0;
513 }
514
515
516 /*
517  * A function to ask a thread to exit
518  * The thread must call CtdlThreadCheckStop() periodically to determine if it should exit
519  */
520 void CtdlThreadStop(CtdlThreadNode *thread)
521 {
522         CtdlThreadNode *this_thread;
523         
524         if (!thread)
525                 this_thread = CT;
526         else
527                 this_thread = thread;
528         if (!this_thread)
529                 return;
530         if (!(this_thread->thread_func))
531                 return;         // Don't stop garbage collector
532
533         if (!citthread_equal(this_thread->tid, GC_thread->tid))
534                 citthread_kill(this_thread->tid, SIGHUP);
535
536         ctdl_thread_internal_change_state (this_thread, CTDL_THREAD_STOP_REQ);
537         citthread_cond_signal(&this_thread->ThreadCond);
538         citthread_cond_signal(&this_thread->SleepCond);
539         this_thread->stop_ticker = time(NULL);
540 }
541
542 /*
543  * So we now have a sleep command that works with threads but it is in seconds
544  */
545 void CtdlThreadSleep(int secs)
546 {
547         struct timespec wake_time;
548         struct timeval time_now;
549         
550         
551         if (!CT)
552         {
553                 syslog(LOG_WARNING, "CtdlThreadSleep() called by something that is not a thread. Should we die?\n");
554                 return;
555         }
556         
557         memset (&wake_time, 0, sizeof(struct timespec));
558         gettimeofday(&time_now, NULL);
559         wake_time.tv_sec = time_now.tv_sec + secs;
560         wake_time.tv_nsec = time_now.tv_usec * 10;
561
562         ctdl_thread_internal_change_state (CT, CTDL_THREAD_SLEEPING);
563         
564         citthread_mutex_lock(&CT->ThreadMutex); /* Prevent something asking us to awaken before we've gone to sleep */
565         citthread_cond_timedwait(&CT->SleepCond, &CT->ThreadMutex, &wake_time);
566         citthread_mutex_unlock(&CT->ThreadMutex);
567         
568         ctdl_thread_internal_change_state (CT, CTDL_THREAD_RUNNING);
569 }
570
571
572 /*
573  * Routine to clean up our thread function on exit
574  */
575 static void ctdl_internal_thread_cleanup(void *arg)
576 {
577         /*
578          * In here we were called by the current thread because it is exiting
579          * NB. WE ARE THE CURRENT THREAD
580          */
581         if (CT)
582         {
583                 const char *name = CT->name;
584                 const pid_t tid = CT->tid;
585
586                 syslog(LOG_NOTICE, "Thread \"%s\" (0x%08lx) exited.\n", name, (unsigned long) tid);
587         }
588         else 
589         {
590                 syslog(LOG_NOTICE, "some ((unknown ? ? ?) Thread exited.\n");
591         }
592         
593         #ifdef HAVE_BACKTRACE
594 ///     eCrash_UnregisterThread();
595         #endif
596         
597         citthread_mutex_lock(&CT->ThreadMutex);
598         CT->state = CTDL_THREAD_EXITED; // needs to be last thing else house keeping will unlink us too early
599         citthread_mutex_unlock(&CT->ThreadMutex);
600 }
601
602 /*
603  * Garbage collection routine.
604  * Gets called by main() in a loop to clean up the thread list periodically.
605  */
606 void CtdlThreadGC (void)
607 {
608
609
610         return;
611         /* FIXME this is a big deal, but I think it's causing corruption */
612
613
614         CtdlThreadNode *this_thread, *that_thread;
615         int workers = 0, sys_workers;
616         int ret=0;
617
618         begin_critical_section(S_THREAD_LIST);
619         
620         /* Handle exiting of garbage collector thread */
621         if(num_threads == 1)
622                 CtdlThreadList->state = CTDL_THREAD_EXITED;
623         
624 #ifdef WITH_THREADLOG
625         syslog(LOG_DEBUG, "Thread system running garbage collection.\n");
626 #endif
627         /*
628          * Woke up to do garbage collection
629          */
630         this_thread = CtdlThreadList;
631         while(this_thread)
632         {
633                 that_thread = this_thread;
634                 this_thread = this_thread->next;
635                 
636                 if ((that_thread->state == CTDL_THREAD_STOP_REQ || that_thread->state == CTDL_THREAD_STOPPING)
637                         && (!citthread_equal(that_thread->tid, citthread_self())))
638                 {
639                         syslog(LOG_DEBUG, "Waiting for thread %s (0x%08lx) to exit.\n", that_thread->name, that_thread->tid);
640                         terminate_stuck_sessions();
641                 }
642                 else
643                 {
644                         /**
645                          * Catch the situation where a worker was asked to stop but couldn't and we are not
646                          * shutting down.
647                          */
648                         that_thread->stop_ticker = 0;
649                 }
650                 
651                 if (that_thread->stop_ticker + 5 == time(NULL))
652                 {
653                         syslog(LOG_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);
654                         if ((that_thread->flags & CTDLTHREAD_WORKER) == 0)
655                                 syslog(LOG_INFO, "Thread System: A non worker thread would have been canceled this may cause message loss.\n");
656 //                      that_thread->state = CTDL_THREAD_CANCELLED;
657                         that_thread->stop_ticker++;
658 //                      citthread_cancel(that_thread->tid);
659 //                      continue;
660                 }
661                 
662                 /* Do we need to clean up this thread? */
663                 if ((that_thread->state != CTDL_THREAD_EXITED) && (that_thread->state != CTDL_THREAD_CANCELLED))
664                 {
665                         if(that_thread->flags & CTDLTHREAD_WORKER)
666                                 workers++;      /* Sanity check on number of worker threads */
667                         continue;
668                 }
669                 
670                 if (citthread_equal(that_thread->tid, citthread_self()) && that_thread->thread_func)
671                 {       /* Sanity check */
672                         end_critical_section(S_THREAD_LIST);
673                         syslog(LOG_EMERG, "Thread system PANIC, a thread is trying to clean up after itself.\n");
674                         abort();
675                         return;
676                 }
677                 
678                 if (num_threads <= 0)
679                 {       /* Sanity check */
680                         end_critical_section(S_THREAD_LIST);
681                         syslog(LOG_EMERG, "Thread system PANIC, num_threads <= 0 and trying to do Garbage Collection.\n");
682                         abort();
683                         return;
684                 }
685
686                 if(that_thread->flags & CTDLTHREAD_WORKER)
687                         num_workers--;  /* This is a wroker thread so reduce the count. */
688                 num_threads--;
689                 /* If we are unlinking the list head then the next becomes the list head */
690                 if(that_thread->prev)
691                         that_thread->prev->next = that_thread->next;
692                 else
693                         CtdlThreadList = that_thread->next;
694                 if(that_thread->next)
695                         that_thread->next->prev = that_thread->prev;
696                 
697                 citthread_cond_signal(&that_thread->ThreadCond);
698                 citthread_cond_signal(&that_thread->SleepCond); // Make sure this thread is awake
699                 citthread_mutex_lock(&that_thread->ThreadMutex);        // Make sure it has done what its doing
700                 citthread_mutex_unlock(&that_thread->ThreadMutex);
701                 /*
702                  * Join on the thread to do clean up and prevent memory leaks
703                  * Also makes sure the thread has cleaned up after itself before we remove it from the list
704                  * We can join on the garbage collector thread the join should just return EDEADLCK
705                  */
706                 ret = citthread_join (that_thread->tid, NULL);
707                 if (ret == EDEADLK)
708                         syslog(LOG_DEBUG, "Garbage collection on own thread.\n");
709                 else if (ret == EINVAL)
710                         syslog(LOG_DEBUG, "Garbage collection, that thread already joined on.\n");
711                 else if (ret == ESRCH)
712                         syslog(LOG_DEBUG, "Garbage collection, no thread to join on.\n");
713                 else if (ret != 0)
714                         syslog(LOG_DEBUG, "Garbage collection, citthread_join returned an unknown error(%d).\n", ret);
715                 /*
716                  * Now we own that thread entry
717                  */
718                 syslog(LOG_INFO, "Garbage Collection for thread \"%s\" (0x%08lx).\n",
719                         that_thread->name, that_thread->tid);
720                 citthread_mutex_destroy(&that_thread->ThreadMutex);
721                 citthread_cond_destroy(&that_thread->ThreadCond);
722                 citthread_mutex_destroy(&that_thread->SleepMutex);
723                 citthread_cond_destroy(&that_thread->SleepCond);
724                 citthread_attr_destroy(&that_thread->attr);
725                 free(that_thread);
726         }
727         sys_workers = num_workers;
728         end_critical_section(S_THREAD_LIST);
729         
730         /* Sanity check number of worker threads */
731         if (workers != sys_workers)
732         {
733                 syslog(LOG_EMERG,
734                         "Thread system PANIC, discrepancy in number of worker threads. Counted %d, should be %d.\n",
735                         workers, sys_workers
736                         );
737                 abort();
738         }
739 }
740
741
742
743  
744 /*
745  * Runtime function for a Citadel Thread.
746  * This initialises the threads environment and then calls the user supplied thread function
747  * Note that this is the REAL thread function and wraps the users thread function.
748  */ 
749 static void *ctdl_internal_thread_func (void *arg)
750 {
751         CtdlThreadNode *this_thread;
752         void *ret = NULL;
753
754         /* lock and unlock the thread list.
755          * This causes this thread to wait until all its creation stuff has finished before it
756          * can continue its execution.
757          */
758         begin_critical_section(S_THREAD_LIST);
759         this_thread = (CtdlThreadNode *) arg;
760         gettimeofday(&this_thread->start_time, NULL);           /* Time this thread started */
761         
762         // Register the cleanup function to take care of when we exit.
763         citthread_cleanup_push(ctdl_internal_thread_cleanup, NULL);
764         // Get our thread data structure
765         CtdlThreadAllocTSD();
766         CT = this_thread;
767         this_thread->pid = getpid();
768         memcpy(&this_thread->last_state_change, &this_thread->start_time, sizeof (struct timeval));     /* Changed state so mark it. */
769         /* Only change to running state if we weren't asked to stop during the create cycle
770          * Other wise there is a window to allow this threads creation to continue to full grown and
771          * therby prevent a shutdown of the server.
772          */
773         if (!CtdlThreadCheckStop())
774         {
775                 citthread_mutex_lock(&this_thread->ThreadMutex);
776                 this_thread->state = CTDL_THREAD_RUNNING;
777                 citthread_mutex_unlock(&this_thread->ThreadMutex);
778         }
779         end_critical_section(S_THREAD_LIST);
780         
781         // Register for tracing
782         #ifdef HAVE_BACKTRACE
783 ///     eCrash_RegisterThread(this_thread->name, 0);
784         #endif
785         
786         // Tell the world we are here
787 #if defined(HAVE_SYSCALL_H) && defined (SYS_gettid)
788         this_thread->reltid = syscall(SYS_gettid);
789 #endif
790         syslog(LOG_NOTICE, "Created a new thread \"%s\" (0x%08lx).\n",
791                 this_thread->name, this_thread->tid);
792         
793         /*
794          * run the thread to do the work but only if we haven't been asked to stop
795          */
796         if (!CtdlThreadCheckStop())
797                 ret = (this_thread->thread_func)(this_thread->user_args);
798         
799         /*
800          * Our thread is exiting either because it wanted to end or because the server is stopping
801          * We need to clean up
802          */
803         citthread_cleanup_pop(1);       // Execute our cleanup routine and remove it
804         
805         return(ret);
806 }
807
808
809
810
811 /*
812  * Function to initialise an empty thread structure
813  */
814 CtdlThreadNode *ctdl_internal_init_thread_struct(CtdlThreadNode *this_thread, long flags)
815 {
816         int ret = 0;
817         
818         // Ensuring this is zero'd means we make sure the thread doesn't start doing its thing until we are ready.
819         memset (this_thread, 0, sizeof(CtdlThreadNode));
820         
821         /* Create the mutex's early so we can use them */
822         citthread_mutex_init (&(this_thread->ThreadMutex), NULL);
823         citthread_cond_init (&(this_thread->ThreadCond), NULL);
824         citthread_mutex_init (&(this_thread->SleepMutex), NULL);
825         citthread_cond_init (&(this_thread->SleepCond), NULL);
826         
827         this_thread->state = CTDL_THREAD_CREATE;
828         
829         if ((ret = citthread_attr_init(&this_thread->attr))) {
830                 citthread_mutex_unlock(&this_thread->ThreadMutex);
831                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
832                 citthread_cond_destroy(&(this_thread->ThreadCond));
833                 citthread_mutex_destroy(&(this_thread->SleepMutex));
834                 citthread_cond_destroy(&(this_thread->SleepCond));
835                 syslog(LOG_EMERG, "Thread system, citthread_attr_init: %s\n", strerror(ret));
836                 free(this_thread);
837                 return NULL;
838         }
839
840         /* Our per-thread stacks need to be bigger than the default size,
841          * otherwise the MIME parser crashes on FreeBSD, and the IMAP service
842          * crashes on 64-bit Linux.
843          */
844         if (flags & CTDLTHREAD_BIGSTACK)
845         {
846 #ifdef WITH_THREADLOG
847                 syslog(LOG_INFO, "Thread system. Creating BIG STACK thread.\n");
848 #endif
849                 if ((ret = citthread_attr_setstacksize(&this_thread->attr, THREADSTACKSIZE))) {
850                         citthread_mutex_unlock(&this_thread->ThreadMutex);
851                         citthread_mutex_destroy(&(this_thread->ThreadMutex));
852                         citthread_cond_destroy(&(this_thread->ThreadCond));
853                         citthread_mutex_destroy(&(this_thread->SleepMutex));
854                         citthread_cond_destroy(&(this_thread->SleepCond));
855                         citthread_attr_destroy(&this_thread->attr);
856                         syslog(LOG_EMERG, "Thread system, citthread_attr_setstacksize: %s\n",
857                                 strerror(ret));
858                         free(this_thread);
859                         return NULL;
860                 }
861         }
862
863         /* Set this new thread with an avg_blocked of 2. We do this so that its creation affects the
864          * load average for the system. If we don't do this then we create a mass of threads at the same time 
865          * because the creation didn't affect the load average.
866          */
867         this_thread->avg_blocked = 2;
868         
869         return (this_thread);
870 }
871
872
873
874  
875 /*
876  * Internal function to create a thread.
877  */ 
878 CtdlThreadNode *ctdl_internal_create_thread(char *name, long flags, void *(*thread_func) (void *arg), void *args)
879 {
880         int ret = 0;
881         CtdlThreadNode *this_thread;
882
883         if (num_threads >= 32767)
884         {
885                 syslog(LOG_EMERG, "Thread system. Thread list full.\n");
886                 return NULL;
887         }
888                 
889         this_thread = malloc(sizeof(CtdlThreadNode));
890         if (this_thread == NULL) {
891                 syslog(LOG_EMERG, "Thread system, can't allocate CtdlThreadNode, exiting\n");
892                 return NULL;
893         }
894         
895         /* Initialise the thread structure */
896         if (ctdl_internal_init_thread_struct(this_thread, flags) == NULL)
897         {
898                 free(this_thread);
899                 syslog(LOG_EMERG, "Thread system, can't initialise CtdlThreadNode, exiting\n");
900                 return NULL;
901         }
902         /*
903          * If we got here we are going to create the thread so we must initilise the structure
904          * first because most implimentations of threading can't create it in a stopped state
905          * and it might want to do things with its structure that aren't initialised otherwise.
906          */
907         if(name)
908         {
909                 this_thread->name = name;
910         }
911         else
912         {
913                 this_thread->name = "Un-named Thread";
914         }
915         
916         this_thread->flags = flags;
917         this_thread->thread_func = thread_func;
918         this_thread->user_args = args;
919         
920         begin_critical_section(S_THREAD_LIST);
921         /*
922          * We pass this_thread into the thread as its args so that it can find out information
923          * about itself and it has a bit of storage space for itself, not to mention that the REAL
924          * thread function needs to finish off the setup of the structure
925          */
926         if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
927         {
928                 end_critical_section(S_THREAD_LIST);
929                 syslog(LOG_ALERT, "Thread system, Can't create thread: %s\n",
930                         strerror(ret));
931                 citthread_mutex_unlock(&this_thread->ThreadMutex);
932                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
933                 citthread_cond_destroy(&(this_thread->ThreadCond));
934                 citthread_mutex_destroy(&(this_thread->SleepMutex));
935                 citthread_cond_destroy(&(this_thread->SleepCond));
936                 citthread_attr_destroy(&this_thread->attr);
937                 free(this_thread);
938                 return NULL;
939         }
940         num_threads++;  // Increase the count of threads in the system.
941         if(this_thread->flags & CTDLTHREAD_WORKER)
942                 num_workers++;
943
944         this_thread->next = CtdlThreadList;
945         CtdlThreadList = this_thread;
946         if (this_thread->next)
947                 this_thread->next->prev = this_thread;
948         
949         end_critical_section(S_THREAD_LIST);
950         
951         return this_thread;
952 }
953
954 /*
955  * Wrapper function to create a thread
956  * ensures the critical section and other protections are in place.
957  * char *name = name to give to thread, if NULL, use generic name
958  * int flags = flags to determine type of thread and standard facilities
959  */
960 CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args)
961 {
962         CtdlThreadNode *ret = NULL;
963         
964         ret = ctdl_internal_create_thread(name, flags, thread_func, args);
965         return ret;
966 }
967
968
969
970 CtdlThreadNode *ctdl_thread_internal_start_scheduled (CtdlThreadNode *this_thread)
971 {
972         int ret = 0;
973         
974         begin_critical_section(S_THREAD_LIST);
975         /*
976          * We pass this_thread into the thread as its args so that it can find out information
977          * about itself and it has a bit of storage space for itself, not to mention that the REAL
978          * thread function needs to finish off the setup of the structure
979          */
980         if ((ret = citthread_create(&this_thread->tid, &this_thread->attr, ctdl_internal_thread_func, this_thread) != 0))
981         {
982                 end_critical_section(S_THREAD_LIST);
983                 syslog(LOG_DEBUG, "Failed to start scheduled thread \"%s\": %s\n", this_thread->name, strerror(ret));
984                 citthread_mutex_destroy(&(this_thread->ThreadMutex));
985                 citthread_cond_destroy(&(this_thread->ThreadCond));
986                 citthread_mutex_destroy(&(this_thread->SleepMutex));
987                 citthread_cond_destroy(&(this_thread->SleepCond));
988                 citthread_attr_destroy(&this_thread->attr);
989                 free(this_thread);
990                 return NULL;
991         }
992         
993         
994         num_threads++;  // Increase the count of threads in the system.
995         if(this_thread->flags & CTDLTHREAD_WORKER)
996                 num_workers++;
997
998         this_thread->next = CtdlThreadList;
999         CtdlThreadList = this_thread;
1000         if (this_thread->next)
1001                 this_thread->next->prev = this_thread;
1002         
1003         end_critical_section(S_THREAD_LIST);
1004         
1005         
1006         return this_thread;
1007 }
1008
1009
1010
1011 void ctdl_thread_internal_check_scheduled(void)
1012 {
1013         CtdlThreadNode *this_thread, *that_thread;
1014         time_t now;
1015         
1016         /* Don't start scheduled threads if the system wants single user mode */
1017         if (CtdlWantSingleUser())
1018                 return;
1019         
1020         if (try_critical_section(S_SCHEDULE_LIST))
1021                 return; /* If this list is locked we wait till the next chance */
1022         
1023         now = time(NULL);
1024         
1025 #ifdef WITH_THREADLOG
1026         syslog(LOG_DEBUG, "Checking for scheduled threads to start.\n");
1027 #endif
1028
1029         this_thread = CtdlThreadSchedList;
1030         while(this_thread)
1031         {
1032                 that_thread = this_thread;
1033                 this_thread = this_thread->next;
1034                 
1035                 if (now > that_thread->when)
1036                 {
1037                         /* Unlink from schedule list */
1038                         if (that_thread->prev)
1039                                 that_thread->prev->next = that_thread->next;
1040                         else
1041                                 CtdlThreadSchedList = that_thread->next;
1042                         if (that_thread->next)
1043                                 that_thread->next->prev = that_thread->prev;
1044                                 
1045                         that_thread->next = that_thread->prev = NULL;
1046 #ifdef WITH_THREADLOG
1047                         syslog(LOG_DEBUG, "About to start scheduled thread \"%s\".\n", that_thread->name);
1048 #endif
1049                         if (CT->state > CTDL_THREAD_STOP_REQ)
1050                         {       /* Only start it if the system is not stopping */
1051                                 if (ctdl_thread_internal_start_scheduled (that_thread))
1052                                 {
1053 #ifdef WITH_THREADLOG
1054                                         syslog(LOG_INFO, "Thread system, Started a scheduled thread \"%s\" (0x%08lx).\n",
1055                                                 that_thread->name, that_thread->tid);
1056 #endif
1057                                 }
1058                         }
1059                 }
1060 #ifdef WITH_THREADLOG
1061                 else
1062                 {
1063                         syslog(LOG_DEBUG, "Thread \"%s\" will start in %ld seconds.\n",
1064                                 that_thread->name, that_thread->when - time(NULL));
1065                 }
1066 #endif
1067         }
1068         end_critical_section(S_SCHEDULE_LIST);
1069 }
1070
1071
1072 /*
1073  * A warapper function for select so we can show a thread as blocked
1074  */
1075 int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
1076 {
1077         int ret = 0;
1078         
1079         ctdl_thread_internal_change_state(CT, CTDL_THREAD_BLOCKED);
1080         if (!CtdlThreadCheckStop())
1081                 ret = select(n, readfds, writefds, exceptfds, timeout);
1082         /**
1083          * If the select returned <= 0 then it failed due to an error
1084          * or timeout so this thread could stop if asked to do so.
1085          * Anything else means it needs to continue unless the system is shutting down
1086          */
1087         if (ret > 0)
1088         {
1089                 /**
1090                  * The select says this thread needs to do something useful.
1091                  * This thread was in an idle state so it may have been asked to stop
1092                  * but if the system isn't shutting down this thread is no longer
1093                  * idle and select has given it a task to do so it must not stop
1094                  * In this condition we need to force it into the running state.
1095                  * CtdlThreadGC will clear its ticker for us.
1096                  *
1097                  * FIXME: there is still a small hole here. It is possible for the sequence of locking
1098                  * to allow the state to get changed to STOP_REQ just after this code if the other thread
1099                  * has decided to change the state before this lock, it there fore has to wait till the lock
1100                  * completes but it will continue to change the state. We need something a bit better here.
1101                  */
1102                 citthread_mutex_lock(&CT->ThreadMutex); /* To prevent race condition of a sleeping thread */
1103                 if (GC_thread->state > CTDL_THREAD_STOP_REQ && CT->state <= CTDL_THREAD_STOP_REQ)
1104                 {
1105                         syslog(LOG_DEBUG, "Thread %s (0x%08lx) refused stop request.\n", CT->name, CT->tid);
1106                         CT->state = CTDL_THREAD_RUNNING;
1107                 }
1108                 citthread_mutex_unlock(&CT->ThreadMutex);
1109         }
1110
1111         ctdl_thread_internal_change_state(CT, CTDL_THREAD_RUNNING);
1112
1113         return ret;
1114 }
1115
1116
1117
1118 void *new_worker_thread(void *arg);
1119 extern void close_masters (void);
1120
1121
1122
1123 void go_threading(void)
1124 {
1125         int i;
1126         CtdlThreadNode *last_worker;
1127
1128         /*
1129          * Initialise the thread system
1130          */
1131         ctdl_thread_internal_init();
1132
1133         /* Second call to module init functions now that threading is up */
1134         initialise_modules(1);
1135         CtdlThreadCreate("select_on_master", CTDLTHREAD_BIGSTACK, select_on_master, NULL);
1136
1137         /*
1138          * This thread is now used for garbage collection of other threads in the thread list
1139          */
1140         syslog(LOG_INFO, "Startup thread %ld becoming garbage collector,\n", (long) citthread_self());
1141
1142         /*
1143          * We do a lot of locking and unlocking of the thread list in here.
1144          * We do this so that we can repeatedly release time for other threads
1145          * that may be waiting on the thread list.
1146          * We are a low priority thread so we can afford to do this
1147          */
1148         
1149         while (CtdlThreadGetCount())
1150         {
1151                 if (CT->signal)
1152                         exit_signal = CT->signal;
1153                 if (exit_signal)
1154                 {
1155                         CtdlThreadStopAll();
1156                 }
1157                 check_sched_shutdown();
1158                 if (CT->state > CTDL_THREAD_STOP_REQ)
1159                 {
1160                         begin_critical_section(S_THREAD_LIST);
1161                         end_critical_section(S_THREAD_LIST);
1162                         
1163                         ctdl_thread_internal_check_scheduled(); /* start scheduled threads */
1164                 }
1165                 
1166                 /* Reduce the size of the worker thread pool if necessary. */
1167                 if ((CtdlThreadGetWorkers() > config.c_min_workers + 1) && (CtdlThreadWorkerAvg < 20) && (CT->state > CTDL_THREAD_STOP_REQ))
1168                 {
1169                         /* Ask a worker thread to stop as we no longer need it */
1170                         begin_critical_section(S_THREAD_LIST);
1171                         last_worker = CtdlThreadList;
1172                         while (last_worker)
1173                         {
1174                                 citthread_mutex_lock(&last_worker->ThreadMutex);
1175                                 if (last_worker->flags & CTDLTHREAD_WORKER && (last_worker->state > CTDL_THREAD_STOPPING) && (last_worker->Context == NULL))
1176                                 {
1177                                         citthread_mutex_unlock(&last_worker->ThreadMutex);
1178                                         break;
1179                                 }
1180                                 citthread_mutex_unlock(&last_worker->ThreadMutex);
1181                                 last_worker = last_worker->next;
1182                         }
1183                         end_critical_section(S_THREAD_LIST);
1184                         if (last_worker)
1185                         {
1186 #ifdef WITH_THREADLOG
1187                                 syslog(LOG_DEBUG, "Thread system, stopping excess worker thread \"%s\" (0x%08lx).\n",
1188                                         last_worker->name,
1189                                         last_worker->tid
1190                                         );
1191 #endif
1192                                 CtdlThreadStop(last_worker);
1193                         }
1194                 }
1195         
1196                 /*
1197                  * If all our workers are working hard, start some more to help out
1198                  * with things
1199                  */
1200                 /* FIXME: come up with a better way to dynamically alter the number of threads
1201                  * based on the system load
1202                  */
1203                 if (    (((CtdlThreadGetWorkers() < config.c_max_workers)
1204                         && (CtdlThreadGetWorkerAvg() > 60))
1205                         || CtdlThreadGetWorkers() < config.c_min_workers)
1206                         && (CT->state > CTDL_THREAD_STOP_REQ)
1207                 )
1208                 {
1209                         /* Only start new threads if we are not going to overload the machine */
1210                         /* Temporarily set to 10 should be enough to make sure we don't stranglew the server
1211                          * at least until we make this a config option */
1212                         if (CtdlThreadGetLoadAvg() < ((double)10.00)) {
1213                                 for (i=0; i<5 ; i++) {
1214                                         CtdlThreadCreate("Worker Thread",
1215                                                 CTDLTHREAD_BIGSTACK + CTDLTHREAD_WORKER,
1216                                                 worker_thread,
1217                                                 NULL
1218                                                 );
1219                                 }
1220                         }
1221                         else
1222                                 syslog(LOG_WARNING, "Server strangled due to machine load average too high.\n");
1223                 }
1224
1225                 CtdlThreadGC();
1226
1227                 if (CtdlThreadGetCount() <= 1) // Shutting down clean up the garbage collector
1228                 {
1229                         CtdlThreadGC();
1230                 }
1231                 
1232 #ifdef THREADS_USESIGNALS
1233                 if (CtdlThreadGetCount() && CT->state > CTDL_THREAD_STOP_REQ)
1234 #else
1235                 if (CtdlThreadGetCount())
1236 #endif
1237                         CtdlThreadSleep(1);
1238         }
1239         /*
1240          * If the above loop exits we must be shutting down since we obviously have no threads
1241          */
1242         ctdl_thread_internal_cleanup();
1243 }
1244
1245
1246
1247