#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",
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");
}