]> code.citadel.org Git - citadel.git/blob - citadel/server/threads.c
ed229c19e76443baaef2024bfa8f57032f1c4fd3
[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_mutex_t Critters[MAX_SEMAPHORES];       // Things needing locking
22 int server_shutting_down = 0;                   // set to nonzero during shutdown
23 pthread_mutex_t ThreadCountMutex;
24
25 void InitializeSemaphores(void) {
26         int i;
27
28         // Set up a bunch of semaphores to be used for critical sections
29         for (i=0; i<MAX_SEMAPHORES; ++i) {
30                 pthread_mutex_init(&Critters[i], NULL);
31         }
32 }
33
34
35 // Obtain a semaphore lock to begin a critical section, but only if no one else has one
36 int try_critical_section(int which_one) {
37         // For all types of critical sections except those listed here,
38         // ensure nobody ever tries to do a critical section within a
39         // transaction; this could lead to deadlock.
40         if (    (which_one != S_FLOORCACHE)
41                 && (which_one != S_NETCONFIGS)
42         ) {
43                 cdb_check_handles();
44         }
45         return (pthread_mutex_trylock(&Critters[which_one]));
46 }
47
48
49 // Obtain a semaphore lock to begin a critical section.
50 void begin_critical_section(int which_one) {
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         if (    (which_one != S_FLOORCACHE)
55                 && (which_one != S_NETCONFIGS)
56         ) {
57                 cdb_check_handles();
58         }
59
60         struct timeval t1,t2;
61         syslog(LOG_DEBUG, "\033[33mthreads: lock %d requested\033[0m", which_one);
62         gettimeofday(&t1, NULL);
63         long ms1 = (t1.tv_sec * 1000) + t1.tv_usec;
64         pthread_mutex_lock(&Critters[which_one]);
65         gettimeofday(&t2, NULL);
66         long ms2 = (t2.tv_sec * 1000) + t2.tv_usec;
67         syslog(LOG_DEBUG, "\033[32mthreads: lock %d acquired in %ld milliseconds\033[0m", which_one, ms2-ms1);
68
69 }
70
71
72 // Release a semaphore lock to end a critical section.
73 void end_critical_section(int which_one) {
74         pthread_mutex_unlock(&Critters[which_one]);
75         syslog(LOG_DEBUG, "\033[31mthreads: lock %d released\033[0m", which_one);
76 }
77
78
79 // Function to create a thread.
80 void CtdlThreadCreate(void *(*start_routine)(void*)) {
81         pthread_t thread;
82         pthread_attr_t attr;
83         int ret = 0;
84
85         ret = pthread_attr_init(&attr);
86         ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE);
87         ret = pthread_create(&thread, &attr, start_routine, NULL);
88         if (ret != 0) syslog(LOG_ERR, "pthread_create() : %m");
89 }
90
91
92 // Initialize the thread system
93 void go_threading(void) {
94         pthread_mutex_init(&ThreadCountMutex, NULL);
95
96         // Second call to module init functions now that threading is up
97         initialize_modules(1);
98
99         // Begin with one worker thread.  We will expand the pool if necessary
100         CtdlThreadCreate(worker_thread);
101
102         // The supervisor thread monitors worker threads and spawns more of them if it finds that they are all in use.
103         while (!server_shutting_down) {
104                 if ((active_workers == num_workers) && (num_workers < CtdlGetConfigInt("c_max_workers"))) {
105                         CtdlThreadCreate(worker_thread);
106                 }
107                 usleep(1000000);
108         }
109
110         // When we get to this point we are getting ready to shut down our Citadel server
111         terminate_all_sessions();               // close all client sockets
112         CtdlShutdownServiceHooks();             // close all listener sockets to prevent new connections
113         PerformSessionHooks(EVT_SHUTDOWN);      // run any registered shutdown hooks
114
115         // We used to wait for all threads to exit.  Fuck that.  The only thing important is that the databases are
116         // cleanly unmounted.  After that, exit the whole program.
117 }