Protecting sync log transaction with mutexes
diff --git a/src/sync-log.cc b/src/sync-log.cc
index 9911416..c9f125a 100644
--- a/src/sync-log.cc
+++ b/src/sync-log.cc
@@ -23,11 +23,17 @@
#include <utility>
#include <boost/make_shared.hpp>
+#include <boost/thread.hpp>
using namespace boost;
using namespace std;
using namespace Ccnx;
+void xTrace (void*, const char* q)
+{
+ cout << q << endl;
+}
+
SyncLog::SyncLog (const boost::filesystem::path &path, const std::string &localName)
: DbHelper (path)
, m_localName (localName)
@@ -93,10 +99,11 @@
return seq_no;
}
-
HashPtr
SyncLog::RememberStateInStateLog ()
{
+ WriteLock lock (m_stateUpdateMutex);
+
int res = sqlite3_exec (m_db, "BEGIN TRANSACTION;", 0,0,0);
res += sqlite3_exec (m_db, "\
@@ -108,8 +115,10 @@
ORDER BY device_name); \
", 0,0,0);
+ // std::cout << sqlite3_errmsg (m_db) << std::endl;
if (res != SQLITE_OK)
{
+ sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
BOOST_THROW_EXCEPTION (Error::Db ()
<< errmsg_info_str (sqlite3_errmsg(m_db)));
}
@@ -127,8 +136,10 @@
res += sqlite3_bind_int64 (insertStmt, 1, rowId);
sqlite3_step (insertStmt);
+ // std::cout << sqlite3_errmsg (m_db) << std::endl;
if (res != SQLITE_OK)
{
+ sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
BOOST_THROW_EXCEPTION (Error::Db ()
<< errmsg_info_str (sqlite3_errmsg(m_db)));
}
@@ -147,15 +158,24 @@
retval = make_shared<Hash> (sqlite3_column_blob (getHashStmt, 0),
sqlite3_column_bytes (getHashStmt, 0));
}
+ else
+ {
+ sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
+
+ // std::cout << sqlite3_errmsg (m_db) << std::endl;
+ BOOST_THROW_EXCEPTION (Error::Db ()
+ << errmsg_info_str ("Not a valid hash in rememberStateInStateLog"));
+ }
sqlite3_finalize (getHashStmt);
res += sqlite3_exec (m_db, "COMMIT;", 0,0,0);
if (res != SQLITE_OK)
{
+ sqlite3_exec (m_db, "ROLLBACK TRANSACTION;", 0,0,0);
BOOST_THROW_EXCEPTION (Error::Db ()
<< errmsg_info_str ("Some error with rememberStateInStateLog"));
}
-
+
return retval;
}
@@ -263,11 +283,6 @@
return locator;
}
-// void xTrace (void*, const char* q)
-// {
-// cout << q << endl;
-// }
-
void
SyncLog::UpdateLocator(const Name &deviceName, const Name &locator)
{
diff --git a/src/sync-log.h b/src/sync-log.h
index d066991..3ab8489 100644
--- a/src/sync-log.h
+++ b/src/sync-log.h
@@ -26,6 +26,7 @@
#include <sync-state.pb.h>
#include <ccnx-name.h>
#include <map>
+#include <boost/thread/shared_mutex.hpp>
typedef boost::shared_ptr<SyncStateMsg> SyncStateMsgPtr;
@@ -83,6 +84,11 @@
Ccnx::Name m_localName;
sqlite3_int64 m_localDeviceId;
+
+ typedef boost::mutex Mutex;
+ typedef boost::unique_lock<Mutex> WriteLock;
+
+ Mutex m_stateUpdateMutex;
};