You've already forked mariadb-columnstore-engine
mirror of
https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
synced 2025-12-15 12:09:09 +03:00
Added some add'l methods to RWLock, added the file locking stuff to IOC.
This commit is contained in:
@@ -435,4 +435,50 @@ string IOCoordinator::getNewKey(string sourceName, size_t offset, size_t length)
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool IOCoordinator::readLock(const string &filename)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(lockMutex);
|
||||
|
||||
auto ins = locks.insert(pair<string, RWLock *>(filename, NULL));
|
||||
if (ins.second)
|
||||
ins.first->second = new RWLock();
|
||||
ins.first->second->readLock(s);
|
||||
}
|
||||
|
||||
void IOCoordinator::readUnlock(const string &filename)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(lockMutex);
|
||||
|
||||
auto it = locks.find(filename);
|
||||
it->second->readUnlock();
|
||||
if (!it->second->inUse())
|
||||
{
|
||||
delete it->second;
|
||||
locks.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
bool IOCoordinator::writeLock(const string &filename)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(lockMutex);
|
||||
|
||||
auto ins = locks.insert(pair<string, RWLock *>(filename, NULL));
|
||||
if (ins.second)
|
||||
ins.first->second = new RWLock();
|
||||
ins.first->second->writeLock(s);
|
||||
}
|
||||
|
||||
void IOCoordinator::writeUnlock(const string &filename)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(lockMutex);
|
||||
|
||||
auto it = locks.find(filename);
|
||||
it->second->readUnlock();
|
||||
if (!it->second->inUse())
|
||||
{
|
||||
delete it->second;
|
||||
locks.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
|
||||
#include "Config.h"
|
||||
#include "SMLogging.h"
|
||||
#include "RWLock.h"
|
||||
|
||||
namespace storagemanager
|
||||
{
|
||||
@@ -44,11 +45,20 @@ class IOCoordinator : public boost::noncopyable
|
||||
boost::shared_array<uint8_t> mergeJournal(const char *objectPath, const char *journalPath, off_t offset = 0, size_t len = 0) const;
|
||||
int mergeJournalInMem(uint8_t *objData, const char *journalPath);
|
||||
|
||||
/* Lock manipulation fcns. They can lock on any param given to them. */
|
||||
bool readLock(const std::string &filename);
|
||||
bool writeLock(const std::string &filename);
|
||||
void readUnlock(const std::string &filename);
|
||||
void writeUnlock(const std::string &filename);
|
||||
|
||||
private:
|
||||
IOCoordinator();
|
||||
Config *config;
|
||||
SMLogging *logger;
|
||||
size_t objectSize;
|
||||
|
||||
std::map<std::string, RWLock *> locks;
|
||||
boost::mutex lockMutex; // lol
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,11 @@ RWLock::~RWLock()
|
||||
assert(!writersRunning);
|
||||
}
|
||||
|
||||
bool RWLock::inUse()
|
||||
{
|
||||
return readersWaiting || readersRunning || writersWaiting || writersRunning;
|
||||
}
|
||||
|
||||
void RWLock::readLock()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(m);
|
||||
@@ -27,6 +32,19 @@ void RWLock::readLock()
|
||||
--readersWaiting;
|
||||
}
|
||||
|
||||
void RWLock::readLock(boost::unique_lock<boost::mutex> &l)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(m);
|
||||
l.unlock();
|
||||
|
||||
++readersWaiting;
|
||||
while (writersWaiting != 0 || writersRunning != 0)
|
||||
okToRead.wait(s);
|
||||
|
||||
++readersRunning;
|
||||
--readersWaiting;
|
||||
}
|
||||
|
||||
void RWLock::readUnlock()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(m);
|
||||
@@ -48,6 +66,19 @@ void RWLock::writeLock()
|
||||
--writersWaiting;
|
||||
}
|
||||
|
||||
void RWLock::writeLock(boost::unique_lock<boost::mutex> &l)
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(m);
|
||||
l.unlock();
|
||||
|
||||
++writersWaiting;
|
||||
while (readersRunning != 0 || writersRunning != 0)
|
||||
okToWrite.wait(s);
|
||||
|
||||
++writersRunning;
|
||||
--writersWaiting;
|
||||
}
|
||||
|
||||
void RWLock::writeUnlock()
|
||||
{
|
||||
boost::unique_lock<boost::mutex> s(m);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <boost/thread/mutex.hpp>
|
||||
#include <boost/thread/condition.hpp>
|
||||
|
||||
|
||||
/* Quicky impl of a read-write lock that prioritizes writers. */
|
||||
namespace storagemanager
|
||||
{
|
||||
@@ -12,10 +11,17 @@ namespace storagemanager
|
||||
~RWLock();
|
||||
|
||||
void readLock();
|
||||
// this version will release the lock in the parameter after locking this instance
|
||||
void readLock(boost::unique_lock<boost::mutex> &);
|
||||
void readUnlock();
|
||||
void writeLock();
|
||||
// this version will release the lock in the parameter after locking this instance
|
||||
void writeLock(boost::unique_lock<boost::mutex> &);
|
||||
void writeUnlock();
|
||||
|
||||
// returns true if anything is blocked on or owns this lock instance.
|
||||
bool inUse();
|
||||
|
||||
private:
|
||||
uint readersWaiting;
|
||||
uint readersRunning;
|
||||
|
||||
Reference in New Issue
Block a user