Merge branch 'timethread_idle' into 'keydbpro'
Sleep the time thread when server sleeps See merge request keydb-dev/KeyDB-Pro!15 Former-commit-id: 0864c46e38f85f03ea69a18162e3797370c17c91
This commit is contained in:
commit
ace6ad4752
@ -61,6 +61,7 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <uuid/uuid.h>
|
#include <uuid/uuid.h>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
#include "aelocker.h"
|
#include "aelocker.h"
|
||||||
#include "keycheck.h"
|
#include "keycheck.h"
|
||||||
#include "motd.h"
|
#include "motd.h"
|
||||||
@ -97,6 +98,10 @@ redisServer *g_pserver = &GlobalHidden::server;
|
|||||||
struct redisServerConst cserver;
|
struct redisServerConst cserver;
|
||||||
thread_local struct redisServerThreadVars *serverTL = NULL; // thread local server vars
|
thread_local struct redisServerThreadVars *serverTL = NULL; // thread local server vars
|
||||||
volatile unsigned long lru_clock; /* Server global current LRU time. */
|
volatile unsigned long lru_clock; /* Server global current LRU time. */
|
||||||
|
std::mutex time_thread_mutex;
|
||||||
|
std::condition_variable time_thread_cv;
|
||||||
|
int sleeping_threads = cserver.cthreads;
|
||||||
|
void wakeTimeThread();
|
||||||
|
|
||||||
/* Our command table.
|
/* Our command table.
|
||||||
*
|
*
|
||||||
@ -2640,6 +2645,13 @@ void beforeSleep(struct aeEventLoop *eventLoop) {
|
|||||||
locker.disarm();
|
locker.disarm();
|
||||||
if (!fSentReplies)
|
if (!fSentReplies)
|
||||||
handleClientsWithPendingWrites(iel, aof_state);
|
handleClientsWithPendingWrites(iel, aof_state);
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(time_thread_mutex);
|
||||||
|
sleeping_threads++;
|
||||||
|
serverAssert(sleeping_threads <= cserver.cthreads);
|
||||||
|
}
|
||||||
|
|
||||||
if (moduleCount()) moduleReleaseGIL(TRUE /*fServerThread*/);
|
if (moduleCount()) moduleReleaseGIL(TRUE /*fServerThread*/);
|
||||||
|
|
||||||
/* Do NOT add anything below moduleReleaseGIL !!! */
|
/* Do NOT add anything below moduleReleaseGIL !!! */
|
||||||
@ -2655,6 +2667,8 @@ void afterSleep(struct aeEventLoop *eventLoop) {
|
|||||||
/* Aquire the modules GIL so that their threads won't touch anything. */
|
/* Aquire the modules GIL so that their threads won't touch anything. */
|
||||||
if (moduleCount()) moduleAcquireGIL(TRUE /*fServerThread*/);
|
if (moduleCount()) moduleAcquireGIL(TRUE /*fServerThread*/);
|
||||||
|
|
||||||
|
wakeTimeThread();
|
||||||
|
|
||||||
serverAssert(serverTL->gcEpoch.isReset());
|
serverAssert(serverTL->gcEpoch.isReset());
|
||||||
serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch();
|
serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch();
|
||||||
for (int idb = 0; idb < cserver.dbnum; ++idb)
|
for (int idb = 0; idb < cserver.dbnum; ++idb)
|
||||||
@ -6076,11 +6090,24 @@ void OnTerminate()
|
|||||||
serverPanic("std::teminate() called");
|
serverPanic("std::teminate() called");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void wakeTimeThread() {
|
||||||
|
std::lock_guard<std::mutex> lock(time_thread_mutex);
|
||||||
|
sleeping_threads--;
|
||||||
|
serverAssert(sleeping_threads >= 0);
|
||||||
|
time_thread_cv.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
void *timeThreadMain(void*) {
|
void *timeThreadMain(void*) {
|
||||||
timespec delay;
|
timespec delay;
|
||||||
delay.tv_sec = 0;
|
delay.tv_sec = 0;
|
||||||
delay.tv_nsec = 100;
|
delay.tv_nsec = 100;
|
||||||
while (true) {
|
while (true) {
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(time_thread_mutex);
|
||||||
|
if (sleeping_threads >= cserver.cthreads) {
|
||||||
|
time_thread_cv.wait(lock);
|
||||||
|
}
|
||||||
|
}
|
||||||
updateCachedTime();
|
updateCachedTime();
|
||||||
clock_nanosleep(CLOCK_REALTIME, 0, &delay, NULL);
|
clock_nanosleep(CLOCK_REALTIME, 0, &delay, NULL);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user