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 as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
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.
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
24 #include <sys/types.h>
26 #include <sys/socket.h>
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
38 # include <sys/time.h>
47 # if HAVE_SYS_SYSCALL_H
48 # include <sys/syscall.h>
52 #include <libcitadel.h>
55 #include "ctdl_module.h"
56 #include "modules_init.h"
57 #include "housekeeping.h"
59 #include "citserver.h"
60 #include "sysdep_decls.h"
64 int num_workers = 0; /* Current number of worker threads */
65 int active_workers = 0; /* Number of ACTIVE worker threads */
66 pthread_key_t ThreadKey;
67 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
68 struct thread_tsd masterTSD;
69 int server_shutting_down = 0; /* set to nonzero during shutdown */
73 void InitializeSemaphores(void)
77 /* Set up a bunch of semaphores to be used for critical sections */
78 for (i=0; i<MAX_SEMAPHORES; ++i) {
79 pthread_mutex_init(&Critters[i], NULL);
87 * Obtain a semaphore lock to begin a critical section.
88 * but only if no one else has one
90 int try_critical_section(int which_one)
92 /* For all types of critical sections except those listed here,
93 * ensure nobody ever tries to do a critical section within a
94 * transaction; this could lead to deadlock.
96 if ( (which_one != S_FLOORCACHE)
97 && (which_one != S_RPLIST)
101 return (pthread_mutex_trylock(&Critters[which_one]));
106 * Obtain a semaphore lock to begin a critical section.
108 void begin_critical_section(int which_one)
110 /* For all types of critical sections except those listed here,
111 * ensure nobody ever tries to do a critical section within a
112 * transaction; this could lead to deadlock.
114 if ( (which_one != S_FLOORCACHE)
115 && (which_one != S_RPLIST)
119 pthread_mutex_lock(&Critters[which_one]);
123 * Release a semaphore lock to end a critical section.
125 void end_critical_section(int which_one)
127 pthread_mutex_unlock(&Critters[which_one]);
134 * Return a pointer to our thread-specific (not session-specific) data.
136 struct thread_tsd *MyThread(void) {
137 register struct thread_tsd *c;
138 return ((c = (struct thread_tsd *) pthread_getspecific(ThreadKey), c == NULL) ? &masterTSD : c);
144 * Called by CtdlThreadCreate()
145 * We have to pass through here before starting our thread in order to create a set of data
146 * that is thread-specific rather than session-specific.
148 void *CTC_backend(void *supplied_start_routine)
150 struct thread_tsd *mytsd;
151 void *(*start_routine)(void*) = supplied_start_routine;
153 mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd));
154 memset(mytsd, 0, sizeof(struct thread_tsd));
155 pthread_setspecific(ThreadKey, (const void *) mytsd);
164 * Function to create a thread.
166 void CtdlThreadCreate(void *(*start_routine)(void*))
173 ret = pthread_attr_init(&attr);
174 ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
175 ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine);
176 if (ret != 0) syslog(LOG_EMERG, "pthread_create() : %s", strerror(errno));
180 void InitializeMasterTSD(void) {
181 memset(&masterTSD, 0, sizeof(struct thread_tsd));
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 /* Second call to module init functions now that threading is up */
196 initialise_modules(1);
198 /* Begin with one worker thread. We will expand the pool if necessary */
199 CtdlThreadCreate(worker_thread);
201 /* The supervisor thread monitors worker threads and spawns more of them if it finds that
202 * they are all in use. FIXME make the 256 max threads a configurable value.
204 while (!server_shutting_down) {
205 if ((active_workers == num_workers) && (num_workers < 256)) {
206 CtdlThreadCreate(worker_thread);
211 /* When we get to this point we are getting ready to shut down our Citadel server */
213 terminate_all_sessions(); /* close all client sockets */
214 CtdlShutdownServiceHooks(); /* close all listener sockets to prevent new connections */
215 PerformSessionHooks(EVT_SHUTDOWN); /* run any registered shutdown hooks */
218 while ( (num_workers > 0) && (countdown-- > 0)) {
219 syslog(LOG_DEBUG, "Waiting %d seconds for %d worker threads to exit",
220 countdown, num_workers