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