1
0
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:
Patrick LeBlanc
2019-03-18 18:12:42 -05:00
parent b4e2596cdf
commit a26e939d75
4 changed files with 94 additions and 1 deletions

View File

@@ -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);
}
}
}

View File

@@ -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
};
}

View File

@@ -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);

View File

@@ -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;