]> code.citadel.org Git - citadel.git/blob - citadel/threads.c
Switched back to the old style thread architecture in preparation for eventual migrat...
[citadel.git] / citadel / threads.c
1 /*
2  * Thread handling stuff for Citadel server
3  *
4  * Copyright (c) 1987-2011 by the citadel.org team
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include <errno.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 #include <syslog.h>
31
32 #include "sysdep.h"
33 #if TIME_WITH_SYS_TIME
34 # include <sys/time.h>
35 # include <time.h>
36 #else
37 # if HAVE_SYS_TIME_H
38 #  include <sys/time.h>
39 # else
40 #  include <time.h>
41 # endif
42 #endif
43
44 #ifdef HAVE_SYSCALL_H
45 # include <syscall.h>
46 #else 
47 # if HAVE_SYS_SYSCALL_H
48 #  include <sys/syscall.h>
49 # endif
50 #endif
51
52 #include <libcitadel.h>
53
54 #include "threads.h"
55 #include "ctdl_module.h"
56 #include "modules_init.h"
57 #include "housekeeping.h"
58 #include "config.h"
59 #include "citserver.h"
60 #include "sysdep_decls.h"
61 #include "context.h"
62
63
64 /*
65  * To create a thread you must call one of the create thread functions.
66  * You must pass it the address of (a pointer to a CtdlThreadNode initialised to NULL) like this
67  * struct CtdlThreadNode *node = NULL;
68  * pass in &node
69  * If the thread is created *node will point to the thread control structure for the created thread.
70  * If the thread creation fails *node remains NULL
71  * Do not free the memory pointed to by *node, it doesn't belong to you.
72  * This new interface duplicates much of the eCrash stuff. We should go for closer integration since that would
73  * remove the need for the calls to eCrashRegisterThread and friends
74  */
75
76 static int num_threads = 0;                     /* Current number of threads */
77 pthread_key_t ThreadKey;
78 pthread_mutex_t Critters[MAX_SEMAPHORES];       /* Things needing locking */
79 struct thread_tsd masterTSD;
80
81
82
83 void InitializeSemaphores(void)
84 {
85         int i;
86
87         /* Set up a bunch of semaphores to be used for critical sections */
88         for (i=0; i<MAX_SEMAPHORES; ++i) {
89                 pthread_mutex_init(&Critters[i], NULL);
90         }
91 }
92
93
94
95
96 /*
97  * Obtain a semaphore lock to begin a critical section.
98  * but only if no one else has one
99  */
100 int try_critical_section(int which_one)
101 {
102         /* For all types of critical sections except those listed here,
103          * ensure nobody ever tries to do a critical section within a
104          * transaction; this could lead to deadlock.
105          */
106         if (    (which_one != S_FLOORCACHE)
107                 && (which_one != S_RPLIST)
108         ) {
109                 cdb_check_handles();
110         }
111         return (pthread_mutex_trylock(&Critters[which_one]));
112 }
113
114
115 /*
116  * Obtain a semaphore lock to begin a critical section.
117  */
118 void begin_critical_section(int which_one)
119 {
120         /* For all types of critical sections except those listed here,
121          * ensure nobody ever tries to do a critical section within a
122          * transaction; this could lead to deadlock.
123          */
124         if (    (which_one != S_FLOORCACHE)
125                 && (which_one != S_RPLIST)
126         ) {
127                 cdb_check_handles();
128         }
129         pthread_mutex_lock(&Critters[which_one]);
130 }
131
132 /*
133  * Release a semaphore lock to end a critical section.
134  */
135 void end_critical_section(int which_one)
136 {
137         pthread_mutex_unlock(&Critters[which_one]);
138 }
139
140
141 /*
142  * A function to tell all threads to exit
143  */
144 void CtdlThreadStopAll(void)
145 {
146         terminate_all_sessions();               /* close all client sockets */
147         CtdlShutdownServiceHooks();             /* close all listener sockets to prevent new connections */
148         PerformSessionHooks(EVT_SHUTDOWN);      /* run any registered shutdown hooks */
149 }
150
151
152 /*
153  * A function for a thread to check if it has been asked to stop
154  */
155 int CtdlThreadCheckStop(void)
156 {
157
158         /* FIXME this needs to do something useful.  moar code pls ! */
159
160         return 0;
161 }
162
163
164 /*
165  * Return a pointer to our thread-specific (not session-specific) data.
166  */ 
167 struct thread_tsd *MyThread(void) {
168         register struct thread_tsd *c;
169         return ((c = (struct thread_tsd *) pthread_getspecific(ThreadKey), c == NULL) ? &masterTSD : c);
170 }
171
172
173
174 /* 
175  * Called by CtdlThreadCreate()
176  * We have to pass through here before starting our thread in order to create a set of data
177  * that is thread-specific rather than session-specific.
178  */
179 void *CTC_backend(void *supplied_start_routine)
180 {
181         struct thread_tsd *mytsd;
182         void *(*start_routine)(void*) = supplied_start_routine;
183
184         mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd));
185         memset(mytsd, 0, sizeof(struct thread_tsd));
186         pthread_setspecific(ThreadKey, (const void *) mytsd);
187
188         start_routine(NULL);
189
190         return(NULL);
191 }
192
193  
194 /*
195  * Function to create a thread.
196  */ 
197 void CtdlThreadCreate(void *(*start_routine)(void*))
198 {
199         pthread_t thread;
200         pthread_attr_t attr;
201         int ret = 0;
202
203
204         ret = pthread_attr_init(&attr);
205         ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
206         ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine);
207         if (ret != 0) syslog(LOG_EMERG, "pthread_create() : %s", strerror(errno));
208
209         ++num_threads;
210 }
211
212
213 void InitializeMasterTSD(void) {
214         memset(&masterTSD, 0, sizeof(struct thread_tsd));
215 }
216
217
218 /*
219  * Initialize the thread system
220  */
221 void go_threading(void)
222 {
223         if (pthread_key_create(&ThreadKey, NULL) != 0) {
224                 syslog(LOG_EMERG, "pthread_key_create() : %s", strerror(errno));
225                 abort();
226         }
227
228         /* Second call to module init functions now that threading is up */
229         initialise_modules(1);
230
231         CtdlThreadCreate(select_on_master);
232
233         /* FIXME temporary fixed size pool of worker threads */
234         CtdlThreadCreate(worker_thread);
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
243         /* At this point I am a union worker and therefore serve no useful purpose. */
244
245         while(!CtdlThreadCheckStop()) {
246                 sleep(3);
247         }
248
249         /* Shut down */
250         CtdlThreadStopAll();
251         exit(0);
252 }