refactor aeReleaseForkLockChild to capture releaseRead case

This commit is contained in:
Malavan Sotheeswaran 2023-02-09 12:06:27 -08:00
parent b5ab1d64e2
commit 0cba0ed30b
4 changed files with 12 additions and 18 deletions

View File

@ -859,9 +859,9 @@ void aeReleaseForkLock()
g_forkLock.downgradeWrite(); g_forkLock.downgradeWrite();
} }
void aeReleaseForkLockChild() void aeForkLockInChild()
{ {
g_forkLock.downgradeWriteChild(); g_forkLock.setNotify(false);
} }
int aeThreadOwnsLock() int aeThreadOwnsLock()

View File

@ -171,7 +171,7 @@ int aeTryAcquireLock(int fWeak);
void aeThreadOffline(); void aeThreadOffline();
void aeReleaseLock(); void aeReleaseLock();
void aeReleaseForkLock(); void aeReleaseForkLock();
void aeReleaseForkLockChild(); void aeForkLockInChild();
int aeThreadOwnsLock(); int aeThreadOwnsLock();
void aeSetThreadOwnsLockOverride(int fOverride); void aeSetThreadOwnsLockOverride(int fOverride);
int aeLockContested(int threshold); int aeLockContested(int threshold);

View File

@ -8,6 +8,7 @@ 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;
public: public:
readWriteLock(const char *name) : m_readLock(name), m_writeLock(name) {} readWriteLock(const char *name) : m_readLock(name), m_writeLock(name) {}
@ -65,7 +66,8 @@ public:
void releaseRead() { void releaseRead() {
std::unique_lock<fastlock> rm(m_readLock); std::unique_lock<fastlock> rm(m_readLock);
m_readCount--; m_readCount--;
m_cv.notify_all(); if (m_notify)
m_cv.notify_all();
} }
void releaseWrite(bool exclusive = true) { void releaseWrite(bool exclusive = true) {
@ -74,16 +76,8 @@ public:
if (exclusive) if (exclusive)
m_writeLock.unlock(); m_writeLock.unlock();
m_writeCount--; m_writeCount--;
m_cv.notify_all(); if (m_notify)
} m_cv.notify_all();
void releaseWriteChild(bool exclusive = true) {
std::unique_lock<fastlock> rm(m_readLock);
serverAssert(m_writeCount > 0);
if (exclusive)
m_writeLock.unlock();
m_writeCount--;
m_writeWaiting = false;
} }
void downgradeWrite(bool exclusive = true) { void downgradeWrite(bool exclusive = true) {
@ -91,9 +85,8 @@ public:
acquireRead(); acquireRead();
} }
void downgradeWriteChild(bool exclusive = true) { void setNotify(bool notify) {
releaseWriteChild(exclusive); m_notify = notify;
acquireRead();
} }
bool hasReader() { bool hasReader() {

View File

@ -6888,7 +6888,8 @@ int redisFork(int purpose) {
latencyAddSampleIfNeeded("fork-lock",(ustime()-startWriteLock)/1000); latencyAddSampleIfNeeded("fork-lock",(ustime()-startWriteLock)/1000);
if ((childpid = fork()) == 0) { if ((childpid = fork()) == 0) {
/* Child */ /* Child */
aeReleaseForkLockChild(); aeForkLockInChild();
aeReleaseForkLock();
g_pserver->in_fork_child = purpose; g_pserver->in_fork_child = purpose;
setOOMScoreAdj(CONFIG_OOM_BGCHILD); setOOMScoreAdj(CONFIG_OOM_BGCHILD);
setupChildSignalHandlers(); setupChildSignalHandlers();