diff options
| author | Martin Ridgers <[email protected]> | 2021-11-26 11:30:34 +0100 |
|---|---|---|
| committer | Martin Ridgers <[email protected]> | 2021-11-26 12:53:45 +0100 |
| commit | 07ab63559a07e50cb2221be0a9460b9f4097b787 (patch) | |
| tree | 37995e298648368462fd00b963a0b250937b27ae /zenserver/upstream/upstreamapply.cpp | |
| parent | Added .gdb_history to .gitignore (diff) | |
| download | zen-07ab63559a07e50cb2221be0a9460b9f4097b787.tar.xz zen-07ab63559a07e50cb2221be0a9460b9f4097b787.zip | |
Wait on an event instead of using sleep()
The sleep() means it can take up to "update_interval * 2" (currently 10
seconds) to shutdown the thread. Not only is it desirable that binaries
exit cleanly as quickly as possible, units tests were taking a
considerable amount of time to run due to the sleep.
Diffstat (limited to 'zenserver/upstream/upstreamapply.cpp')
| -rw-r--r-- | zenserver/upstream/upstreamapply.cpp | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/zenserver/upstream/upstreamapply.cpp b/zenserver/upstream/upstreamapply.cpp index 0118902a6..1779099ff 100644 --- a/zenserver/upstream/upstreamapply.cpp +++ b/zenserver/upstream/upstreamapply.cpp @@ -14,6 +14,7 @@ #include <zencore/session.h> #include <zencore/stats.h> #include <zencore/stream.h> +#include <zencore/thread.h> #include <zencore/timer.h> #include <zenstore/cas.h> @@ -1199,6 +1200,8 @@ public: if (m_RunState.IsRunning) { + m_ShutdownEvent.Reset(); + for (uint32_t Idx = 0; Idx < m_Options.ThreadCount; Idx++) { m_UpstreamThreads.emplace_back(&DefaultUpstreamApply::ProcessUpstreamQueue, this); @@ -1422,11 +1425,9 @@ private: void ProcessUpstreamUpdates() { - const auto& UpdateSleep = std::chrono::seconds(m_Options.UpdatesInterval); - for (;;) + const auto& UpdateSleep = std::chrono::milliseconds(m_Options.UpdatesInterval); + while (!m_ShutdownEvent.Wait(uint32_t(UpdateSleep.count()))) { - std::this_thread::sleep_for(UpdateSleep); - if (!m_RunState.IsRunning) { break; @@ -1489,6 +1490,8 @@ private: { if (m_RunState.Stop()) { + m_ShutdownEvent.Set(); + m_UpstreamQueue.CompleteAdding(); for (std::thread& Thread : m_UpstreamThreads) { @@ -1536,6 +1539,7 @@ private: UpstreamApplyTasks m_ApplyTasks; std::mutex m_ApplyTasksMutex; std::vector<std::unique_ptr<UpstreamApplyEndpoint>> m_Endpoints; + Event m_ShutdownEvent; std::vector<std::thread> m_UpstreamThreads; std::thread m_UpstreamUpdatesThread; std::thread m_EndpointMonitorThread; |