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 <libcitadel.h>
19 #include "modules_init.h"
20 #include "serv_extensions.h"
21 #include "ctdl_module.h"
26 int num_workers = 0; /* Current number of worker threads */
27 int active_workers = 0; /* Number of ACTIVE worker threads */
28 pthread_key_t ThreadKey;
29 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
30 struct thread_tsd masterTSD;
31 int server_shutting_down = 0; /* set to nonzero during shutdown */
33 pthread_mutex_t ThreadCountMutex;;
36 void InitializeSemaphores(void)
40 /* Set up a bunch of semaphores to be used for critical sections */
41 for (i=0; i<MAX_SEMAPHORES; ++i) {
42 pthread_mutex_init(&Critters[i], NULL);
50 * Obtain a semaphore lock to begin a critical section.
51 * but only if no one else has one
53 int try_critical_section(int which_one)
55 /* For all types of critical sections except those listed here,
56 * ensure nobody ever tries to do a critical section within a
57 * transaction; this could lead to deadlock.
59 if ( (which_one != S_FLOORCACHE)
60 && (which_one != S_RPLIST)
64 return (pthread_mutex_trylock(&Critters[which_one]));
69 * Obtain a semaphore lock to begin a critical section.
71 void begin_critical_section(int which_one)
73 /* For all types of critical sections except those listed here,
74 * ensure nobody ever tries to do a critical section within a
75 * transaction; this could lead to deadlock.
77 if ( (which_one != S_FLOORCACHE)
78 && (which_one != S_RPLIST)
82 pthread_mutex_lock(&Critters[which_one]);
86 * Release a semaphore lock to end a critical section.
88 void end_critical_section(int which_one)
90 pthread_mutex_unlock(&Critters[which_one]);
97 * Return a pointer to our thread-specific (not session-specific) data.
99 struct thread_tsd *MyThread(void) {
100 register struct thread_tsd *c;
101 return ((c = (struct thread_tsd *) pthread_getspecific(ThreadKey), c == NULL) ? &masterTSD : c);
107 * Called by CtdlThreadCreate()
108 * We have to pass through here before starting our thread in order to create a set of data
109 * that is thread-specific rather than session-specific.
111 void *CTC_backend(void *supplied_start_routine)
113 struct thread_tsd *mytsd;
114 void *(*start_routine)(void*) = supplied_start_routine;
116 mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd));
117 memset(mytsd, 0, sizeof(struct thread_tsd));
118 pthread_setspecific(ThreadKey, (const void *) mytsd);
128 * Function to create a thread.
130 void CtdlThreadCreate(void *(*start_routine)(void*))
137 ret = pthread_attr_init(&attr);
138 ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
139 ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine);
140 if (ret != 0) syslog(LOG_EMERG, "pthread_create() : %s", strerror(errno));
144 void InitializeMasterTSD(void) {
145 memset(&masterTSD, 0, sizeof(struct thread_tsd));
148 extern void ShutDownEventQueues(void);
150 int EventQShuttingDown = 0;
153 * Initialize the thread system
155 void go_threading(void)
157 if (pthread_key_create(&ThreadKey, NULL) != 0) {
158 syslog(LOG_EMERG, "pthread_key_create() : %s", strerror(errno));
162 pthread_mutex_init(&ThreadCountMutex, NULL);
164 /* Second call to module init functions now that threading is up */
165 initialise_modules(1);
167 /* Begin with one worker thread. We will expand the pool if necessary */
168 CtdlThreadCreate(worker_thread);
170 /* The supervisor thread monitors worker threads and spawns more of them if it finds that
171 * they are all in use.
173 while (!server_shutting_down) {
174 if ((active_workers == num_workers) && (num_workers < config.c_max_workers)) {
175 CtdlThreadCreate(worker_thread);
180 /* When we get to this point we are getting ready to shut down our Citadel server */
181 if (!EventQShuttingDown)
183 EventQShuttingDown = 1;
184 ShutDownEventQueues();
190 terminate_all_sessions(); /* close all client sockets */
191 CtdlShutdownServiceHooks(); /* close all listener sockets to prevent new connections */
192 PerformSessionHooks(EVT_SHUTDOWN); /* run any registered shutdown hooks */
195 while ( (num_workers > 0) && (countdown-- > 0)) {
196 syslog(LOG_DEBUG, "Waiting %d seconds for %d worker threads to exit",
197 countdown, num_workers