Add atomic reference counting and port DBI (bug 5876 part 3, r=ds).
--HG-- extra : rebase_source : a6defaf477e7a856ce91f92d5f3143f12c141da3
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* vim: set ts=4 sw=4 tw=99 noet :
|
||||
* =============================================================================
|
||||
* SourceMod SQLite Extension
|
||||
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||
@@ -34,47 +34,25 @@
|
||||
#include "SqQuery.h"
|
||||
|
||||
SqDatabase::SqDatabase(sqlite3 *sq3, bool persistent) :
|
||||
m_sq3(sq3), m_refcount(1), m_pFullLock(NULL), m_Persistent(persistent)
|
||||
m_sq3(sq3), m_Persistent(persistent)
|
||||
{
|
||||
m_pRefLock = threader->MakeMutex();
|
||||
}
|
||||
|
||||
SqDatabase::~SqDatabase()
|
||||
{
|
||||
m_pRefLock->DestroyThis();
|
||||
if (m_pFullLock)
|
||||
{
|
||||
m_pFullLock->DestroyThis();
|
||||
}
|
||||
if (m_Persistent)
|
||||
g_SqDriver.RemovePersistent(this);
|
||||
sqlite3_close(m_sq3);
|
||||
}
|
||||
|
||||
void SqDatabase::IncReferenceCount()
|
||||
{
|
||||
m_pRefLock->Lock();
|
||||
m_refcount++;
|
||||
m_pRefLock->Unlock();
|
||||
AddRef();
|
||||
}
|
||||
|
||||
bool SqDatabase::Close()
|
||||
{
|
||||
m_pRefLock->Lock();
|
||||
if (m_refcount > 1)
|
||||
{
|
||||
m_refcount--;
|
||||
m_pRefLock->Unlock();
|
||||
return false;
|
||||
}
|
||||
m_pRefLock->Unlock();
|
||||
|
||||
if (m_Persistent)
|
||||
{
|
||||
g_SqDriver.RemovePersistent(this);
|
||||
}
|
||||
|
||||
delete this;
|
||||
|
||||
return true;
|
||||
return !Release();
|
||||
}
|
||||
|
||||
const char *SqDatabase::GetError(int *errorCode/* =NULL */)
|
||||
@@ -84,26 +62,17 @@ const char *SqDatabase::GetError(int *errorCode/* =NULL */)
|
||||
|
||||
bool SqDatabase::LockForFullAtomicOperation()
|
||||
{
|
||||
if (!m_pFullLock)
|
||||
{
|
||||
m_pFullLock = threader->MakeMutex();
|
||||
if (!m_pFullLock)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
m_pFullLock->Lock();
|
||||
if (!m_FullLock)
|
||||
m_FullLock = new ke::Mutex();
|
||||
|
||||
m_FullLock->Lock();
|
||||
return true;
|
||||
}
|
||||
|
||||
void SqDatabase::UnlockFromFullAtomicOperation()
|
||||
{
|
||||
if (m_pFullLock)
|
||||
{
|
||||
m_pFullLock->Unlock();
|
||||
}
|
||||
if (m_FullLock)
|
||||
m_FullLock->Unlock();
|
||||
}
|
||||
|
||||
IDBDriver *SqDatabase::GetDriver()
|
||||
@@ -259,4 +228,4 @@ bool SqDatabase::SetCharacterSet(const char *characterset)
|
||||
{
|
||||
// sqlite only supports utf8 and utf16 - by the time the database is created. It's too late here.
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* vim: set ts=4 sw=4 tw=99 noet :
|
||||
* =============================================================================
|
||||
* SourceMod SQLite Extension
|
||||
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||
@@ -32,10 +32,13 @@
|
||||
#ifndef _INCLUDE_SQLITE_SOURCEMOD_DATABASE_H_
|
||||
#define _INCLUDE_SQLITE_SOURCEMOD_DATABASE_H_
|
||||
|
||||
#include <IThreader.h>
|
||||
#include <am-refcounting-threadsafe.h>
|
||||
#include <am-thread-utils.h>
|
||||
#include "SqDriver.h"
|
||||
|
||||
class SqDatabase : public IDatabase
|
||||
class SqDatabase
|
||||
: public IDatabase,
|
||||
public ke::RefcountedThreadsafe<SqDatabase>
|
||||
{
|
||||
public:
|
||||
SqDatabase(sqlite3 *sq3, bool persistent);
|
||||
@@ -63,9 +66,7 @@ public:
|
||||
sqlite3 *GetDb();
|
||||
private:
|
||||
sqlite3 *m_sq3;
|
||||
unsigned int m_refcount;
|
||||
IMutex *m_pFullLock;
|
||||
IMutex *m_pRefLock;
|
||||
ke::AutoPtr<ke::Mutex> m_FullLock;
|
||||
bool m_Persistent;
|
||||
String m_LastError;
|
||||
int m_LastErrorCode;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* vim: set ts=4 sw=4 tw=99 noet :
|
||||
* =============================================================================
|
||||
* SourceMod SQLite Extension
|
||||
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||
@@ -66,24 +66,16 @@ int busy_handler(void *unused1, int unused2)
|
||||
SqDriver::SqDriver()
|
||||
{
|
||||
m_Handle = BAD_HANDLE;
|
||||
m_pOpenLock = NULL;
|
||||
m_bThreadSafe = false;
|
||||
}
|
||||
|
||||
void SqDriver::Initialize()
|
||||
{
|
||||
m_pOpenLock = threader->MakeMutex();
|
||||
|
||||
InitializeThreadSafety();
|
||||
}
|
||||
|
||||
void SqDriver::Shutdown()
|
||||
{
|
||||
if (m_pOpenLock)
|
||||
{
|
||||
m_pOpenLock->DestroyThis();
|
||||
}
|
||||
|
||||
if (m_bThreadSafe)
|
||||
{
|
||||
sqlite3_enable_shared_cache(0);
|
||||
@@ -158,8 +150,7 @@ inline bool IsPathSepChar(char c)
|
||||
|
||||
IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *error, size_t maxlength)
|
||||
{
|
||||
/* We wrap most of the open process in a mutex just to be safe */
|
||||
m_pOpenLock->Lock();
|
||||
ke::AutoLock lock(&m_OpenLock);
|
||||
|
||||
/* Format our path */
|
||||
char path[PLATFORM_MAX_PATH];
|
||||
@@ -189,7 +180,6 @@ IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *er
|
||||
if (!libsys->CreateFolder(fullpath))
|
||||
{
|
||||
strncopy(error, "Could not create or open \"data\" folder\"", maxlength);
|
||||
m_pOpenLock->Unlock();
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -237,7 +227,6 @@ IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *er
|
||||
if ((*iter).path.compare(fullpath) == 0)
|
||||
{
|
||||
(*iter).db->IncReferenceCount();
|
||||
m_pOpenLock->Unlock();
|
||||
return (*iter).db;
|
||||
}
|
||||
}
|
||||
@@ -250,7 +239,6 @@ IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *er
|
||||
{
|
||||
strncopy(error, sqlite3_errmsg(sql), maxlength);
|
||||
sqlite3_close(sql);
|
||||
m_pOpenLock->Unlock();
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -266,13 +254,13 @@ IDatabase *SqDriver::Connect(const DatabaseInfo *info, bool persistent, char *er
|
||||
m_Cache.push_back(pinfo);
|
||||
}
|
||||
|
||||
m_pOpenLock->Unlock();
|
||||
|
||||
return pdb;
|
||||
}
|
||||
|
||||
void SqDriver::RemovePersistent(IDatabase *pdb)
|
||||
{
|
||||
ke::AutoLock lock(&m_OpenLock);
|
||||
|
||||
List<SqDbInfo>::iterator iter;
|
||||
for (iter = m_Cache.begin(); iter != m_Cache.end(); iter++)
|
||||
{
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* vim: set ts=4 sw=4 tw=99 noet :
|
||||
* =============================================================================
|
||||
* SourceMod SQLite Extension
|
||||
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||
@@ -32,10 +32,11 @@
|
||||
#ifndef _INCLUDE_SQLITE_SOURCEMOD_DRIVER_H_
|
||||
#define _INCLUDE_SQLITE_SOURCEMOD_DRIVER_H_
|
||||
|
||||
#define SOURCEMOD_SQL_DRIVER_CODE
|
||||
#include <IDBDriver.h>
|
||||
#include <IThreader.h>
|
||||
#include <sh_list.h>
|
||||
#include <sh_string.h>
|
||||
#include <am-thread-utils.h>
|
||||
#include "sqlite-source/sqlite3.h"
|
||||
|
||||
using namespace SourceMod;
|
||||
@@ -70,7 +71,7 @@ public:
|
||||
void RemovePersistent(IDatabase *pdb);
|
||||
private:
|
||||
Handle_t m_Handle;
|
||||
IMutex *m_pOpenLock;
|
||||
ke::Mutex m_OpenLock;
|
||||
List<SqDbInfo> m_Cache;
|
||||
bool m_bThreadSafe;
|
||||
};
|
||||
|
||||
@@ -36,14 +36,12 @@ SqQuery::SqQuery(SqDatabase *parent, sqlite3_stmt *stmt) :
|
||||
{
|
||||
m_ParamCount = sqlite3_bind_parameter_count(m_pStmt);
|
||||
m_ColCount = sqlite3_column_count(m_pStmt);
|
||||
m_pParent->IncReferenceCount();
|
||||
}
|
||||
|
||||
SqQuery::~SqQuery()
|
||||
{
|
||||
delete m_pResults;
|
||||
sqlite3_finalize(m_pStmt);
|
||||
m_pParent->Close();
|
||||
}
|
||||
|
||||
IResultSet *SqQuery::GetResultSet()
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* vim: set ts=4 :
|
||||
* vim: set ts=4 sw=4 tw=99 noet :
|
||||
* =============================================================================
|
||||
* SourceMod SQLite Extension
|
||||
* Copyright (C) 2004-2008 AlliedModders LLC. All rights reserved.
|
||||
@@ -32,6 +32,7 @@
|
||||
#ifndef _INCLUDE_SQLITE_SOURCEMOD_QUERY_H_
|
||||
#define _INCLUDE_SQLITE_SOURCEMOD_QUERY_H_
|
||||
|
||||
#include <am-refcounting.h>
|
||||
#include "SqDatabase.h"
|
||||
#include "SqResults.h"
|
||||
|
||||
@@ -81,7 +82,7 @@ public: //IResultRow
|
||||
public:
|
||||
sqlite3_stmt *GetStmt();
|
||||
private:
|
||||
SqDatabase *m_pParent;
|
||||
ke::Ref<SqDatabase> m_pParent;
|
||||
sqlite3_stmt *m_pStmt;
|
||||
SqResults *m_pResults;
|
||||
unsigned int m_ParamCount;
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
* @brief Sample extension code header.
|
||||
*/
|
||||
|
||||
#define SOURCEMOD_SQL_DRIVER_CODE
|
||||
#include "smsdk_ext.h"
|
||||
|
||||
|
||||
|
||||
@@ -67,7 +67,7 @@
|
||||
//#define SMEXT_ENABLE_MEMUTILS
|
||||
//#define SMEXT_ENABLE_GAMEHELPERS
|
||||
//#define SMEXT_ENABLE_TIMERSYS
|
||||
#define SMEXT_ENABLE_THREADER
|
||||
//#define SMEXT_ENABLE_THREADER
|
||||
#define SMEXT_ENABLE_LIBSYS
|
||||
|
||||
#endif // _INCLUDE_SOURCEMOD_EXTENSION_CONFIG_H_
|
||||
|
||||
Reference in New Issue
Block a user