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