]> code.citadel.org Git - citadel.git/blobdiff - citadel/server/backends/berkeley_db/berkeley_db.c
This works much better. TSD maintained in-module instead of globally.
[citadel.git] / citadel / server / backends / berkeley_db / berkeley_db.c
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");
 }