From 91919dcd0a4a5e8263a677a1a82bdf4b580ae161 Mon Sep 17 00:00:00 2001 From: Art Cancro Date: Fri, 11 Aug 2023 04:13:23 -0900 Subject: [PATCH] This works much better. TSD maintained in-module instead of globally. --- .../server/backends/berkeley_db/berkeley_db.c | 33 ++++++++++++++++- citadel/server/server_main.c | 1 - citadel/server/sysdep.c | 5 --- citadel/server/threads.c | 36 +------------------ citadel/server/threads.h | 17 --------- 5 files changed, 33 insertions(+), 59 deletions(-) diff --git a/citadel/server/backends/berkeley_db/berkeley_db.c b/citadel/server/backends/berkeley_db/berkeley_db.c index ee437e046..c9e355a11 100644 --- a/citadel/server/backends/berkeley_db/berkeley_db.c +++ b/citadel/server/backends/berkeley_db/berkeley_db.c @@ -26,11 +26,37 @@ #error Citadel requires Berkeley DB v18.0 or newer. Please upgrade. #endif - +// Globals (these are used across multiple functions in *this* module, but not elsewhere) static DB *dbp[MAXCDB]; // One DB handle for each Citadel database static DB_ENV *dbenv; // The DB environment (global) +// Thread-Specific-Storage items for this backend +struct thread_tsd { + DB_TXN *tid; /* Transaction handle */ + DBC *cursors[MAXCDB]; /* Cursors, for traversals... */ +}; + +pthread_key_t bdb_thread_key; +#define TSD bdb_tsd() + +// Return a pointer to our thread-specific (not session-specific) data. +struct thread_tsd *bdb_tsd(void) { + + struct thread_tsd *c = (struct thread_tsd *) pthread_getspecific(bdb_thread_key) ; + if (c != NULL) { + return(c); // Got it. + } + + // If there's no TSD for this thread, it must be a new thread. Create our TSD region. + c = (struct thread_tsd *) malloc(sizeof(struct thread_tsd)); + memset(c, 0, sizeof(struct thread_tsd)); + pthread_setspecific(bdb_thread_key, (const void *) c); + syslog(LOG_DEBUG, "\033[31mCREATED %lx\033[0m", (long unsigned int)c); + return(c); +} + + // Called by other functions in this module to GTFO quickly if we need to. Not part of the backend API. void bdb_abort(void) { syslog(LOG_DEBUG, "bdb: citserver is stopping in order to prevent data loss. uid=%d gid=%d euid=%d egid=%d", @@ -761,6 +787,11 @@ void bdb_init_backend(void) { cdb_trunc = bdb_trunc; cdb_chmod_data = bdb_chmod_data; + if (pthread_key_create(&bdb_thread_key, NULL) != 0) { // TSD key for this module + syslog(LOG_ERR, "pthread_key_create() : %m"); + exit(CTDLEXIT_THREAD); + } + syslog(LOG_INFO, "db: initialized Berkeley DB backend"); } diff --git a/citadel/server/server_main.c b/citadel/server/server_main.c index 4858c85ea..870205346 100644 --- a/citadel/server/server_main.c +++ b/citadel/server/server_main.c @@ -199,7 +199,6 @@ int main(int argc, char **argv) { // initialize the master context InitializeMasterCC(); - InitializeMasterTSD(); setlogmask(LOG_UPTO(max_log_level)); openlog("citserver", diff --git a/citadel/server/sysdep.c b/citadel/server/sysdep.c index 798e218d0..668b50bd0 100644 --- a/citadel/server/sysdep.c +++ b/citadel/server/sysdep.c @@ -75,11 +75,6 @@ void init_sysdep(void) { init_ssl(); #endif - if (pthread_key_create(&ThreadKey, NULL) != 0) { // TSD for threads - syslog(LOG_ERR, "pthread_key_create() : %m"); - exit(CTDLEXIT_THREAD); - } - if (pthread_key_create(&MyConKey, NULL) != 0) { // TSD for sessions syslog(LOG_CRIT, "sysdep: can't create TSD key: %m"); exit(CTDLEXIT_THREAD); diff --git a/citadel/server/threads.c b/citadel/server/threads.c index a745d2a45..f666c329b 100644 --- a/citadel/server/threads.c +++ b/citadel/server/threads.c @@ -18,9 +18,7 @@ int num_workers = 0; // Current number of worker threads int active_workers = 0; // Number of ACTIVE worker threads -pthread_key_t ThreadKey; pthread_mutex_t Critters[MAX_SEMAPHORES]; // Things needing locking -struct thread_tsd masterTSD; int server_shutting_down = 0; // set to nonzero during shutdown pthread_mutex_t ThreadCountMutex; @@ -68,33 +66,6 @@ void end_critical_section(int which_one) { } -// Return a pointer to our thread-specific (not session-specific) data. -struct thread_tsd *MyThread(void) { - struct thread_tsd *c = (struct thread_tsd *) pthread_getspecific(ThreadKey) ; - if (!c) { - return &masterTSD; - } - return c; -} - - -// Called by CtdlThreadCreate() -// We have to pass through here before starting our thread in order to create a set of data -// that is thread-specific rather than session-specific. -void *CTC_backend(void *supplied_start_routine) { - struct thread_tsd *mytsd; - void *(*start_routine)(void*) = supplied_start_routine; - - mytsd = (struct thread_tsd *) malloc(sizeof(struct thread_tsd)); - memset(mytsd, 0, sizeof(struct thread_tsd)); - pthread_setspecific(ThreadKey, (const void *) mytsd); - - start_routine(NULL); - // free(mytsd); - return(NULL); -} - - // Function to create a thread. void CtdlThreadCreate(void *(*start_routine)(void*)) { pthread_t thread; @@ -103,16 +74,11 @@ void CtdlThreadCreate(void *(*start_routine)(void*)) { ret = pthread_attr_init(&attr); ret = pthread_attr_setstacksize(&attr, THREADSTACKSIZE); - ret = pthread_create(&thread, &attr, CTC_backend, (void *)start_routine); + ret = pthread_create(&thread, &attr, start_routine, NULL); if (ret != 0) syslog(LOG_ERR, "pthread_create() : %m"); } -void InitializeMasterTSD(void) { - memset(&masterTSD, 0, sizeof(struct thread_tsd)); -} - - // Initialize the thread system void go_threading(void) { pthread_mutex_init(&ThreadCountMutex, NULL); diff --git a/citadel/server/threads.h b/citadel/server/threads.h index 33b854ea9..e55aa394c 100644 --- a/citadel/server/threads.h +++ b/citadel/server/threads.h @@ -10,29 +10,13 @@ #include #include - -#include - #include "server.h" #include "sysdep_decls.h" -/* - * Things we need to keep track of per-thread instead of per-session - */ -struct thread_tsd { - DB_TXN *tid; /* Transaction handle */ - DBC *cursors[MAXCDB]; /* Cursors, for traversals... */ -}; - -extern pthread_key_t ThreadKey; -extern struct thread_tsd masterTSD; -#define TSD MyThread() - extern int num_workers; extern int active_workers; extern int server_shutting_down; -struct thread_tsd *MyThread(void); int try_critical_section (int which_one); void begin_critical_section (int which_one); void end_critical_section (int which_one); @@ -40,7 +24,6 @@ void go_threading(void); void InitializeMasterTSD(void); void CtdlThreadCreate(void *(*start_routine)(void*)); - extern pthread_mutex_t ThreadCountMutex;; #endif // THREADS_H -- 2.39.2