ds@1952: /**
ds@1952: * vim: set ts=4 :
ds@1952: * =============================================================================
ds@1952: * SourceMod
ds@1952: * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
ds@1952: * =============================================================================
ds@1952: *
ds@1952: * This program is free software; you can redistribute it and/or modify it under
ds@1952: * the terms of the GNU General Public License, version 3.0, as published by the
ds@1952: * Free Software Foundation.
ds@1952: *
ds@1952: * This program is distributed in the hope that it will be useful, but WITHOUT
ds@1952: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
ds@1952: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
ds@1952: * details.
ds@1952: *
ds@1952: * You should have received a copy of the GNU General Public License along with
ds@1952: * this program. If not, see .
ds@1952: *
ds@1952: * As a special exception, AlliedModders LLC gives you permission to link the
ds@1952: * code of this program (as well as its derivative works) to "Half-Life 2," the
ds@1952: * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
ds@1952: * by the Valve Corporation. You must obey the GNU General Public License in
ds@1952: * all respects for all other code used. Additionally, AlliedModders LLC grants
ds@1952: * this exception to all derivative works. AlliedModders LLC defines further
ds@1952: * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
ds@1952: * or .
ds@1952: *
ds@1952: * Version: $Id$
ds@1952: */
ds@1952:
ds@1952: #include "Database.h"
ds@1952: #include "HandleSys.h"
ds@1952: #include "ShareSys.h"
ds@1952: #include "sourcemod.h"
ds@1952: #include "sm_stringutil.h"
ds@1952: #include "Logger.h"
ds@1952: #include "ExtensionSys.h"
ds@1952: #include
ds@1952: #include "ThreadSupport.h"
ds@1952:
ds@1952: #define DBPARSE_LEVEL_NONE 0
ds@1952: #define DBPARSE_LEVEL_MAIN 1
ds@1952: #define DBPARSE_LEVEL_DATABASE 2
ds@1952:
ds@1952: DBManager g_DBMan;
ds@1952: static bool s_OneTimeThreaderErrorMsg = false;
ds@1952:
ds@1952: DBManager::DBManager()
ds@1952: : m_StrTab(512), m_ParseLevel(0), m_ParseState(0), m_pDefault(NULL)
ds@1952: {
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnSourceModAllInitialized()
ds@1952: {
ds@1952: HandleAccess sec;
ds@1952:
ds@1952: g_HandleSys.InitAccessDefaults(NULL, &sec);
ds@1952: sec.access[HandleAccess_Delete] |= HANDLE_RESTRICT_IDENTITY;
ds@1952: sec.access[HandleAccess_Clone] |= HANDLE_RESTRICT_IDENTITY;
ds@1952:
ds@1952: m_DriverType = g_HandleSys.CreateType("IDriver", this, 0, NULL, &sec, g_pCoreIdent, NULL);
ds@1952: m_DatabaseType = g_HandleSys.CreateType("IDatabase", this, 0, NULL, NULL, g_pCoreIdent, NULL);
ds@1952:
ds@1952: g_ShareSys.AddInterface(NULL, this);
ds@1952:
ds@1952: g_SourceMod.BuildPath(Path_SM, m_Filename, sizeof(m_Filename), "configs/databases.cfg");
ds@1952:
ds@1952: m_pConfigLock = g_pThreader->MakeMutex();
ds@1952: m_pThinkLock = g_pThreader->MakeMutex();
ds@1952: m_pQueueLock = g_pThreader->MakeMutex();
ds@1952:
ds@1952: g_PluginSys.AddPluginsListener(this);
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnSourceModLevelChange(const char *mapName)
ds@1952: {
ds@1952: SMCError err;
ds@1952: SMCStates states = {0, 0};
ds@1952:
ds@1952: /* We lock and don't give up the lock until we're done.
ds@1952: * This way the thread's search won't be searching through a
ds@1952: * potentially empty/corrupt list, which would be very bad.
ds@1952: */
ds@1952: m_pConfigLock->Lock();
ds@1952: if ((err = textparsers->ParseFile_SMC(m_Filename, this, &states)) != SMCError_Okay)
ds@1952: {
ds@1952: g_Logger.LogError("[SM] Detected parse error(s) in file \"%s\"", m_Filename);
ds@1952: if (err != SMCError_Custom)
ds@1952: {
ds@1952: const char *txt = textparsers->GetSMCErrorString(err);
ds@1952: g_Logger.LogError("[SM] Line %d: %s", states.line, txt);
ds@1952: }
ds@1952: }
ds@1952: m_pConfigLock->Unlock();
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnSourceModShutdown()
ds@1952: {
ds@1952: KillWorkerThread();
ds@1952: g_PluginSys.RemovePluginsListener(this);
ds@1952: m_pConfigLock->DestroyThis();
ds@1952: m_pThinkLock->DestroyThis();
ds@1952: m_pQueueLock->DestroyThis();
ds@1952: g_HandleSys.RemoveType(m_DatabaseType, g_pCoreIdent);
ds@1952: g_HandleSys.RemoveType(m_DriverType, g_pCoreIdent);
ds@1952: }
ds@1952:
ds@1952: unsigned int DBManager::GetInterfaceVersion()
ds@1952: {
ds@1952: return SMINTERFACE_DBI_VERSION;
ds@1952: }
ds@1952:
ds@1952: const char *DBManager::GetInterfaceName()
ds@1952: {
ds@1952: return SMINTERFACE_DBI_NAME;
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnHandleDestroy(HandleType_t type, void *object)
ds@1952: {
ds@1952: if (type == m_DriverType)
ds@1952: {
ds@1952: /* Ignore */
ds@1952: return;
ds@1952: }
ds@1952:
ds@1952: if (g_HandleSys.TypeCheck(type, m_DatabaseType))
ds@1952: {
ds@1952: IDatabase *pdb = (IDatabase *)object;
ds@1952: pdb->Close();
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: void DBManager::ReadSMC_ParseStart()
ds@1952: {
ds@1952: m_confs.clear();
ds@1952: m_ParseLevel = 0;
ds@1952: m_ParseState = DBPARSE_LEVEL_NONE;
ds@1952: m_StrTab.Reset();
ds@1952: m_DefDriver.clear();
ds@1952: }
ds@1952:
ds@1952: ConfDbInfo s_CurInfo;
ds@1952: SMCResult DBManager::ReadSMC_NewSection(const SMCStates *states, const char *name)
ds@1952: {
ds@1952: if (m_ParseLevel)
ds@1952: {
ds@1952: m_ParseLevel++;
ds@1952: return SMCResult_Continue;
ds@1952: }
ds@1952:
ds@1952: if (m_ParseState == DBPARSE_LEVEL_NONE)
ds@1952: {
ds@1952: if (strcmp(name, "Databases") == 0)
ds@1952: {
ds@1952: m_ParseState = DBPARSE_LEVEL_MAIN;
ds@1952: } else {
ds@1952: m_ParseLevel++;
ds@1952: }
ds@1952: } else if (m_ParseState == DBPARSE_LEVEL_MAIN) {
ds@1952: s_CurInfo = ConfDbInfo();
ds@1952: s_CurInfo.name = m_StrTab.AddString(name);
ds@1952: m_ParseState = DBPARSE_LEVEL_DATABASE;
ds@1952: } else if (m_ParseState == DBPARSE_LEVEL_DATABASE) {
ds@1952: m_ParseLevel++;
ds@1952: }
ds@1952:
ds@1952: return SMCResult_Continue;
ds@1952: }
ds@1952:
ds@1952: SMCResult DBManager::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
ds@1952: {
ds@1952: if (m_ParseLevel)
ds@1952: {
ds@1952: return SMCResult_Continue;
ds@1952: }
ds@1952:
ds@1952: if (m_ParseState == DBPARSE_LEVEL_MAIN)
ds@1952: {
ds@1952: if (strcmp(key, "driver_default") == 0)
ds@1952: {
ds@1952: m_DefDriver.assign(value);
ds@1952: }
ds@1952: } else if (m_ParseState == DBPARSE_LEVEL_DATABASE) {
ds@1952: if (strcmp(key, "driver") == 0)
ds@1952: {
ds@1952: if (strcmp(value, "default") != 0)
ds@1952: {
ds@1952: s_CurInfo.driver = m_StrTab.AddString(value);
ds@1952: }
ds@1952: } else if (strcmp(key, "database") == 0) {
ds@1952: s_CurInfo.database = m_StrTab.AddString(value);
ds@1952: } else if (strcmp(key, "host") == 0) {
ds@1952: s_CurInfo.host = m_StrTab.AddString(value);
ds@1952: } else if (strcmp(key, "user") == 0) {
ds@1952: s_CurInfo.user = m_StrTab.AddString(value);
ds@1952: } else if (strcmp(key, "pass") == 0) {
ds@1952: s_CurInfo.pass = m_StrTab.AddString(value);
ds@1952: } else if (strcmp(key, "timeout") == 0) {
ds@1952: s_CurInfo.info.maxTimeout = atoi(value);
ds@1952: } else if (strcmp(key, "port") == 0) {
ds@1952: s_CurInfo.info.port = atoi(value);
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: return SMCResult_Continue;
ds@1952: }
ds@1952:
ds@1952: #define ASSIGN_VAR(var) \
ds@1952: if (s_CurInfo.var == -1) { \
ds@1952: s_CurInfo.info.var = ""; \
ds@1952: } else { \
ds@1952: s_CurInfo.info.var = m_StrTab.GetString(s_CurInfo.var); \
ds@1952: }
ds@1952:
ds@1952: SMCResult DBManager::ReadSMC_LeavingSection(const SMCStates *states)
ds@1952: {
ds@1952: if (m_ParseLevel)
ds@1952: {
ds@1952: m_ParseLevel--;
ds@1952: return SMCResult_Continue;
ds@1952: }
ds@1952:
ds@1952: if (m_ParseState == DBPARSE_LEVEL_DATABASE)
ds@1952: {
ds@1952: /* Set all of the info members to either a blank string
ds@1952: * or the string pointer from the string table.
ds@1952: */
ds@1952: ASSIGN_VAR(driver);
ds@1952: ASSIGN_VAR(database);
ds@1952: ASSIGN_VAR(host);
ds@1952: ASSIGN_VAR(user);
ds@1952: ASSIGN_VAR(pass);
ds@1952:
ds@1952: /* Save it.. */
ds@1952: m_confs.push_back(s_CurInfo);
ds@1952:
ds@1952: /* Go up one level */
ds@1952: m_ParseState = DBPARSE_LEVEL_MAIN;
ds@1952: } else if (m_ParseState == DBPARSE_LEVEL_MAIN) {
ds@1952: m_ParseState = DBPARSE_LEVEL_NONE;
ds@1952: return SMCResult_Halt;
ds@1952: }
ds@1952:
ds@1952: return SMCResult_Continue;
ds@1952: }
ds@1952: #undef ASSIGN_VAR
ds@1952:
ds@1952: void DBManager::ReadSMC_ParseEnd(bool halted, bool failed)
ds@1952: {
ds@1952: }
ds@1952:
ds@1952: bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength)
ds@1952: {
ds@1952: ConfDbInfo *pInfo = GetDatabaseConf(name);
ds@1952:
ds@1952: if (!pInfo)
ds@1952: {
ds@1952: if (pdr)
ds@1952: {
ds@1952: *pdr = NULL;
ds@1952: }
ds@1952: *pdb = NULL;
ds@1952: UTIL_Format(error, maxlength, "Configuration \"%s\" not found", name);
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: const char *dname = pInfo->info.driver;
ds@1952: if (!pInfo->realDriver)
ds@1952: {
ds@1952: /* Try to assign a real driver pointer */
ds@1952: if (pInfo->info.driver[0] == '\0')
ds@1952: {
ds@1952: if (!m_pDefault && m_DefDriver.size() > 0)
ds@1952: {
ds@1952: m_pDefault = FindOrLoadDriver(m_DefDriver.c_str());
ds@1952: }
ds@1952: dname = m_DefDriver.size() ? m_DefDriver.c_str() : "default";
ds@1952: pInfo->realDriver = m_pDefault;
ds@1952: } else {
ds@1952: pInfo->realDriver = FindOrLoadDriver(pInfo->info.driver);
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: if (pInfo->realDriver)
ds@1952: {
ds@1952: if (pdr)
ds@1952: {
ds@1952: *pdr = pInfo->realDriver;
ds@1952: }
ds@1952: *pdb = pInfo->realDriver->Connect(&pInfo->info, persistent, error, maxlength);
ds@1952: return (*pdb != NULL);
ds@1952: }
ds@1952:
ds@1952: if (pdr)
ds@1952: {
ds@1952: *pdr = NULL;
ds@1952: }
ds@1952: *pdb = NULL;
ds@1952:
ds@1952: UTIL_Format(error, maxlength, "Driver \"%s\" not found", dname);
ds@1952:
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: void DBManager::AddDriver(IDBDriver *pDriver)
ds@1952: {
ds@1952: /* Let's kill the worker. Join the thread and let the queries flush.
ds@1952: * This is kind of stupid but we just want to unload safely.
ds@1952: * Rather than recreate the worker, we'll wait until someone throws
ds@1952: * another query through.
ds@1952: */
ds@1952: KillWorkerThread();
ds@1952:
ds@1952: m_drivers.push_back(pDriver);
ds@1952: }
ds@1952:
ds@1952: void DBManager::RemoveDriver(IDBDriver *pDriver)
ds@1952: {
ds@1952: /* Again, we're forced to kill the worker. How rude!
ds@1952: * Doing this flushes the queue, and thus we don't need to
ds@1952: * clean anything else.
ds@1952: */
ds@1952: KillWorkerThread();
ds@1952:
ds@1952: for (size_t i=0; i::iterator iter;
ds@1952: for (iter=m_confs.begin(); iter!=m_confs.end(); iter++)
ds@1952: {
ds@1952: ConfDbInfo &db = (*iter);
ds@1952: if (db.realDriver == pDriver)
ds@1952: {
ds@1952: db.realDriver = NULL;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: /* Now that the driver is gone, we have to test the think queue.
ds@1952: * Whatever happens therein is up to the db op!
ds@1952: */
ds@1952: Queue::iterator qiter = m_ThinkQueue.begin();
ds@1952: Queue templist;
ds@1952: while (qiter != m_ThinkQueue.end())
ds@1952: {
ds@1952: IDBThreadOperation *op = (*qiter);
ds@1952: if (op->GetDriver() == pDriver)
ds@1952: {
ds@1952: templist.push(op);
ds@1952: qiter = m_ThinkQueue.erase(qiter);
ds@1952: } else {
ds@1952: qiter++;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: for (qiter = templist.begin();
ds@1952: qiter != templist.end();
ds@1952: qiter++)
ds@1952: {
ds@1952: IDBThreadOperation *op = (*qiter);
ds@1952: op->CancelThinkPart();
ds@1952: op->Destroy();
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: IDBDriver *DBManager::GetDefaultDriver()
ds@1952: {
ds@1952: if (!m_pDefault && m_DefDriver.size() > 0)
ds@1952: {
ds@1952: m_pDefault = FindOrLoadDriver(m_DefDriver.c_str());
ds@1952: }
ds@1952:
ds@1952: return m_pDefault;
ds@1952: }
ds@1952:
ds@1952: Handle_t DBManager::CreateHandle(DBHandleType dtype, void *ptr, IdentityToken_t *pToken)
ds@1952: {
ds@1952: HandleType_t type = 0;
ds@1952:
ds@1952: if (dtype == DBHandle_Driver)
ds@1952: {
ds@1952: type = m_DriverType;
ds@1952: } else if (dtype == DBHandle_Database) {
ds@1952: type = m_DatabaseType;
ds@1952: } else {
ds@1952: return BAD_HANDLE;
ds@1952: }
ds@1952:
ds@1952: return g_HandleSys.CreateHandle(type, ptr, pToken, g_pCoreIdent, NULL);
ds@1952: }
ds@1952:
ds@1952: HandleError DBManager::ReadHandle(Handle_t hndl, DBHandleType dtype, void **ptr)
ds@1952: {
ds@1952: HandleType_t type = 0;
ds@1952:
ds@1952: if (dtype == DBHandle_Driver)
ds@1952: {
ds@1952: type = m_DriverType;
ds@1952: } else if (dtype == DBHandle_Database) {
ds@1952: type = m_DatabaseType;
ds@1952: } else {
ds@1952: return HandleError_Type;
ds@1952: }
ds@1952:
ds@1952: HandleSecurity sec(NULL, g_pCoreIdent);
ds@1952:
ds@1952: return g_HandleSys.ReadHandle(hndl, type, &sec, ptr);
ds@1952: }
ds@1952:
ds@1952: HandleError DBManager::ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token)
ds@1952: {
ds@1952: HandleSecurity sec(token, g_pCoreIdent);
ds@1952: return g_HandleSys.FreeHandle(hndl, &sec);
ds@1952: }
ds@1952:
ds@1952: unsigned int DBManager::GetDriverCount()
ds@1952: {
ds@1952: return (unsigned int)m_drivers.size();
ds@1952: }
ds@1952:
ds@1952: IDBDriver *DBManager::GetDriver(unsigned int index)
ds@1952: {
ds@1952: if (index >= m_drivers.size())
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: return m_drivers[index];
ds@1952: }
ds@1952:
ds@1952: const DatabaseInfo *DBManager::FindDatabaseConf(const char *name)
ds@1952: {
ds@1952: ConfDbInfo *info = GetDatabaseConf(name);
ds@1952: if (!info)
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: return &info->info;
ds@1952: }
ds@1952:
ds@1952: ConfDbInfo *DBManager::GetDatabaseConf(const char *name)
ds@1952: {
ds@1952: List::iterator iter;
ds@1952:
ds@1952: for (iter=m_confs.begin(); iter!=m_confs.end(); iter++)
ds@1952: {
ds@1952: ConfDbInfo &conf = (*iter);
ds@1952: if (strcmp(m_StrTab.GetString(conf.name), name) == 0)
ds@1952: {
ds@1952: return &conf;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: IDBDriver *DBManager::FindOrLoadDriver(const char *name)
ds@1952: {
ds@1952: size_t last_size = m_drivers.size();
ds@1952: for (size_t i=0; iGetIdentifier(), name) == 0)
ds@1952: {
ds@1952: return m_drivers[i];
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: char filename[PLATFORM_MAX_PATH];
ds@1952: UTIL_Format(filename, sizeof(filename), "dbi.%s.ext", name);
ds@1952:
ds@1952: IExtension *pExt = g_Extensions.LoadAutoExtension(filename);
ds@1952: if (!pExt || !pExt->IsLoaded() || m_drivers.size() <= last_size)
ds@1952: {
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: /* last_size is now gauranteed to be a valid index.
ds@1952: * The identifier must match the name.
ds@1952: */
ds@1952: if (strcmp(m_drivers[last_size]->GetIdentifier(), name) == 0)
ds@1952: {
ds@1952: return m_drivers[last_size];
ds@1952: }
ds@1952:
ds@1952: return NULL;
ds@1952: }
ds@1952:
ds@1952: void DBManager::KillWorkerThread()
ds@1952: {
ds@1952: if (m_pWorker)
ds@1952: {
ds@1952: m_pWorker->Stop(false);
ds@1952: g_pThreader->DestroyWorker(m_pWorker);
ds@1952: m_pWorker = NULL;
ds@1952: s_OneTimeThreaderErrorMsg = false;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: static IdentityToken_t *s_pAddBlock = NULL;
ds@1952:
ds@1952: bool DBManager::AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio)
ds@1952: {
ds@1952: if (s_pAddBlock && op->GetOwner() == s_pAddBlock)
ds@1952: {
ds@1952: return false;
ds@1952: }
ds@1952:
ds@1952: if (!m_pWorker)
ds@1952: {
ds@1952: m_pWorker = g_pThreader->MakeWorker(this, true);
ds@1952: if (!m_pWorker)
ds@1952: {
ds@1952: if (!s_OneTimeThreaderErrorMsg)
ds@1952: {
ds@1952: g_Logger.LogError("[SM] Unable to create db threader (error unknown)");
ds@1952: s_OneTimeThreaderErrorMsg = true;
ds@1952: }
ds@1952: return false;
ds@1952: }
ds@1952: if (!m_pWorker->Start())
ds@1952: {
ds@1952: if (!s_OneTimeThreaderErrorMsg)
ds@1952: {
ds@1952: g_Logger.LogError("[SM] Unable to start db threader (error unknown)");
ds@1952: s_OneTimeThreaderErrorMsg = true;
ds@1952: }
ds@1952: g_pThreader->DestroyWorker(m_pWorker);
ds@1952: m_pWorker = NULL;
ds@1952: return false;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: /* Add to the queue */
ds@1952: {
ds@1952: m_pQueueLock->Lock();
ds@1952: Queue &queue = m_OpQueue.GetQueue(prio);
ds@1952: queue.push(op);
ds@1952: m_pQueueLock->Unlock();
ds@1952: }
ds@1952:
ds@1952: /* Make the thread */
ds@1952: m_pWorker->MakeThread(this);
ds@1952:
ds@1952: return true;
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnWorkerStart(IThreadWorker *pWorker)
ds@1952: {
ds@1952: m_drSafety.clear();
ds@1952: for (size_t i=0; iIsThreadSafe())
ds@1952: {
ds@1952: m_drSafety.push_back(m_drivers[i]->InitializeThreadSafety());
ds@1952: } else {
ds@1952: m_drSafety.push_back(false);
ds@1952: }
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnWorkerStop(IThreadWorker *pWorker)
ds@1952: {
ds@1952: for (size_t i=0; iShutdownThreadSafety();
ds@1952: }
ds@1952: }
ds@1952: m_drSafety.clear();
ds@1952: }
ds@1952:
ds@1952: void DBManager::RunThread(IThreadHandle *pThread)
ds@1952: {
ds@1952: IDBThreadOperation *op = NULL;
ds@1952:
ds@1952: /* Get something from the queue */
ds@1952: {
ds@1952: m_pQueueLock->Lock();
ds@1952: Queue &queue = m_OpQueue.GetLikelyQueue();
ds@1952: if (!queue.empty())
ds@1952: {
ds@1952: op = queue.first();
ds@1952: queue.pop();
ds@1952: }
ds@1952: m_pQueueLock->Unlock();
ds@1952: }
ds@1952:
ds@1952: /* whoa. hi. did we get something? we should have. */
ds@1952: if (!op)
ds@1952: {
ds@1952: /* wtf? */
ds@1952: return;
ds@1952: }
ds@1952:
ds@1952: op->RunThreadPart();
ds@1952:
ds@1952: m_pThinkLock->Lock();
ds@1952: m_ThinkQueue.push(op);
ds@1952: m_pThinkLock->Unlock();
ds@1952: }
ds@1952:
ds@1952: void DBManager::RunFrame()
ds@1952: {
ds@1952: /* Don't bother if we're empty */
ds@1952: if (!m_ThinkQueue.size())
ds@1952: {
ds@1952: return;
ds@1952: }
ds@1952:
ds@1952: /* Dump one thing per-frame so the server stays sane. */
ds@1952: m_pThinkLock->Lock();
ds@1952: IDBThreadOperation *op = m_ThinkQueue.first();
ds@1952: m_ThinkQueue.pop();
ds@1952: m_pThinkLock->Unlock();
ds@1952: op->RunThinkPart();
ds@1952: op->Destroy();
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnTerminate(IThreadHandle *pThread, bool cancel)
ds@1952: {
ds@1952: /* Do nothing */
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnSourceModIdentityDropped(IdentityToken_t *pToken)
ds@1952: {
ds@1952: s_pAddBlock = pToken;
ds@1952:
ds@1952: /* Kill the thread so we can flush everything into the think queue... */
ds@1952: KillWorkerThread();
ds@1952:
ds@1952: /* Run all of the think operations.
ds@1952: * Unlike the driver unloading example, we'll let these calls go through,
ds@1952: * since a plugin unloading is far more normal.
ds@1952: */
ds@1952: Queue::iterator iter = m_ThinkQueue.begin();
ds@1952: Queue templist;
ds@1952: while (iter != m_ThinkQueue.end())
ds@1952: {
ds@1952: IDBThreadOperation *op = (*iter);
ds@1952: if (op->GetOwner() == pToken)
ds@1952: {
ds@1952: templist.push(op);
ds@1952: iter = m_ThinkQueue.erase(iter);
ds@1952: } else {
ds@1952: iter++;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: for (iter = templist.begin();
ds@1952: iter != templist.end();
ds@1952: iter++)
ds@1952: {
ds@1952: IDBThreadOperation *op = (*iter);
ds@1952: op->RunThinkPart();
ds@1952: op->Destroy();
ds@1952: }
ds@1952:
ds@1952: s_pAddBlock = NULL;
ds@1952: }
ds@1952:
ds@1952: void DBManager::OnPluginUnloaded(IPlugin *plugin)
ds@1952: {
ds@1952: /* Kill the thread so we can flush everything into the think queue... */
ds@1952: KillWorkerThread();
ds@1952:
ds@1952: /* Mark the plugin as being unloaded so future database calls will ignore threading... */
ds@1952: plugin->SetProperty("DisallowDBThreads", NULL);
ds@1952:
ds@1952: /* Run all of the think operations.
ds@1952: * Unlike the driver unloading example, we'll let these calls go through,
ds@1952: * since a plugin unloading is far more normal.
ds@1952: */
ds@1952: Queue::iterator iter = m_ThinkQueue.begin();
ds@1952: Queue templist;
ds@1952: while (iter != m_ThinkQueue.end())
ds@1952: {
ds@1952: IDBThreadOperation *op = (*iter);
ds@1952: if (op->GetOwner() == plugin->GetIdentity())
ds@1952: {
ds@1952: templist.push(op);
ds@1952: iter = m_ThinkQueue.erase(iter);
ds@1952: } else {
ds@1952: iter++;
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: for (iter = templist.begin();
ds@1952: iter != templist.end();
ds@1952: iter++)
ds@1952: {
ds@1952: IDBThreadOperation *op = (*iter);
ds@1952: op->RunThinkPart();
ds@1952: op->Destroy();
ds@1952: }
ds@1952: }
ds@1952:
ds@1952: void DBManager::LockConfig()
ds@1952: {
ds@1952: m_pConfigLock->Lock();
ds@1952: }
ds@1952:
ds@1952: void DBManager::UnlockConfig()
ds@1952: {
ds@1952: m_pConfigLock->Unlock();
ds@1952: }
ds@1952:
ds@1952: const char *DBManager::GetDefaultDriverName()
ds@1952: {
ds@1952: return m_DefDriver.c_str();
ds@1952: }