Commit f8235653 authored by Elvin Sindrilaru's avatar Elvin Sindrilaru

COMMON: Implement move constructor and delete the copy constructor for the RWMutex

parent c9161a19
......@@ -115,7 +115,8 @@ pthread_rwlock_t RWMutex::mOrderChkLock;
// Constructor
//------------------------------------------------------------------------------
RWMutex::RWMutex(bool prefer_rd):
mBlocking(false), mRdLockCounter(0), mWrLockCounter(0), mPreferRd(prefer_rd)
mBlocking(false), mMutexImp(nullptr), mRdLockCounter(0), mWrLockCounter(0),
mPreferRd(prefer_rd)
{
// Try to get write lock in 5 seconds, then release quickly and retry
wlocktime.tv_sec = 5;
......@@ -185,6 +186,31 @@ RWMutex::~RWMutex()
delete mMutexImp;
}
//------------------------------------------------------------------------------
// Move assignment operator
//------------------------------------------------------------------------------
RWMutex&
RWMutex::operator=(RWMutex&& other) noexcept
{
if (this != &other) {
this->mMutexImp = other.mMutexImp;
other.mMutexImp = nullptr;
this->mBlocking = other.mBlocking;
}
return *this;
}
//------------------------------------------------------------------------------
// Move constructor
//------------------------------------------------------------------------------
RWMutex::RWMutex(RWMutex&& other) noexcept
{
*this = std::move(other);
}
//------------------------------------------------------------------------------
// Try to read lock the mutex within the timout value
//------------------------------------------------------------------------------
......@@ -814,16 +840,20 @@ size_t
RWMutex::EstimateOrderCheckingAddedLatency(size_t nmutexes,
size_t loopsize)
{
std::vector<RWMutex> mutexes;
mutexes.resize(nmutexes);
std::vector<RWMutex*> mutexes;
mutexes.reserve(nmutexes);
for (size_t i = 0; i < nmutexes; ++i) {
mutexes.push_back(new RWMutex());
}
std::vector<RWMutex*> order;
order.resize(nmutexes);
int count = 0;
order.reserve(nmutexes);
for (auto it = mutexes.begin(); it != mutexes.end(); ++it) {
it->SetTiming(false);
it->SetSampling(false);
order[count++] = &(*it);
for (auto& mtx : mutexes) {
mtx->SetTiming(false);
mtx->SetSampling(false);
order.push_back(mtx);
}
RWMutex::AddOrderRule("estimaterule", order);
......@@ -835,11 +865,11 @@ RWMutex::EstimateOrderCheckingAddedLatency(size_t nmutexes,
for (size_t k = 0; k < loopsize; k++) {
for (auto it = mutexes.begin(); it != mutexes.end(); ++it) {
it->LockWrite();
(*it)->LockWrite();
}
for (auto rit = mutexes.rbegin(); rit != mutexes.rend(); ++rit) {
rit->UnLockWrite();
for (auto it = mutexes.rbegin(); it != mutexes.rend(); ++it) {
(*it)->UnLockWrite();
}
}
......@@ -849,11 +879,11 @@ RWMutex::EstimateOrderCheckingAddedLatency(size_t nmutexes,
for (size_t k = 0; k < loopsize; ++k) {
for (auto it = mutexes.begin(); it != mutexes.end(); ++it) {
it->LockWrite();
(*it)->LockWrite();
}
for (auto rit = mutexes.rbegin(); rit != mutexes.rend(); ++rit) {
rit->UnLockWrite();
for (auto it = mutexes.rbegin(); it != mutexes.rend(); ++it) {
(*it)->UnLockWrite();
}
}
......@@ -861,6 +891,11 @@ RWMutex::EstimateOrderCheckingAddedLatency(size_t nmutexes,
RWMutex::SetTimingGlobal(sav);
RWMutex::SetOrderCheckingGlobal(sav2);
RemoveOrderRule("estimaterule");
for (size_t i = 0; i < nmutexes; ++i) {
delete mutexes[i];
}
return size_t(double(s - t) / (loopsize * nmutexes));
}
......
......@@ -96,6 +96,26 @@ public:
//----------------------------------------------------------------------------
~RWMutex();
//----------------------------------------------------------------------------
//! Move constructor
//----------------------------------------------------------------------------
RWMutex(RWMutex&& other) noexcept;
//----------------------------------------------------------------------------
//! Move assignment operator
//----------------------------------------------------------------------------
RWMutex& operator=(RWMutex&& other) noexcept;
//----------------------------------------------------------------------------
//! Copy constructor
//----------------------------------------------------------------------------
RWMutex(const RWMutex&) = delete;
//----------------------------------------------------------------------------
//! Copy assignment operator
//----------------------------------------------------------------------------
RWMutex& operator=(const RWMutex&) = delete;
//----------------------------------------------------------------------------
//! Set the write lock to blocking or not blocking
//!
......
......@@ -105,6 +105,12 @@ public:
virtual ~FmdDbMapHandler()
{
Shutdown();
for (auto it = mFsMtxMap.begin(); it != mFsMtxMap.end(); ++it) {
delete it->second;
}
mFsMtxMap.clear();
}
//----------------------------------------------------------------------------
......@@ -400,7 +406,7 @@ private:
std::map<eos::common::FileSystem::fsid_t, std::string> DBfilename;
std::map<eos::common::FileSystem::fsid_t, bool> mIsSyncing;
///! Map containing mutexes for each file system id
google::dense_hash_map<eos::common::FileSystem::fsid_t, eos::common::RWMutex>
google::dense_hash_map<eos::common::FileSystem::fsid_t, eos::common::RWMutex*>
mFsMtxMap;
eos::common::RWMutex mFsMtxMapMutex; ///< Mutex protecting the previous map
......@@ -417,20 +423,21 @@ private:
if (mFsMtxMap.count(fsid)) {
if (write) {
mFsMtxMap[fsid].LockWrite();
mFsMtxMap[fsid]->LockWrite();
} else {
mFsMtxMap[fsid].LockRead();
mFsMtxMap[fsid]->LockRead();
}
mFsMtxMapMutex.UnLockRead();
} else {
mFsMtxMapMutex.UnLockRead();
mFsMtxMapMutex.LockWrite();
mFsMtxMap[fsid] = new eos::common::RWMutex();
if (write) {
mFsMtxMap[fsid].LockWrite();
mFsMtxMap[fsid]->LockWrite();
} else {
mFsMtxMap[fsid].LockRead();
mFsMtxMap[fsid]->LockRead();
}
mFsMtxMapMutex.UnLockWrite();
......@@ -450,9 +457,9 @@ private:
if (mFsMtxMap.count(fsid)) {
if (write) {
mFsMtxMap[fsid].UnLockWrite();
mFsMtxMap[fsid]->UnLockWrite();
} else {
mFsMtxMap[fsid].UnLockRead();
mFsMtxMap[fsid]->UnLockRead();
}
mFsMtxMapMutex.UnLockRead();
......@@ -461,11 +468,12 @@ private:
// mutex #i should have been locked at some point.
mFsMtxMapMutex.UnLockRead();
mFsMtxMapMutex.LockWrite();
mFsMtxMap[fsid] = new eos::common::RWMutex();
if (write) {
mFsMtxMap[fsid].UnLockWrite();
mFsMtxMap[fsid]->UnLockWrite();
} else {
mFsMtxMap[fsid].UnLockRead();
mFsMtxMap[fsid]->UnLockRead();
}
mFsMtxMapMutex.UnLockWrite();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment