21f585cdae75671707f0adcb33684f7ac30cb3cf
[citadel.git] / citadel / threads.c
1 /*
2  * Thread handling stuff for Citadel server
3  *
4  * Copyright (c) 1987-2019 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, version 3.
8  *
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.
13  */
14
15 #include <errno.h>
16 #include <stdio.h>
17 #include <syslog.h>
18 #include <libcitadel.h>
19 #include "modules_init.h"
20 #include "serv_extensions.h"
21 #include "ctdl_module.h"
22 #include "config.h"
23 #include "context.h"
24 #include "threads.h"
25
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 */
32 pthread_mutex_t ThreadCountMutex;
33
34 void InitializeSemaphores(void)
35 {
36         int i;
37
38         /* Set up a bunch of semaphores to be used for critical sections */
39         for (i=0; i<MAX_SEMAPHORES; ++i) {
40                 pthread_mutex_init(&Critters[i], NULL);
41         }
42 }
43
44
45 /*
46  * Obtain a semaphore lock to begin a critical section.
47  * but only if no one else has one
48  */
49 int try_critical_section(int which_one)
50 {
51         /* For all types of critical sections except those listed here,
52          * ensure nobody ever tries to do a critical section within a
53          * transaction; this could lead to deadlock.
54          */
55         if (    (which_one != S_FLOORCACHE)
56         ) {
57                 cdb_check_handles();
58         }
59         return (pthread_mutex_trylock(&Critters[which_one]));
60 }
61
62
63 /*
64  * Obtain a semaphore lock to begin a critical section.
65  */
66 void begin_critical_section(int which_one)
67 {
68         /* For all types of critical sections except those listed here,
69          * ensure nobody ever tries to do a critical section within a
70          * transaction; this could lead to deadlock.
71          */
72         if (    (which_one != S_FLOORCACHE)
73         ) {
74                 cdb_check_handles();
75         }
76         pthread_mutex_lock(&Critters[which_one]);
77 }
78
79
80 /*
81  * Release a semaphore lock to end a critical section.
82  */
83 void end_critical_section(int which_one)
84 {
85         pthread_mutex_unlock(&Critters[which_one]);
86 }
87
88
89 /*
90  * Return a pointer to our thread-specific (not session-specific) data.
91  */ 
92 struct thread_tsd *MyThread(void) {
93         struct thread_tsd *c = (struct thread_tsd *) pthread_getspecific(ThreadKey) ;
94         if (!c) {
95                 return &masterTSD;
96         }
97         return c;
98 }
99
100
101 /* 
102  * Called by CtdlThreadCreate()
103  * We have to pass through here before starting our thread in order to create a set of data
104  * that is thread-specific rather than session-specific.
105  */
106 void *CTC_backend(void *supplied_start_routine)
107 {
108         struct thread_tsd *mytsd;
109         void *(*start_routine)(void*) = supplied_start_routine;
110
111         mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd));
112         memset(mytsd, 0, sizeof(struct thread_tsd));
113         pthread_setspecific(ThreadKey, (const void *) mytsd);
114
115         start_routine(NULL);
116         // free(mytsd);
117         return(NULL);
118 }
119
120  
121 /*
122  * Function to create a thread.
123  */ 
124 void CtdlThreadCreate(void *(*start_routine)(void*))
125 {
126         pthread_t thread;
127         pthread_attr_t attr;
128         int ret = 0;
129
130         ret = pthread_attr_init(&attr);
131         ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
132         ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine);
133         if (ret != 0) syslog(LOG_ERR, "pthread_create() : %m");
134 }
135
136
137 void InitializeMasterTSD(void) {
138         memset(&masterTSD, 0, sizeof(struct thread_tsd));
139 }
140
141
142 /*
143  * Initialize the thread system
144  */
145 void go_threading(void)
146 {
147         pthread_mutex_init(&ThreadCountMutex, NULL);
148
149         /* Second call to module init functions now that threading is up */
150         initialise_modules(1);
151
152         /* Begin with one worker thread.  We will expand the pool if necessary */
153         CtdlThreadCreate(worker_thread);
154
155         /* The supervisor thread monitors worker threads and spawns more of them if it finds that
156          * they are all in use.
157          */
158         while (!server_shutting_down) {
159                 if ((active_workers == num_workers) && (num_workers < CtdlGetConfigInt("c_max_workers"))) {
160                         CtdlThreadCreate(worker_thread);
161                 }
162                 usleep(1000000);
163         }
164
165         /* When we get to this point we are getting ready to shut down our Citadel server */
166         terminate_all_sessions();               /* close all client sockets */
167         CtdlShutdownServiceHooks();             /* close all listener sockets to prevent new connections */
168         PerformSessionHooks(EVT_SHUTDOWN);      /* run any registered shutdown hooks */
169
170         /* We used to wait for all threads to exit.  Fuck that.  The only thing important is that the databases are
171          * cleanly unmounted.  After that, exit the whole program.
172          */
173 #if 0
174         int countdown = 30;
175         while ( (num_workers > 0) && (countdown-- > 0)) {
176                 syslog(LOG_DEBUG, "Waiting %d seconds for %d worker threads to exit",
177                         countdown, num_workers
178                 );
179                 usleep(1000000);
180         }
181 #endif
182
183 }