only use internal locks when multithreaded (#205)
This commit is contained in:
parent
570bcb55c7
commit
596c513d3e
@ -861,7 +861,7 @@ void aeReleaseForkLock()
|
|||||||
|
|
||||||
void aeForkLockInChild()
|
void aeForkLockInChild()
|
||||||
{
|
{
|
||||||
g_forkLock.setNotify(false);
|
g_forkLock.setMulti(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
int aeThreadOwnsLock()
|
int aeThreadOwnsLock()
|
||||||
|
@ -8,38 +8,46 @@ class readWriteLock {
|
|||||||
int m_readCount = 0;
|
int m_readCount = 0;
|
||||||
int m_writeCount = 0;
|
int m_writeCount = 0;
|
||||||
bool m_writeWaiting = false;
|
bool m_writeWaiting = false;
|
||||||
bool m_notify = true;
|
bool m_multi = true;
|
||||||
public:
|
public:
|
||||||
readWriteLock(const char *name) : m_readLock(name), m_writeLock(name) {}
|
readWriteLock(const char *name) : m_readLock(name), m_writeLock(name) {}
|
||||||
|
|
||||||
void acquireRead() {
|
void acquireRead() {
|
||||||
std::unique_lock<fastlock> rm(m_readLock);
|
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
|
||||||
while (m_writeCount > 0 || m_writeWaiting)
|
if (m_multi) {
|
||||||
m_cv.wait(rm);
|
rm.lock();
|
||||||
|
while (m_writeCount > 0 || m_writeWaiting)
|
||||||
|
m_cv.wait(rm);
|
||||||
|
}
|
||||||
m_readCount++;
|
m_readCount++;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool tryAcquireRead() {
|
bool tryAcquireRead() {
|
||||||
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
|
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
|
||||||
if (!rm.try_lock())
|
if (m_multi) {
|
||||||
return false;
|
if (!rm.try_lock())
|
||||||
if (m_writeCount > 0 || m_writeWaiting)
|
return false;
|
||||||
return false;
|
if (m_writeCount > 0 || m_writeWaiting)
|
||||||
|
return false;
|
||||||
|
}
|
||||||
m_readCount++;
|
m_readCount++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void acquireWrite(bool exclusive = true) {
|
void acquireWrite(bool exclusive = true) {
|
||||||
std::unique_lock<fastlock> rm(m_readLock);
|
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
|
||||||
m_writeWaiting = true;
|
if (m_multi) {
|
||||||
while (m_readCount > 0)
|
rm.lock();
|
||||||
m_cv.wait(rm);
|
m_writeWaiting = true;
|
||||||
if (exclusive) {
|
while (m_readCount > 0)
|
||||||
/* 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_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_writeCount++;
|
||||||
m_writeWaiting = false;
|
m_writeWaiting = false;
|
||||||
@ -52,32 +60,38 @@ public:
|
|||||||
|
|
||||||
bool tryAcquireWrite(bool exclusive = true) {
|
bool tryAcquireWrite(bool exclusive = true) {
|
||||||
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
|
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
|
||||||
if (!rm.try_lock())
|
if (m_multi) {
|
||||||
return false;
|
if (!rm.try_lock())
|
||||||
if (m_readCount > 0)
|
|
||||||
return false;
|
|
||||||
if (exclusive)
|
|
||||||
if (!m_writeLock.try_lock())
|
|
||||||
return false;
|
return false;
|
||||||
|
if (m_readCount > 0)
|
||||||
|
return false;
|
||||||
|
if (exclusive)
|
||||||
|
if (!m_writeLock.try_lock())
|
||||||
|
return false;
|
||||||
|
}
|
||||||
m_writeCount++;
|
m_writeCount++;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void releaseRead() {
|
void releaseRead() {
|
||||||
std::unique_lock<fastlock> rm(m_readLock);
|
std::unique_lock<fastlock> rm(m_readLock, std::defer_lock);
|
||||||
m_readCount--;
|
if (m_multi) {
|
||||||
if (m_notify)
|
rm.lock();
|
||||||
m_cv.notify_all();
|
m_cv.notify_all();
|
||||||
|
}
|
||||||
|
m_readCount--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void releaseWrite(bool exclusive = true) {
|
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);
|
serverAssert(m_writeCount > 0);
|
||||||
if (exclusive)
|
if (m_multi) {
|
||||||
m_writeLock.unlock();
|
rm.lock();
|
||||||
m_writeCount--;
|
if (exclusive)
|
||||||
if (m_notify)
|
m_writeLock.unlock();
|
||||||
m_cv.notify_all();
|
m_cv.notify_all();
|
||||||
|
}
|
||||||
|
m_writeCount--;
|
||||||
}
|
}
|
||||||
|
|
||||||
void downgradeWrite(bool exclusive = true) {
|
void downgradeWrite(bool exclusive = true) {
|
||||||
@ -85,8 +99,8 @@ public:
|
|||||||
acquireRead();
|
acquireRead();
|
||||||
}
|
}
|
||||||
|
|
||||||
void setNotify(bool notify) {
|
void setMulti(bool multi) {
|
||||||
m_notify = notify;
|
m_multi = multi;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool hasReader() {
|
bool hasReader() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user