lock for cache
diff --git a/include/object-db-file.h b/include/object-db-file.h
index b90bd91..dea8582 100644
--- a/include/object-db-file.h
+++ b/include/object-db-file.h
@@ -10,6 +10,7 @@
#include <deque>
#include <boost/thread/locks.hpp>
#include <boost/lexical_cast.hpp>
+#include <boost/thread/shared_mutex.hpp>
#include <boost/interprocess/sync/file_lock.hpp>
#include <boost/interprocess/sync/sharable_lock.hpp>
#include <boost/interprocess/sync/scoped_lock.hpp>
@@ -44,6 +45,9 @@
typedef boost::interprocess::file_lock Filelock;
typedef boost::interprocess::scoped_lock<Filelock> WriteLock;
typedef boost::interprocess::sharable_lock<Filelock> ReadLock;
+ typedef boost::shared_mutex Mutex;
+ typedef boost::shared_lock<Mutex> SLock;
+ typedef boost::unique_lock<Mutex> ULock;
ObjectDBFile(const string &filename);
virtual ~ObjectDBFile(){}
@@ -121,6 +125,7 @@
// If needed and time allows, we can have more complex cache
#define CACHE_SIZE 10
map<int, Bytes> m_dummyCache;
+ Mutex m_cacheMutex;
};
void inline
diff --git a/src/object-db-file.cpp b/src/object-db-file.cpp
index 02991a9..526ea4e 100644
--- a/src/object-db-file.cpp
+++ b/src/object-db-file.cpp
@@ -122,16 +122,20 @@
Bytes
ObjectDBFile::next()
{
- ReadLock(m_filelock);
- // We are been lazy here; just use file lock as mutex
- // for the access to the cache too
- if (m_dummyCache.find(m_index) != map::end)
+ // Scoped shared lock for cache
{
- int index = m_index;
- m_index++;
- return m_dummyCache[index];
+ SLock(m_cacheLock);
+ // no need to read file if found in cache
+ if (m_dummyCache.find(m_index) != map::end)
+ {
+ int index = m_index;
+ m_index++;
+ return m_dummyCache[index];
+ }
}
+ ReadLock(m_filelock);
+
// m_index not found in cache
Bytes co;
if (m_index >= m_size)
@@ -152,6 +156,7 @@
void
ObjectDBFile::fillDummyCache()
{
+ ULock(m_cacheLock);
m_dummyCache.clear();
int stop = (m_index + CACHE_SIZE < m_size) ? m_index + CACHE_SIZE : m_size;
// the m_index should not change