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