Back to index

citadel  8.12
threads.c
Go to the documentation of this file.
00001 /*
00002  * Thread handling stuff for Citadel server
00003  *
00004  * Copyright (c) 1987-2011 by the citadel.org team
00005  *
00006  * This program is open source software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 3 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00019  */
00020 
00021 #include <stdlib.h>
00022 #include <unistd.h>
00023 #include <stdio.h>
00024 #include <sys/types.h>
00025 #include <errno.h>
00026 #include <sys/socket.h>
00027 #include <unistd.h>
00028 #include <fcntl.h>
00029 #include <signal.h>
00030 #include <syslog.h>
00031 
00032 #include "sysdep.h"
00033 #if TIME_WITH_SYS_TIME
00034 # include <sys/time.h>
00035 # include <time.h>
00036 #else
00037 # if HAVE_SYS_TIME_H
00038 #  include <sys/time.h>
00039 # else
00040 #  include <time.h>
00041 # endif
00042 #endif
00043 
00044 #ifdef HAVE_SYSCALL_H
00045 # include <syscall.h>
00046 #else 
00047 # if HAVE_SYS_SYSCALL_H
00048 #  include <sys/syscall.h>
00049 # endif
00050 #endif
00051 
00052 #include <libcitadel.h>
00053 
00054 #include "threads.h"
00055 #include "ctdl_module.h"
00056 #include "modules_init.h"
00057 #include "housekeeping.h"
00058 #include "config.h"
00059 #include "citserver.h"
00060 #include "sysdep_decls.h"
00061 #include "context.h"
00062 #include "event_client.h"
00063 
00064 
00065 int num_workers = 0;                      /* Current number of worker threads */
00066 int active_workers = 0;                          /* Number of ACTIVE worker threads */
00067 pthread_key_t ThreadKey;
00068 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
00069 struct thread_tsd masterTSD;
00070 int server_shutting_down = 0;                    /* set to nonzero during shutdown */
00071 
00072 
00073 
00074 void InitializeSemaphores(void)
00075 {
00076        int i;
00077 
00078        /* Set up a bunch of semaphores to be used for critical sections */
00079        for (i=0; i<MAX_SEMAPHORES; ++i) {
00080               pthread_mutex_init(&Critters[i], NULL);
00081        }
00082 }
00083 
00084 
00085 
00086 
00087 /*
00088  * Obtain a semaphore lock to begin a critical section.
00089  * but only if no one else has one
00090  */
00091 int try_critical_section(int which_one)
00092 {
00093        /* For all types of critical sections except those listed here,
00094         * ensure nobody ever tries to do a critical section within a
00095         * transaction; this could lead to deadlock.
00096         */
00097        if (   (which_one != S_FLOORCACHE)
00098               && (which_one != S_RPLIST)
00099        ) {
00100               cdb_check_handles();
00101        }
00102        return (pthread_mutex_trylock(&Critters[which_one]));
00103 }
00104 
00105 
00106 /*
00107  * Obtain a semaphore lock to begin a critical section.
00108  */
00109 void begin_critical_section(int which_one)
00110 {
00111        /* For all types of critical sections except those listed here,
00112         * ensure nobody ever tries to do a critical section within a
00113         * transaction; this could lead to deadlock.
00114         */
00115        if (   (which_one != S_FLOORCACHE)
00116               && (which_one != S_RPLIST)
00117        ) {
00118               cdb_check_handles();
00119        }
00120        pthread_mutex_lock(&Critters[which_one]);
00121 }
00122 
00123 /*
00124  * Release a semaphore lock to end a critical section.
00125  */
00126 void end_critical_section(int which_one)
00127 {
00128        pthread_mutex_unlock(&Critters[which_one]);
00129 }
00130 
00131 
00132 
00133 
00134 /*
00135  * Return a pointer to our thread-specific (not session-specific) data.
00136  */ 
00137 struct thread_tsd *MyThread(void) {
00138         register struct thread_tsd *c;
00139         return ((c = (struct thread_tsd *) pthread_getspecific(ThreadKey), c == NULL) ? &masterTSD : c);
00140 }
00141 
00142 
00143 
00144 /* 
00145  * Called by CtdlThreadCreate()
00146  * We have to pass through here before starting our thread in order to create a set of data
00147  * that is thread-specific rather than session-specific.
00148  */
00149 void *CTC_backend(void *supplied_start_routine)
00150 {
00151        struct thread_tsd *mytsd;
00152        void *(*start_routine)(void*) = supplied_start_routine;
00153 
00154        mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd));
00155        memset(mytsd, 0, sizeof(struct thread_tsd));
00156        pthread_setspecific(ThreadKey, (const void *) mytsd);
00157 
00158        start_routine(NULL);
00159 
00160        free(mytsd);
00161        return(NULL);
00162 }
00163 
00164  
00165 /*
00166  * Function to create a thread.
00167  */ 
00168 void CtdlThreadCreate(void *(*start_routine)(void*))
00169 {
00170        pthread_t thread;
00171        pthread_attr_t attr;
00172        int ret = 0;
00173 
00174 
00175        ret = pthread_attr_init(&attr);
00176        ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
00177        ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine);
00178        if (ret != 0) syslog(LOG_EMERG, "pthread_create() : %s", strerror(errno));
00179 }
00180 
00181 
00182 void InitializeMasterTSD(void) {
00183        memset(&masterTSD, 0, sizeof(struct thread_tsd));
00184 }
00185 
00186 extern void ShutDownEventQueues(void);
00187 
00188 int EventQShuttingDown = 0;
00189 int EVQShutDown = 0;
00190 /*
00191  * Initialize the thread system
00192  */
00193 void go_threading(void)
00194 {
00195        if (pthread_key_create(&ThreadKey, NULL) != 0) {
00196               syslog(LOG_EMERG, "pthread_key_create() : %s", strerror(errno));
00197               abort();
00198        }
00199 
00200        /* Second call to module init functions now that threading is up */
00201        initialise_modules(1);
00202 
00203        /* Begin with one worker thread.  We will expand the pool if necessary */
00204        CtdlThreadCreate(worker_thread);
00205 
00206        /* The supervisor thread monitors worker threads and spawns more of them if it finds that
00207         * they are all in use.  FIXME make the 256 max threads a configurable value.
00208         */
00209        while (!server_shutting_down) {
00210               if ((active_workers == num_workers) && (num_workers < 256)) {
00211                      CtdlThreadCreate(worker_thread);
00212               }
00213               usleep(1000);
00214        }
00215 
00216        /* When we get to this point we are getting ready to shut down our Citadel server */
00217        if (!EventQShuttingDown)
00218        {
00219               EventQShuttingDown = 1;
00220               ShutDownEventQueues();
00221        }
00222        while (!EVQShutDown)
00223               usleep(1000);
00224 
00225 
00226        terminate_all_sessions();          /* close all client sockets */
00227        CtdlShutdownServiceHooks();        /* close all listener sockets to prevent new connections */
00228        PerformSessionHooks(EVT_SHUTDOWN); /* run any registered shutdown hooks */
00229 
00230        int countdown = 30;
00231        while ( (num_workers > 0) && (countdown-- > 0)) {
00232               syslog(LOG_DEBUG, "Waiting %d seconds for %d worker threads to exit",
00233                      countdown, num_workers
00234               );
00235               usleep(1000);
00236        }
00237 }