Skip to content

Commit

Permalink
only use internal locks when multithreaded (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
msotheeswaran-sc authored and GitHub Enterprise committed Sep 25, 2023
1 parent 570bcb5 commit 596c513
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/ae.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,7 @@ void aeReleaseForkLock()

void aeForkLockInChild()
{
g_forkLock.setNotify(false);
g_forkLock.setMulti(false);
}

int aeThreadOwnsLock()
Expand Down
80 changes: 47 additions & 33 deletions src/readwritelock.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,46 @@ class readWriteLock {
int m_readCount = 0;
int m_writeCount = 0;
bool m_writeWaiting = false;
bool m_notify = true;
bool m_multi = true;
public:
readWriteLock(const char *name) : m_readLock(name), m_writeLock(name) {}

void acquireRead() {
std::unique_lock<fastlock> rm(m_readLock);
while (m_writeCount > 0 || m_writeWaiting)
m_cv.wait(rm);
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
if (m_multi) {
rm.lock();
while (m_writeCount > 0 || m_writeWaiting)
m_cv.wait(rm);
}
m_readCount++;
}

bool tryAcquireRead() {
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
if (!rm.try_lock())
return false;
if (m_writeCount > 0 || m_writeWaiting)
return false;
if (m_multi) {
if (!rm.try_lock())
return false;
if (m_writeCount > 0 || m_writeWaiting)
return false;
}
m_readCount++;
return true;
}

void acquireWrite(bool exclusive = true) {
std::unique_lock<fastlock> rm(m_readLock);
m_writeWaiting = true;
while (m_readCount > 0)
m_cv.wait(rm);
if (exclusive) {
/* Another thread might have the write lock while we have the read lock
but won't be able to release it until they can acquire the read lock
so release the read lock and try again instead of waiting to avoid deadlock */
while(!m_writeLock.try_lock())
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
if (m_multi) {
rm.lock();
m_writeWaiting = true;
while (m_readCount > 0)
m_cv.wait(rm);
if (exclusive) {
/* Another thread might have the write lock while we have the read lock
but won't be able to release it until they can acquire the read lock
so release the read lock and try again instead of waiting to avoid deadlock */
while(!m_writeLock.try_lock())
m_cv.wait(rm);
}
}
m_writeCount++;
m_writeWaiting = false;
Expand All @@ -52,41 +60,47 @@ class readWriteLock {

bool tryAcquireWrite(bool exclusive = true) {
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
if (!rm.try_lock())
return false;
if (m_readCount > 0)
return false;
if (exclusive)
if (!m_writeLock.try_lock())
if (m_multi) {
if (!rm.try_lock())
return false;
if (m_readCount > 0)
return false;
if (exclusive)
if (!m_writeLock.try_lock())
return false;
}
m_writeCount++;
return true;
}

void releaseRead() {
std::unique_lock<fastlock> rm(m_readLock);
m_readCount--;
if (m_notify)
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
if (m_multi) {
rm.lock();
m_cv.notify_all();
}
m_readCount--;
}

void releaseWrite(bool exclusive = true) {
std::unique_lock<fastlock> rm(m_readLock);
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
serverAssert(m_writeCount > 0);
if (exclusive)
m_writeLock.unlock();
m_writeCount--;
if (m_notify)
if (m_multi) {
rm.lock();
if (exclusive)
m_writeLock.unlock();
m_cv.notify_all();
}
m_writeCount--;
}

void downgradeWrite(bool exclusive = true) {
releaseWrite(exclusive);
acquireRead();
}

void setNotify(bool notify) {
m_notify = notify;
void setMulti(bool multi) {
m_multi = multi;
}

bool hasReader() {
Expand Down

0 comments on commit 596c513

Please sign in to comment.