From 301e4f6a16b8a7c9ebaf9cde50527ed720e944e1 Mon Sep 17 00:00:00 2001 From: christianEQ Date: Wed, 24 Mar 2021 19:27:39 +0000 Subject: [PATCH] added condition variable to time thread; wake on afterSleep, sleep on beforeSleep Former-commit-id: ff2f2a3aceff2ba4a74951197348d67fc39568b2 --- src/server.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/server.cpp b/src/server.cpp index fbe0458e3..6c3f740af 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -61,6 +61,7 @@ #include #include #include +#include #include "aelocker.h" #include "keycheck.h" #include "motd.h" @@ -97,6 +98,10 @@ redisServer *g_pserver = &GlobalHidden::server; struct redisServerConst cserver; thread_local struct redisServerThreadVars *serverTL = NULL; // thread local server vars volatile unsigned long lru_clock; /* Server global current LRU time. */ +std::mutex time_thread_mutex; +std::condition_variable time_thread_cv; +bool time_thread_running = false; +void wakeTimeThread(); /* Our command table. * @@ -2509,6 +2514,8 @@ void beforeSleep(struct aeEventLoop *eventLoop) { serverAssert(g_pserver->repl_batch_offStart < 0); runAndPropogateToReplicas(processClients); + time_thread_running = false; + /* Handle precise timeouts of blocked clients. */ handleBlockedClientsTimeout(); @@ -2655,6 +2662,8 @@ void afterSleep(struct aeEventLoop *eventLoop) { /* Aquire the modules GIL so that their threads won't touch anything. */ if (moduleCount()) moduleAcquireGIL(TRUE /*fServerThread*/); + wakeTimeThread(); + serverAssert(serverTL->gcEpoch.isReset()); serverTL->gcEpoch = g_pserver->garbageCollector.startEpoch(); for (int idb = 0; idb < cserver.dbnum; ++idb) @@ -5731,6 +5740,8 @@ static void sigShutdownHandler(int sig) { msg = "Received shutdown signal, scheduling shutdown..."; }; + wakeTimeThread(); + /* SIGINT is often delivered via Ctrl+C in an interactive session. * If we receive the signal the second time, we interpret this as * the user really wanting to quit ASAP without waiting to persist @@ -6076,15 +6087,24 @@ void OnTerminate() serverPanic("std::teminate() called"); } +void wakeTimeThread() { + time_thread_running = true; + time_thread_cv.notify_one(); +} + void *timeThreadMain(void*) { timespec delay; delay.tv_sec = 0; delay.tv_nsec = 100; while (true) { + std::unique_lock lock(time_thread_mutex); + if (!time_thread_running) { + time_thread_cv.wait(lock); + } updateCachedTime(); clock_nanosleep(CLOCK_REALTIME, 0, &delay, NULL); } -} +} void *workerThreadMain(void *parg) {