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"
62 #include "event_client.h"
66 * To create a thread you must call one of the create thread functions.
67 * You must pass it the address of (a pointer to a CtdlThreadNode initialised to NULL) like this
68 * struct CtdlThreadNode *node = NULL;
70 * If the thread is created *node will point to the thread control structure for the created thread.
71 * If the thread creation fails *node remains NULL
72 * Do not free the memory pointed to by *node, it doesn't belong to you.
73 * This new interface duplicates much of the eCrash stuff. We should go for closer integration since that would
74 * remove the need for the calls to eCrashRegisterThread and friends
77 static int num_threads = 0; /* Current number of threads */
78 pthread_key_t ThreadKey;
79 pthread_mutex_t Critters[MAX_SEMAPHORES]; /* Things needing locking */
80 struct thread_tsd masterTSD;
84 void InitializeSemaphores(void)
88 /* Set up a bunch of semaphores to be used for critical sections */
89 for (i=0; i<MAX_SEMAPHORES; ++i) {
90 pthread_mutex_init(&Critters[i], NULL);
98 * Obtain a semaphore lock to begin a critical section.
99 * but only if no one else has one
101 int try_critical_section(int which_one)
103 /* For all types of critical sections except those listed here,
104 * ensure nobody ever tries to do a critical section within a
105 * transaction; this could lead to deadlock.
107 if ( (which_one != S_FLOORCACHE)
108 && (which_one != S_RPLIST)
112 return (pthread_mutex_trylock(&Critters[which_one]));
117 * Obtain a semaphore lock to begin a critical section.
119 void begin_critical_section(int which_one)
121 /* For all types of critical sections except those listed here,
122 * ensure nobody ever tries to do a critical section within a
123 * transaction; this could lead to deadlock.
125 if ( (which_one != S_FLOORCACHE)
126 && (which_one != S_RPLIST)
130 pthread_mutex_lock(&Critters[which_one]);
134 * Release a semaphore lock to end a critical section.
136 void end_critical_section(int which_one)
138 pthread_mutex_unlock(&Critters[which_one]);
143 * A function to tell all threads to exit
145 void CtdlThreadStopAll(void)
147 terminate_all_sessions(); /* close all client sockets */
148 CtdlShutdownServiceHooks(); /* close all listener sockets to prevent new connections */
149 PerformSessionHooks(EVT_SHUTDOWN); /* run any registered shutdown hooks */
154 * A function for a thread to check if it has been asked to stop
156 int CtdlThreadCheckStop(void)
159 /* FIXME this needs to do something useful. moar code pls ! */
166 * Return a pointer to our thread-specific (not session-specific) data.
168 struct thread_tsd *MyThread(void) {
169 register struct thread_tsd *c;
170 return ((c = (struct thread_tsd *) pthread_getspecific(ThreadKey), c == NULL) ? &masterTSD : c);
176 * Called by CtdlThreadCreate()
177 * We have to pass through here before starting our thread in order to create a set of data
178 * that is thread-specific rather than session-specific.
180 void *CTC_backend(void *supplied_start_routine)
182 struct thread_tsd *mytsd;
183 void *(*start_routine)(void*) = supplied_start_routine;
185 mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd));
186 memset(mytsd, 0, sizeof(struct thread_tsd));
187 pthread_setspecific(ThreadKey, (const void *) mytsd);
196 * Function to create a thread.
198 void CtdlThreadCreate(void *(*start_routine)(void*))
205 ret = pthread_attr_init(&attr);
206 ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
207 ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine);
208 if (ret != 0) syslog(LOG_EMERG, "pthread_create() : %s", strerror(errno));
214 void InitializeMasterTSD(void) {
215 memset(&masterTSD, 0, sizeof(struct thread_tsd));
220 * Initialize the thread system
222 void go_threading(void)
224 if (pthread_key_create(&ThreadKey, NULL) != 0) {
225 syslog(LOG_EMERG, "pthread_key_create() : %s", strerror(errno));
229 /* Second call to module init functions now that threading is up */
230 initialise_modules(1);
232 CtdlThreadCreate(select_on_master);
234 /* FIXME temporary fixed size pool of worker threads */
235 CtdlThreadCreate(worker_thread);
236 CtdlThreadCreate(worker_thread);
237 CtdlThreadCreate(worker_thread);
238 CtdlThreadCreate(worker_thread);
239 CtdlThreadCreate(worker_thread);
240 CtdlThreadCreate(worker_thread);
241 CtdlThreadCreate(worker_thread);
242 CtdlThreadCreate(worker_thread);
244 /* At this point I am a union worker and therefore serve no useful purpose. */
246 while(!CtdlThreadCheckStop()) {