#include "DatabaseForThread.h" #include "ThreadClass.h" typedef std::map< std::string, DatabaseWithRetry * > Databases; static __thread Databases *sharedDatabases; DatabaseWithRetry *DatabaseForThread(std::string name) { if (!sharedDatabases) sharedDatabases = new Databases(); DatabaseWithRetry *&result = (*sharedDatabases)[name]; if (!result) { ThreadClass *thread = ThreadClass::findThread(); std::string debugName = "automatic: " + name; if (thread) debugName += " in thread " + thread->getName(); result = new DatabaseWithRetry(name, debugName); } return result; } void assertNoDatabaseThisThread() { if (sharedDatabases) // Someone created at least one database before this call. Report the // error. abort(); // Store a bogus pointer. DatabaseForThread() will see that this is not // NULL, so it won't try to create a legitimate value. But any attempt // use this will fail. sharedDatabases = (Databases *)-1; } void disableDatabaseThisThread(std::string const &name) { if (!sharedDatabases) sharedDatabases = new Databases(); DatabaseWithRetry *&pointerToDatabase = (*sharedDatabases)[name]; if (pointerToDatabase) // Already initialized before this call. abort(); pointerToDatabase = (DatabaseWithRetry *)-1; }