citadel
About:
Citadel
is an advanced messaging and collaboration system for groupware and BBS applications (preferred OS: Linux).
Fossies
Dox
:
citadel.tar.gz
("unofficial" and yet experimental doxygen-generated source code documentation)
Loading...
Searching...
No Matches
threads.c
Go to the documentation of this file.
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
) && (which_one !=
S_NETCONFIGS
)) {
55
cdb_check_handles
();
56
}
57
58
pthread_mutex_lock(&
Critters
[which_one]);
59
}
60
61
62
// Release a semaphore lock to end a critical section.
63
void
end_critical_section
(
int
which_one) {
64
pthread_mutex_unlock(&
Critters
[which_one]);
65
}
66
67
68
// Function to create a thread.
69
void
CtdlThreadCreate
(
void
*(*start_routine)(
void
*)) {
70
pthread_t thread;
71
pthread_attr_t attr;
72
int
ret = 0;
73
74
ret = pthread_attr_init(&attr);
75
ret = pthread_attr_setstacksize(&attr,
THREADSTACKSIZE
);
76
ret = pthread_create(&thread, &attr, start_routine, NULL);
77
if
(ret != 0) syslog(LOG_ERR,
"pthread_create() : %m"
);
78
}
79
80
81
// Initialize the thread system
82
void
go_threading
(
void
) {
83
pthread_mutex_init(&
ThreadCountMutex
, NULL);
84
85
// Second call to module init functions now that threading is up
86
initialize_modules
(1);
87
88
// Begin with one worker thread. We will expand the pool if necessary
89
CtdlThreadCreate
(
worker_thread
);
90
91
// The supervisor thread monitors worker threads and spawns more of them if it finds that they are all in use.
92
while
(!
server_shutting_down
) {
93
if
((
active_workers
==
num_workers
) && (
num_workers
<
CtdlGetConfigInt
(
"c_max_workers"
))) {
94
CtdlThreadCreate
(
worker_thread
);
95
}
96
usleep(1000000);
97
}
98
99
// When we get to this point we are getting ready to shut down our Citadel server
100
terminate_all_sessions
();
// close all client sockets
101
CtdlShutdownServiceHooks
();
// close all listener sockets to prevent new connections
102
PerformSessionHooks
(
EVT_SHUTDOWN
);
// run any registered shutdown hooks
103
104
// We used to wait for all threads to exit. Fuck that. The only thing important is that the databases are
105
// cleanly unmounted. After that, exit the whole program.
106
}
EVT_SHUTDOWN
@ EVT_SHUTDOWN
Definition
citadel_defs.h:229
S_FLOORCACHE
@ S_FLOORCACHE
Definition
citadel_defs.h:164
S_NETCONFIGS
@ S_NETCONFIGS
Definition
citadel_defs.h:163
MAX_SEMAPHORES
@ MAX_SEMAPHORES
Definition
citadel_defs.h:174
CtdlGetConfigInt
int CtdlGetConfigInt(char *key)
Definition
config.c:388
config.h
terminate_all_sessions
void terminate_all_sessions(void)
Definition
context.c:217
context.h
ctdl_module.h
CtdlShutdownServiceHooks
void CtdlShutdownServiceHooks(void)
Definition
serv_extensions.c:441
cdb_check_handles
void(* cdb_check_handles)(void)
Definition
database.c:36
initialize_modules
void initialize_modules(int is_threading)
Definition
modules_init.c:26
modules_init.h
PerformSessionHooks
void PerformSessionHooks(int EventType)
Definition
serv_extensions.c:461
serv_extensions.h
THREADSTACKSIZE
#define THREADSTACKSIZE
Definition
sysconfig.h:84
worker_thread
void * worker_thread(void *blah)
Definition
sysdep.c:655
begin_critical_section
void begin_critical_section(int which_one)
Definition
threads.c:50
InitializeSemaphores
void InitializeSemaphores(void)
Definition
threads.c:25
end_critical_section
void end_critical_section(int which_one)
Definition
threads.c:63
Critters
pthread_mutex_t Critters[MAX_SEMAPHORES]
Definition
threads.c:21
CtdlThreadCreate
void CtdlThreadCreate(void *(*start_routine)(void *))
Definition
threads.c:69
ThreadCountMutex
pthread_mutex_t ThreadCountMutex
Definition
threads.c:23
active_workers
int active_workers
Definition
threads.c:20
server_shutting_down
int server_shutting_down
Definition
threads.c:22
go_threading
void go_threading(void)
Definition
threads.c:82
num_workers
int num_workers
Definition
threads.c:19
threads.h
server
threads.c
Generated by
1.10.0