From b76443dbded27952a32d69db1499e9828708ff3f Mon Sep 17 00:00:00 2001 From: Vinnie Falco Date: Thu, 3 Oct 2013 18:29:20 -0700 Subject: [PATCH] Use a simple Thread in SqliteDatabase --- src/ripple_app/data/SqliteDatabase.cpp | 27 +++++++++++++++++++++++--- src/ripple_app/data/SqliteDatabase.h | 12 ++++++++---- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/ripple_app/data/SqliteDatabase.cpp b/src/ripple_app/data/SqliteDatabase.cpp index 98eeee269..874828b6b 100644 --- a/src/ripple_app/data/SqliteDatabase.cpp +++ b/src/ripple_app/data/SqliteDatabase.cpp @@ -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(); } } diff --git a/src/ripple_app/data/SqliteDatabase.h b/src/ripple_app/data/SqliteDatabase.h index 3898e25dc..d76bc3ffe 100644 --- a/src/ripple_app/data/SqliteDatabase.h +++ b/src/ripple_app/data/SqliteDatabase.h @@ -23,10 +23,12 @@ class SqliteDatabase : public Database - , LeakChecked + , private Thread + , private LeakChecked { 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: