This works much better. TSD maintained in-module instead of globally.
authorArt Cancro <ajc@citadel.org>
Fri, 11 Aug 2023 13:13:23 +0000 (04:13 -0900)
committerArt Cancro <ajc@citadel.org>
Fri, 11 Aug 2023 13:13:23 +0000 (04:13 -0900)
citadel/server/backends/berkeley_db/berkeley_db.c
citadel/server/server_main.c
citadel/server/sysdep.c
citadel/server/threads.c
citadel/server/threads.h

index ee437e0467ba1278e5764ef4a0dabe96ea05240f..c9e355a110a862016d19f3fb162ab9a1a3b686a2 100644 (file)
 #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");
 }
 
index 4858c85ea54ca2d9e1f73c45144497ad656577b6..870205346e8156cdf3a25b0d093d666e290d5d4f 100644 (file)
@@ -199,7 +199,6 @@ int main(int argc, char **argv) {
 
        // initialize the master context
        InitializeMasterCC();
-       InitializeMasterTSD();
 
        setlogmask(LOG_UPTO(max_log_level));
        openlog("citserver",
index 798e218d051dc6b6200557bc2141a795bf747dbe..668b50bd023d4e8c1a402f2f273c7de9240bcd79 100644 (file)
@@ -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);
index a745d2a4552fd0ecb840a7d63056b4321e96e9a8..f666c329b55fce56e40cc74df26e78f500fa40a0 100644 (file)
@@ -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);
index 33b854ea9cbee84d8ce5dc67f28734ae7f754f1c..e55aa394c2642afa8ddacff793b5c519a192dbc7 100644 (file)
 
 #include <sys/time.h>
 #include <string.h>
-
-#include <db.h>
-
 #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