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 */
68 void InitializeSemaphores(void)
72 /* Set up a bunch of semaphores to be used for critical sections */
73 for (i=0; i<MAX_SEMAPHORES; ++i) {
74 pthread_mutex_init(&Critters[i], NULL);
82 * Obtain a semaphore lock to begin a critical section.
83 * but only if no one else has one
85 int try_critical_section(int which_one)
87 /* For all types of critical sections except those listed here,
88 * ensure nobody ever tries to do a critical section within a
89 * transaction; this could lead to deadlock.
91 if ( (which_one != S_FLOORCACHE)
92 && (which_one != S_RPLIST)
96 return (pthread_mutex_trylock(&Critters[which_one]));
101 * Obtain a semaphore lock to begin a critical section.
103 void begin_critical_section(int which_one)
105 /* For all types of critical sections except those listed here,
106 * ensure nobody ever tries to do a critical section within a
107 * transaction; this could lead to deadlock.
109 if ( (which_one != S_FLOORCACHE)
110 && (which_one != S_RPLIST)
114 pthread_mutex_lock(&Critters[which_one]);
118 * Release a semaphore lock to end a critical section.
120 void end_critical_section(int which_one)
122 pthread_mutex_unlock(&Critters[which_one]);
129 * Return a pointer to our thread-specific (not session-specific) data.
131 struct thread_tsd *MyThread(void) {
132 register struct thread_tsd *c;
133 return ((c = (struct thread_tsd *) pthread_getspecific(ThreadKey), c == NULL) ? &masterTSD : c);
139 * Called by CtdlThreadCreate()
140 * We have to pass through here before starting our thread in order to create a set of data
141 * that is thread-specific rather than session-specific.
143 void *CTC_backend(void *supplied_start_routine)
145 struct thread_tsd *mytsd;
146 void *(*start_routine)(void*) = supplied_start_routine;
148 mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd));
149 memset(mytsd, 0, sizeof(struct thread_tsd));
150 pthread_setspecific(ThreadKey, (const void *) mytsd);
160 * Function to create a thread.
162 void CtdlThreadCreate(void *(*start_routine)(void*))
169 ret = pthread_attr_init(&attr);
170 ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
171 ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine);
172 if (ret != 0) syslog(LOG_EMERG, "pthread_create() : %s", strerror(errno));
176 void InitializeMasterTSD(void) {
177 memset(&masterTSD, 0, sizeof(struct thread_tsd));
180 extern void ShutDownEventQueues(void);
182 int EventQShuttingDown = 0;
185 * Initialize the thread system
187 void go_threading(void)
189 if (pthread_key_create(&ThreadKey, NULL) != 0) {
190 syslog(LOG_EMERG, "pthread_key_create() : %s", strerror(errno));
194 /* Second call to module init functions now that threading is up */
195 initialise_modules(1);
197 /* Begin with one worker thread. We will expand the pool if necessary */
198 CtdlThreadCreate(worker_thread);
200 /* The supervisor thread monitors worker threads and spawns more of them if it finds that
201 * they are all in use. FIXME make the 256 max threads a configurable value.
203 while (!server_shutting_down) {
204 if ((active_workers == num_workers) && (num_workers < 256)) {
205 CtdlThreadCreate(worker_thread);
210 /* When we get to this point we are getting ready to shut down our Citadel server */
211 if (!EventQShuttingDown)
213 EventQShuttingDown = 1;
214 ShutDownEventQueues();
220 terminate_all_sessions(); /* close all client sockets */
221 CtdlShutdownServiceHooks(); /* close all listener sockets to prevent new connections */
222 PerformSessionHooks(EVT_SHUTDOWN); /* run any registered shutdown hooks */
225 while ( (num_workers > 0) && (countdown-- > 0)) {
226 syslog(LOG_DEBUG, "Waiting %d seconds for %d worker threads to exit",
227 countdown, num_workers