2 * Thread handling stuff for Citadel server
4 * Copyright (c) 1987-2011 by the citadel.org team
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, version 3.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
18 #include <sys/types.h>
20 #include <sys/socket.h>
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
32 # include <sys/time.h>
41 # if HAVE_SYS_SYSCALL_H
42 # include <sys/syscall.h>
46 #include <libcitadel.h>
49 #include "ctdl_module.h"
50 #include "modules_init.h"
51 #include "housekeeping.h"
53 #include "citserver.h"
54 #include "sysdep_decls.h"
56 #include "event_client.h"
59 int num_workers = 0; /* Current number of worker threads */
60 int active_workers = 0; /* Number of ACTIVE worker threads */
61 pthread_key_t ThreadKey;
62 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
63 struct thread_tsd masterTSD;
64 int server_shutting_down = 0; /* set to nonzero during shutdown */
66 pthread_mutex_t ThreadCountMutex;;
69 void InitializeSemaphores(void)
73 /* Set up a bunch of semaphores to be used for critical sections */
74 for (i=0; i<MAX_SEMAPHORES; ++i) {
75 pthread_mutex_init(&Critters[i], NULL);
83 * Obtain a semaphore lock to begin a critical section.
84 * but only if no one else has one
86 int try_critical_section(int which_one)
88 /* For all types of critical sections except those listed here,
89 * ensure nobody ever tries to do a critical section within a
90 * transaction; this could lead to deadlock.
92 if ( (which_one != S_FLOORCACHE)
93 && (which_one != S_RPLIST)
97 return (pthread_mutex_trylock(&Critters[which_one]));
102 * Obtain a semaphore lock to begin a critical section.
104 void begin_critical_section(int which_one)
106 /* For all types of critical sections except those listed here,
107 * ensure nobody ever tries to do a critical section within a
108 * transaction; this could lead to deadlock.
110 if ( (which_one != S_FLOORCACHE)
111 && (which_one != S_RPLIST)
115 pthread_mutex_lock(&Critters[which_one]);
119 * Release a semaphore lock to end a critical section.
121 void end_critical_section(int which_one)
123 pthread_mutex_unlock(&Critters[which_one]);
130 * Return a pointer to our thread-specific (not session-specific) data.
132 struct thread_tsd *MyThread(void) {
133 register struct thread_tsd *c;
134 return ((c = (struct thread_tsd *) pthread_getspecific(ThreadKey), c == NULL) ? &masterTSD : c);
140 * Called by CtdlThreadCreate()
141 * We have to pass through here before starting our thread in order to create a set of data
142 * that is thread-specific rather than session-specific.
144 void *CTC_backend(void *supplied_start_routine)
146 struct thread_tsd *mytsd;
147 void *(*start_routine)(void*) = supplied_start_routine;
149 mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd));
150 memset(mytsd, 0, sizeof(struct thread_tsd));
151 pthread_setspecific(ThreadKey, (const void *) mytsd);
161 * Function to create a thread.
163 void CtdlThreadCreate(void *(*start_routine)(void*))
170 ret = pthread_attr_init(&attr);
171 ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
172 ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine);
173 if (ret != 0) syslog(LOG_EMERG, "pthread_create() : %s", strerror(errno));
177 void InitializeMasterTSD(void) {
178 memset(&masterTSD, 0, sizeof(struct thread_tsd));
181 extern void ShutDownEventQueues(void);
183 int EventQShuttingDown = 0;
186 * Initialize the thread system
188 void go_threading(void)
190 if (pthread_key_create(&ThreadKey, NULL) != 0) {
191 syslog(LOG_EMERG, "pthread_key_create() : %s", strerror(errno));
195 pthread_mutex_init(&ThreadCountMutex, NULL);
197 /* Second call to module init functions now that threading is up */
198 initialise_modules(1);
200 /* Begin with one worker thread. We will expand the pool if necessary */
201 CtdlThreadCreate(worker_thread);
203 /* The supervisor thread monitors worker threads and spawns more of them if it finds that
204 * they are all in use.
206 while (!server_shutting_down) {
207 if ((active_workers == num_workers) && (num_workers < config.c_max_workers)) {
208 CtdlThreadCreate(worker_thread);
213 /* When we get to this point we are getting ready to shut down our Citadel server */
214 if (!EventQShuttingDown)
216 EventQShuttingDown = 1;
217 ShutDownEventQueues();
223 terminate_all_sessions(); /* close all client sockets */
224 CtdlShutdownServiceHooks(); /* close all listener sockets to prevent new connections */
225 PerformSessionHooks(EVT_SHUTDOWN); /* run any registered shutdown hooks */
228 while ( (num_workers > 0) && (countdown-- > 0)) {
229 syslog(LOG_DEBUG, "Waiting %d seconds for %d worker threads to exit",
230 countdown, num_workers