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)  

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