Use a simple Thread in SqliteDatabase

This commit is contained in:
Vinnie Falco
2013-10-03 18:29:20 -07:00
parent 5fc823ae08
commit b76443dbde
2 changed files with 32 additions and 7 deletions

View File

@@ -51,18 +51,24 @@ SqliteStatement::~SqliteStatement ()
SqliteDatabase::SqliteDatabase (const char* host)
: Database (host)
, Thread ("sqlitedb")
, m_walMutex (this, "SqliteDB", __FILE__, __LINE__)
, m_thread ("sqlitedb")
, mWalQ (NULL)
, walRunning (false)
{
m_thread.start ();
startThread ();
mConnection = NULL;
mAuxConnection = NULL;
mCurrentStmt = NULL;
}
SqliteDatabase::~SqliteDatabase ()
{
// Blocks until the thread exits in an orderly fashion
stopThread ();
}
void SqliteDatabase::connect ()
{
int rc = sqlite3_open_v2 (mHost.c_str (), &mConnection,
@@ -317,7 +323,22 @@ void SqliteDatabase::doHook (const char* db, int pages)
}
else
{
m_thread.call (&SqliteDatabase::runWal, this);
notify();
}
}
void SqliteDatabase::run ()
{
// Simple thread loop runs Wal every time it wakes up via
// the call to Thread::notify, unless Thread::threadShouldExit returns
// true in which case we simply break.
//
for (;;)
{
wait ();
if (threadShouldExit())
break;
runWal();
}
}

View File

@@ -23,10 +23,12 @@
class SqliteDatabase
: public Database
, LeakChecked <SqliteDatabase>
, private Thread
, private LeakChecked <SqliteDatabase>
{
public:
explicit SqliteDatabase (char const* host);
~SqliteDatabase ();
void connect ();
void disconnect ();
@@ -66,19 +68,19 @@ public:
return this;
}
void runWal ();
void doHook (const char* db, int walSize);
int getKBUsedDB ();
int getKBUsedAll ();
private:
void run ();
void runWal ();
typedef RippleMutex LockType;
typedef LockType::ScopedLockType ScopedLockType;
LockType m_walMutex;
ThreadWithCallQueue m_thread;
sqlite3* mConnection;
// VFALCO TODO Why do we need an "aux" connection? Should just use a second SqliteDatabase object.
sqlite3* mAuxConnection;
@@ -89,6 +91,8 @@ private:
bool walRunning;
};
//------------------------------------------------------------------------------
class SqliteStatement
{
private: