1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/logic/Database.cpp Wed Oct 09 08:43:08 2013 -0400
1.3 @@ -0,0 +1,733 @@
1.4 +/**
1.5 + * vim: set ts=4 sw=4 tw=99 noet :
1.6 + * =============================================================================
1.7 + * SourceMod
1.8 + * Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
1.9 + * =============================================================================
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify it under
1.12 + * the terms of the GNU General Public License, version 3.0, as published by the
1.13 + * Free Software Foundation.
1.14 + *
1.15 + * This program is distributed in the hope that it will be useful, but WITHOUT
1.16 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
1.18 + * details.
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License along with
1.21 + * this program. If not, see <http://www.gnu.org/licenses/>.
1.22 + *
1.23 + * As a special exception, AlliedModders LLC gives you permission to link the
1.24 + * code of this program (as well as its derivative works) to "Half-Life 2," the
1.25 + * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
1.26 + * by the Valve Corporation. You must obey the GNU General Public License in
1.27 + * all respects for all other code used. Additionally, AlliedModders LLC grants
1.28 + * this exception to all derivative works. AlliedModders LLC defines further
1.29 + * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
1.30 + * or <http://www.sourcemod.net/license.php>.
1.31 + *
1.32 + * Version: $Id$
1.33 + */
1.34 +
1.35 +#include "Database.h"
1.36 +#include "ISourceMod.h"
1.37 +#include "HandleSys.h"
1.38 +#include "ExtensionSys.h"
1.39 +#include "PluginSys.h"
1.40 +#include <stdlib.h>
1.41 +#include <IThreader.h>
1.42 +
1.43 +#define DBPARSE_LEVEL_NONE 0
1.44 +#define DBPARSE_LEVEL_MAIN 1
1.45 +#define DBPARSE_LEVEL_DATABASE 2
1.46 +
1.47 +DBManager g_DBMan;
1.48 +static bool s_OneTimeThreaderErrorMsg = false;
1.49 +
1.50 +DBManager::DBManager()
1.51 + : m_Terminate(false),
1.52 + m_ParseLevel(0),
1.53 + m_ParseState(0),
1.54 + m_pDefault(NULL)
1.55 +{
1.56 +}
1.57 +
1.58 +static void FrameHook(bool simulating)
1.59 +{
1.60 + g_DBMan.RunFrame();
1.61 +}
1.62 +
1.63 +void DBManager::OnSourceModAllInitialized()
1.64 +{
1.65 + HandleAccess sec;
1.66 +
1.67 + g_HandleSys.InitAccessDefaults(NULL, &sec);
1.68 + sec.access[HandleAccess_Delete] |= HANDLE_RESTRICT_IDENTITY;
1.69 + sec.access[HandleAccess_Clone] |= HANDLE_RESTRICT_IDENTITY;
1.70 +
1.71 + m_DriverType = g_HandleSys.CreateType("IDriver", this, 0, NULL, &sec, g_pCoreIdent, NULL);
1.72 + m_DatabaseType = g_HandleSys.CreateType("IDatabase", this, 0, NULL, NULL, g_pCoreIdent, NULL);
1.73 +
1.74 + g_ShareSys.AddInterface(NULL, this);
1.75 +
1.76 + g_pSM->BuildPath(Path_SM, m_Filename, sizeof(m_Filename), "configs/databases.cfg");
1.77 +
1.78 + g_PluginSys.AddPluginsListener(this);
1.79 +
1.80 + g_pSM->AddGameFrameHook(&FrameHook);
1.81 +}
1.82 +
1.83 +void DBManager::OnSourceModLevelChange(const char *mapName)
1.84 +{
1.85 + SMCError err;
1.86 + SMCStates states = {0, 0};
1.87 +
1.88 + /* We lock and don't give up the lock until we're done.
1.89 + * This way the thread's search won't be searching through a
1.90 + * potentially empty/corrupt list, which would be very bad.
1.91 + */
1.92 + ke::AutoLock lock(&m_ConfigLock);
1.93 + if ((err = textparsers->ParseFile_SMC(m_Filename, this, &states)) != SMCError_Okay)
1.94 + {
1.95 + smcore.LogError("[SM] Detected parse error(s) in file \"%s\"", m_Filename);
1.96 + if (err != SMCError_Custom)
1.97 + {
1.98 + const char *txt = textparsers->GetSMCErrorString(err);
1.99 + smcore.LogError("[SM] Line %d: %s", states.line, txt);
1.100 + }
1.101 + }
1.102 +}
1.103 +
1.104 +void DBManager::OnSourceModShutdown()
1.105 +{
1.106 + g_pSM->RemoveGameFrameHook(&FrameHook);
1.107 + KillWorkerThread();
1.108 + g_PluginSys.RemovePluginsListener(this);
1.109 + g_HandleSys.RemoveType(m_DatabaseType, g_pCoreIdent);
1.110 + g_HandleSys.RemoveType(m_DriverType, g_pCoreIdent);
1.111 + ClearConfigs();
1.112 +}
1.113 +
1.114 +unsigned int DBManager::GetInterfaceVersion()
1.115 +{
1.116 + return SMINTERFACE_DBI_VERSION;
1.117 +}
1.118 +
1.119 +const char *DBManager::GetInterfaceName()
1.120 +{
1.121 + return SMINTERFACE_DBI_NAME;
1.122 +}
1.123 +
1.124 +void DBManager::OnHandleDestroy(HandleType_t type, void *object)
1.125 +{
1.126 + if (type == m_DriverType)
1.127 + {
1.128 + /* Ignore */
1.129 + return;
1.130 + }
1.131 +
1.132 + if (g_HandleSys.TypeCheck(type, m_DatabaseType))
1.133 + {
1.134 + IDatabase *pdb = (IDatabase *)object;
1.135 + pdb->Close();
1.136 + }
1.137 +}
1.138 +
1.139 +void DBManager::ReadSMC_ParseStart()
1.140 +{
1.141 + ClearConfigs();
1.142 + m_ParseLevel = 0;
1.143 + m_ParseState = DBPARSE_LEVEL_NONE;
1.144 + m_DefDriver.clear();
1.145 +}
1.146 +
1.147 +void DBManager::ClearConfigs()
1.148 +{
1.149 + List<ConfDbInfo *>::iterator iter;
1.150 + for (iter=m_confs.begin(); iter!=m_confs.end(); iter++)
1.151 + delete (*iter);
1.152 + m_confs.clear();
1.153 +}
1.154 +
1.155 +ConfDbInfo s_CurInfo;
1.156 +SMCResult DBManager::ReadSMC_NewSection(const SMCStates *states, const char *name)
1.157 +{
1.158 + if (m_ParseLevel)
1.159 + {
1.160 + m_ParseLevel++;
1.161 + return SMCResult_Continue;
1.162 + }
1.163 +
1.164 + if (m_ParseState == DBPARSE_LEVEL_NONE)
1.165 + {
1.166 + if (strcmp(name, "Databases") == 0)
1.167 + {
1.168 + m_ParseState = DBPARSE_LEVEL_MAIN;
1.169 + } else {
1.170 + m_ParseLevel++;
1.171 + }
1.172 + } else if (m_ParseState == DBPARSE_LEVEL_MAIN) {
1.173 + s_CurInfo = ConfDbInfo();
1.174 + s_CurInfo.name = name;
1.175 + m_ParseState = DBPARSE_LEVEL_DATABASE;
1.176 + } else if (m_ParseState == DBPARSE_LEVEL_DATABASE) {
1.177 + m_ParseLevel++;
1.178 + }
1.179 +
1.180 + return SMCResult_Continue;
1.181 +}
1.182 +
1.183 +SMCResult DBManager::ReadSMC_KeyValue(const SMCStates *states, const char *key, const char *value)
1.184 +{
1.185 + if (m_ParseLevel)
1.186 + {
1.187 + return SMCResult_Continue;
1.188 + }
1.189 +
1.190 + if (m_ParseState == DBPARSE_LEVEL_MAIN)
1.191 + {
1.192 + if (strcmp(key, "driver_default") == 0)
1.193 + {
1.194 + m_DefDriver.assign(value);
1.195 + }
1.196 + } else if (m_ParseState == DBPARSE_LEVEL_DATABASE) {
1.197 + if (strcmp(key, "driver") == 0)
1.198 + {
1.199 + if (strcmp(value, "default") != 0)
1.200 + {
1.201 + s_CurInfo.driver = value;
1.202 + }
1.203 + } else if (strcmp(key, "database") == 0) {
1.204 + s_CurInfo.database = value;
1.205 + } else if (strcmp(key, "host") == 0) {
1.206 + s_CurInfo.host = value;
1.207 + } else if (strcmp(key, "user") == 0) {
1.208 + s_CurInfo.user = value;
1.209 + } else if (strcmp(key, "pass") == 0) {
1.210 + s_CurInfo.pass = value;
1.211 + } else if (strcmp(key, "timeout") == 0) {
1.212 + s_CurInfo.info.maxTimeout = atoi(value);
1.213 + } else if (strcmp(key, "port") == 0) {
1.214 + s_CurInfo.info.port = atoi(value);
1.215 + }
1.216 + }
1.217 +
1.218 + return SMCResult_Continue;
1.219 +}
1.220 +
1.221 +SMCResult DBManager::ReadSMC_LeavingSection(const SMCStates *states)
1.222 +{
1.223 + if (m_ParseLevel)
1.224 + {
1.225 + m_ParseLevel--;
1.226 + return SMCResult_Continue;
1.227 + }
1.228 +
1.229 + if (m_ParseState == DBPARSE_LEVEL_DATABASE)
1.230 + {
1.231 + ConfDbInfo *cdb = new ConfDbInfo();
1.232 +
1.233 + cdb->name = s_CurInfo.name;
1.234 + cdb->driver = s_CurInfo.driver;
1.235 + cdb->host = s_CurInfo.host;
1.236 + cdb->user = s_CurInfo.user;
1.237 + cdb->pass = s_CurInfo.pass;
1.238 + cdb->database = s_CurInfo.database;
1.239 + cdb->realDriver = s_CurInfo.realDriver;
1.240 + cdb->info.maxTimeout = s_CurInfo.info.maxTimeout;
1.241 + cdb->info.port = s_CurInfo.info.port;
1.242 +
1.243 + cdb->info.driver = cdb->driver.c_str();
1.244 + cdb->info.database = cdb->database.c_str();
1.245 + cdb->info.host = cdb->host.c_str();
1.246 + cdb->info.user = cdb->user.c_str();
1.247 + cdb->info.pass = cdb->pass.c_str();
1.248 +
1.249 + /* Save it.. */
1.250 + m_confs.push_back(cdb);
1.251 +
1.252 + /* Go up one level */
1.253 + m_ParseState = DBPARSE_LEVEL_MAIN;
1.254 + } else if (m_ParseState == DBPARSE_LEVEL_MAIN) {
1.255 + m_ParseState = DBPARSE_LEVEL_NONE;
1.256 + return SMCResult_Halt;
1.257 + }
1.258 +
1.259 + return SMCResult_Continue;
1.260 +}
1.261 +
1.262 +void DBManager::ReadSMC_ParseEnd(bool halted, bool failed)
1.263 +{
1.264 +}
1.265 +
1.266 +bool DBManager::Connect(const char *name, IDBDriver **pdr, IDatabase **pdb, bool persistent, char *error, size_t maxlength)
1.267 +{
1.268 + ConfDbInfo *pInfo = GetDatabaseConf(name);
1.269 +
1.270 + if (!pInfo)
1.271 + {
1.272 + if (pdr)
1.273 + {
1.274 + *pdr = NULL;
1.275 + }
1.276 + *pdb = NULL;
1.277 + g_pSM->Format(error, maxlength, "Configuration \"%s\" not found", name);
1.278 + return false;
1.279 + }
1.280 +
1.281 + const char *dname = pInfo->info.driver;
1.282 + if (!pInfo->realDriver)
1.283 + {
1.284 + /* Try to assign a real driver pointer */
1.285 + if (pInfo->info.driver[0] == '\0')
1.286 + {
1.287 + if (!m_pDefault && m_DefDriver.size() > 0)
1.288 + {
1.289 + m_pDefault = FindOrLoadDriver(m_DefDriver.c_str());
1.290 + }
1.291 + dname = m_DefDriver.size() ? m_DefDriver.c_str() : "default";
1.292 + pInfo->realDriver = m_pDefault;
1.293 + } else {
1.294 + pInfo->realDriver = FindOrLoadDriver(pInfo->info.driver);
1.295 + }
1.296 + }
1.297 +
1.298 + if (pInfo->realDriver)
1.299 + {
1.300 + if (pdr)
1.301 + {
1.302 + *pdr = pInfo->realDriver;
1.303 + }
1.304 + *pdb = pInfo->realDriver->Connect(&pInfo->info, persistent, error, maxlength);
1.305 + return (*pdb != NULL);
1.306 + }
1.307 +
1.308 + if (pdr)
1.309 + {
1.310 + *pdr = NULL;
1.311 + }
1.312 + *pdb = NULL;
1.313 +
1.314 + g_pSM->Format(error, maxlength, "Driver \"%s\" not found", dname);
1.315 +
1.316 + return false;
1.317 +}
1.318 +
1.319 +void DBManager::AddDriver(IDBDriver *pDriver)
1.320 +{
1.321 + /* Let's kill the worker. Join the thread and let the queries flush.
1.322 + * This is kind of stupid but we just want to unload safely.
1.323 + * Rather than recreate the worker, we'll wait until someone throws
1.324 + * another query through.
1.325 + */
1.326 + KillWorkerThread();
1.327 +
1.328 + m_drivers.push_back(pDriver);
1.329 +}
1.330 +
1.331 +void DBManager::RemoveDriver(IDBDriver *pDriver)
1.332 +{
1.333 + /* Again, we're forced to kill the worker. How rude!
1.334 + * Doing this flushes the queue, and thus we don't need to
1.335 + * clean anything else.
1.336 + */
1.337 + KillWorkerThread();
1.338 +
1.339 + for (size_t i=0; i<m_drivers.size(); i++)
1.340 + {
1.341 + if (m_drivers[i] == pDriver)
1.342 + {
1.343 + m_drivers.erase(m_drivers.iterAt(i));
1.344 + break;
1.345 + }
1.346 + }
1.347 +
1.348 + /* Make sure NOTHING references this! */
1.349 + List<ConfDbInfo *>::iterator iter;
1.350 + for (iter=m_confs.begin(); iter!=m_confs.end(); iter++)
1.351 + {
1.352 + ConfDbInfo &db = *(*iter);
1.353 + if (db.realDriver == pDriver)
1.354 + {
1.355 + db.realDriver = NULL;
1.356 + }
1.357 + }
1.358 +
1.359 + /* Now that the driver is gone, we have to test the think queue.
1.360 + * Whatever happens therein is up to the db op!
1.361 + */
1.362 + Queue<IDBThreadOperation *>::iterator qiter = m_ThinkQueue.begin();
1.363 + Queue<IDBThreadOperation *> templist;
1.364 + while (qiter != m_ThinkQueue.end())
1.365 + {
1.366 + IDBThreadOperation *op = (*qiter);
1.367 + if (op->GetDriver() == pDriver)
1.368 + {
1.369 + templist.push(op);
1.370 + qiter = m_ThinkQueue.erase(qiter);
1.371 + } else {
1.372 + qiter++;
1.373 + }
1.374 + }
1.375 +
1.376 + for (qiter = templist.begin();
1.377 + qiter != templist.end();
1.378 + qiter++)
1.379 + {
1.380 + IDBThreadOperation *op = (*qiter);
1.381 + op->CancelThinkPart();
1.382 + op->Destroy();
1.383 + }
1.384 +}
1.385 +
1.386 +IDBDriver *DBManager::GetDefaultDriver()
1.387 +{
1.388 + if (!m_pDefault && m_DefDriver.size() > 0)
1.389 + {
1.390 + m_pDefault = FindOrLoadDriver(m_DefDriver.c_str());
1.391 + }
1.392 +
1.393 + return m_pDefault;
1.394 +}
1.395 +
1.396 +Handle_t DBManager::CreateHandle(DBHandleType dtype, void *ptr, IdentityToken_t *pToken)
1.397 +{
1.398 + HandleType_t type = 0;
1.399 +
1.400 + if (dtype == DBHandle_Driver)
1.401 + {
1.402 + type = m_DriverType;
1.403 + } else if (dtype == DBHandle_Database) {
1.404 + type = m_DatabaseType;
1.405 + } else {
1.406 + return BAD_HANDLE;
1.407 + }
1.408 +
1.409 + return g_HandleSys.CreateHandle(type, ptr, pToken, g_pCoreIdent, NULL);
1.410 +}
1.411 +
1.412 +HandleError DBManager::ReadHandle(Handle_t hndl, DBHandleType dtype, void **ptr)
1.413 +{
1.414 + HandleType_t type = 0;
1.415 +
1.416 + if (dtype == DBHandle_Driver)
1.417 + {
1.418 + type = m_DriverType;
1.419 + } else if (dtype == DBHandle_Database) {
1.420 + type = m_DatabaseType;
1.421 + } else {
1.422 + return HandleError_Type;
1.423 + }
1.424 +
1.425 + HandleSecurity sec(NULL, g_pCoreIdent);
1.426 +
1.427 + return g_HandleSys.ReadHandle(hndl, type, &sec, ptr);
1.428 +}
1.429 +
1.430 +HandleError DBManager::ReleaseHandle(Handle_t hndl, DBHandleType type, IdentityToken_t *token)
1.431 +{
1.432 + HandleSecurity sec(token, g_pCoreIdent);
1.433 + return g_HandleSys.FreeHandle(hndl, &sec);
1.434 +}
1.435 +
1.436 +unsigned int DBManager::GetDriverCount()
1.437 +{
1.438 + return (unsigned int)m_drivers.size();
1.439 +}
1.440 +
1.441 +IDBDriver *DBManager::GetDriver(unsigned int index)
1.442 +{
1.443 + if (index >= m_drivers.size())
1.444 + {
1.445 + return NULL;
1.446 + }
1.447 +
1.448 + return m_drivers[index];
1.449 +}
1.450 +
1.451 +const DatabaseInfo *DBManager::FindDatabaseConf(const char *name)
1.452 +{
1.453 + ConfDbInfo *info = GetDatabaseConf(name);
1.454 + if (!info)
1.455 + {
1.456 + return NULL;
1.457 + }
1.458 +
1.459 + return &info->info;
1.460 +}
1.461 +
1.462 +ConfDbInfo *DBManager::GetDatabaseConf(const char *name)
1.463 +{
1.464 + List<ConfDbInfo *>::iterator iter;
1.465 +
1.466 + for (iter=m_confs.begin(); iter!=m_confs.end(); iter++)
1.467 + {
1.468 + ConfDbInfo &conf = *(*iter);
1.469 + if (conf.name == name)
1.470 + {
1.471 + return &conf;
1.472 + }
1.473 + }
1.474 +
1.475 + return NULL;
1.476 +}
1.477 +
1.478 +IDBDriver *DBManager::FindOrLoadDriver(const char *name)
1.479 +{
1.480 + size_t last_size = m_drivers.size();
1.481 + for (size_t i=0; i<last_size; i++)
1.482 + {
1.483 + if (strcmp(m_drivers[i]->GetIdentifier(), name) == 0)
1.484 + {
1.485 + return m_drivers[i];
1.486 + }
1.487 + }
1.488 +
1.489 + char filename[PLATFORM_MAX_PATH];
1.490 + g_pSM->Format(filename, sizeof(filename), "dbi.%s.ext", name);
1.491 +
1.492 + IExtension *pExt = g_Extensions.LoadAutoExtension(filename);
1.493 + if (!pExt || !pExt->IsLoaded() || m_drivers.size() <= last_size)
1.494 + {
1.495 + return NULL;
1.496 + }
1.497 +
1.498 + /* last_size is now gauranteed to be a valid index.
1.499 + * The identifier must match the name.
1.500 + */
1.501 + if (strcmp(m_drivers[last_size]->GetIdentifier(), name) == 0)
1.502 + {
1.503 + return m_drivers[last_size];
1.504 + }
1.505 +
1.506 + return NULL;
1.507 +}
1.508 +
1.509 +void DBManager::KillWorkerThread()
1.510 +{
1.511 + if (m_Worker)
1.512 + {
1.513 + {
1.514 + ke::AutoLock lock(&m_QueueEvent);
1.515 + m_Terminate = true;
1.516 + m_QueueEvent.Notify();
1.517 + }
1.518 + m_Worker->Join();
1.519 + m_Worker = NULL;
1.520 + s_OneTimeThreaderErrorMsg = false;
1.521 + m_Terminate = false;
1.522 + }
1.523 +}
1.524 +
1.525 +static IdentityToken_t *s_pAddBlock = NULL;
1.526 +
1.527 +bool DBManager::AddToThreadQueue(IDBThreadOperation *op, PrioQueueLevel prio)
1.528 +{
1.529 + if (s_pAddBlock && op->GetOwner() == s_pAddBlock)
1.530 + {
1.531 + return false;
1.532 + }
1.533 +
1.534 + if (!m_Worker)
1.535 + {
1.536 + m_Worker = new ke::Thread(this, "SM SQL Worker");
1.537 + if (!m_Worker->Succeeded())
1.538 + {
1.539 + if (!s_OneTimeThreaderErrorMsg)
1.540 + {
1.541 + smcore.LogError("[SM] Unable to create db threader (error unknown)");
1.542 + s_OneTimeThreaderErrorMsg = true;
1.543 + }
1.544 + m_Worker = NULL;
1.545 + return false;
1.546 + }
1.547 + }
1.548 +
1.549 + /* Add to the queue */
1.550 + {
1.551 + ke::AutoLock lock(&m_QueueEvent);
1.552 + Queue<IDBThreadOperation *> &queue = m_OpQueue.GetQueue(prio);
1.553 + queue.push(op);
1.554 + m_QueueEvent.Notify();
1.555 + }
1.556 +
1.557 + return true;
1.558 +}
1.559 +
1.560 +void DBManager::Run()
1.561 +{
1.562 + // Initialize DB threadsafety.
1.563 + for (size_t i=0; i < m_drivers.size(); i++)
1.564 + {
1.565 + if (m_drivers[i]->IsThreadSafe())
1.566 + m_drSafety.push_back(m_drivers[i]->InitializeThreadSafety());
1.567 + else
1.568 + m_drSafety.push_back(false);
1.569 + }
1.570 +
1.571 + // Run actual worker thread logic.
1.572 + ThreadMain();
1.573 +
1.574 + // Shutdown DB threadsafety.
1.575 + for (size_t i=0; i<m_drivers.size(); i++)
1.576 + {
1.577 + if (m_drSafety[i])
1.578 + m_drivers[i]->ShutdownThreadSafety();
1.579 + }
1.580 + m_drSafety.clear();
1.581 +}
1.582 +
1.583 +void DBManager::ThreadMain()
1.584 +{
1.585 + ke::AutoLock lock(&m_QueueEvent);
1.586 +
1.587 + while (true) {
1.588 + // The lock has been acquired. Grab everything we can out of the
1.589 + // queue. Since we want to flush the queue even if we're terminated,
1.590 + // we process all operations we can before checking to terminate.
1.591 + // There's no risk of starvation since the main thread blocks on us
1.592 + // terminating.
1.593 + while (true)
1.594 + {
1.595 + Queue<IDBThreadOperation *> &queue = m_OpQueue.GetLikelyQueue();
1.596 + if (queue.empty())
1.597 + break;
1.598 +
1.599 + IDBThreadOperation *op = queue.first();
1.600 + queue.pop();
1.601 +
1.602 + // Unlock the queue when we run the query, so the main thread can
1.603 + // keep pumping events. We re-acquire the lock to check for more
1.604 + // items. It's okay if we terminate while unlocked; the main
1.605 + // thread would be blocked and we'd need to flush the queue
1.606 + // anyway, so after we've depleted the queue here, we'll just
1.607 + // reach the terminate at the top of the loop.
1.608 + {
1.609 + ke::AutoUnlock unlock(&m_QueueEvent);
1.610 + op->RunThreadPart();
1.611 +
1.612 + ke::AutoLock lock(&m_ThinkLock);
1.613 + m_ThinkQueue.push(op);
1.614 + }
1.615 + }
1.616 +
1.617 + if (m_Terminate)
1.618 + return;
1.619 +
1.620 + // Release the lock and wait for a signal.
1.621 + m_QueueEvent.Wait();
1.622 + }
1.623 +}
1.624 +
1.625 +void DBManager::RunFrame()
1.626 +{
1.627 + /* Don't bother if we're empty */
1.628 + if (!m_ThinkQueue.size())
1.629 + {
1.630 + return;
1.631 + }
1.632 +
1.633 + /* Dump one thing per-frame so the server stays sane. */
1.634 + IDBThreadOperation *op;
1.635 + {
1.636 + ke::AutoLock lock(&m_ThinkLock);
1.637 + op = m_ThinkQueue.first();
1.638 + m_ThinkQueue.pop();
1.639 + }
1.640 + op->RunThinkPart();
1.641 + op->Destroy();
1.642 +}
1.643 +
1.644 +void DBManager::OnSourceModIdentityDropped(IdentityToken_t *pToken)
1.645 +{
1.646 + s_pAddBlock = pToken;
1.647 +
1.648 + /* Kill the thread so we can flush everything into the think queue... */
1.649 + KillWorkerThread();
1.650 +
1.651 + /* Run all of the think operations.
1.652 + * Unlike the driver unloading example, we'll let these calls go through,
1.653 + * since a plugin unloading is far more normal.
1.654 + */
1.655 + Queue<IDBThreadOperation *>::iterator iter = m_ThinkQueue.begin();
1.656 + Queue<IDBThreadOperation *> templist;
1.657 + while (iter != m_ThinkQueue.end())
1.658 + {
1.659 + IDBThreadOperation *op = (*iter);
1.660 + if (op->GetOwner() == pToken)
1.661 + {
1.662 + templist.push(op);
1.663 + iter = m_ThinkQueue.erase(iter);
1.664 + } else {
1.665 + iter++;
1.666 + }
1.667 + }
1.668 +
1.669 + for (iter = templist.begin();
1.670 + iter != templist.end();
1.671 + iter++)
1.672 + {
1.673 + IDBThreadOperation *op = (*iter);
1.674 + op->RunThinkPart();
1.675 + op->Destroy();
1.676 + }
1.677 +
1.678 + s_pAddBlock = NULL;
1.679 +}
1.680 +
1.681 +void DBManager::OnPluginUnloaded(IPlugin *plugin)
1.682 +{
1.683 + /* Kill the thread so we can flush everything into the think queue... */
1.684 + KillWorkerThread();
1.685 +
1.686 + /* Mark the plugin as being unloaded so future database calls will ignore threading... */
1.687 + plugin->SetProperty("DisallowDBThreads", NULL);
1.688 +
1.689 + /* Run all of the think operations.
1.690 + * Unlike the driver unloading example, we'll let these calls go through,
1.691 + * since a plugin unloading is far more normal.
1.692 + */
1.693 + Queue<IDBThreadOperation *>::iterator iter = m_ThinkQueue.begin();
1.694 + Queue<IDBThreadOperation *> templist;
1.695 + while (iter != m_ThinkQueue.end())
1.696 + {
1.697 + IDBThreadOperation *op = (*iter);
1.698 + if (op->GetOwner() == plugin->GetIdentity())
1.699 + {
1.700 + templist.push(op);
1.701 + iter = m_ThinkQueue.erase(iter);
1.702 + } else {
1.703 + iter++;
1.704 + }
1.705 + }
1.706 +
1.707 + for (iter = templist.begin();
1.708 + iter != templist.end();
1.709 + iter++)
1.710 + {
1.711 + IDBThreadOperation *op = (*iter);
1.712 + op->RunThinkPart();
1.713 + op->Destroy();
1.714 + }
1.715 +}
1.716 +
1.717 +void DBManager::LockConfig()
1.718 +{
1.719 + m_ConfigLock.Lock();
1.720 +}
1.721 +
1.722 +void DBManager::UnlockConfig()
1.723 +{
1.724 + m_ConfigLock.Unlock();
1.725 +}
1.726 +
1.727 +const char *DBManager::GetDefaultDriverName()
1.728 +{
1.729 + return m_DefDriver.c_str();
1.730 +}
1.731 +
1.732 +void DBManager::AddDependency(IExtension *myself, IDBDriver *driver)
1.733 +{
1.734 + g_Extensions.AddRawDependency(myself, driver->GetIdentity(), driver);
1.735 +}
1.736 +