Skip to content

Commit 8173252

Browse files
iox-eclipse-iceoryx#2330 Add condition_variable to watchdog loop
Introduce std::condition_variable and std::mutex to replace sleep logic in the watchdog loop. This change aims to improve resource management and responsiveness to shutdown signals, making the watchdog more efficient.
1 parent 3c334a6 commit 8173252

File tree

1 file changed

+13
-1
lines changed
  • iceoryx_posh/include/iceoryx_posh/internal/roudi

1 file changed

+13
-1
lines changed

iceoryx_posh/include/iceoryx_posh/internal/roudi/roudi.hpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include <thread>
3939

4040
#ifdef USE_SYSTEMD
41+
#include <condition_variable>
4142
#include <systemd/sd-daemon.h>
4243
#endif
4344

@@ -143,8 +144,16 @@ class SystemdServiceHandler final : public ISystemd
143144
IOX_LOG(DEBUG, "WatchDog READY=1");
144145

145146
IOX_LOG(INFO, "Start watchdog");
147+
IOX_LOG(INFO, "std::condition_variable: " << sizeof(std::condition_variable));
148+
IOX_LOG(INFO, "std::mutex: " << sizeof(std::mutex));
146149
while (!m_shutdown.load())
147150
{
151+
std::unique_lock<std::mutex> lock(watchdogMutex);
152+
if (watchdogNotifyCondition.wait_for(
153+
lock, std::chrono::seconds(1), [this] { return m_shutdown.load(); }))
154+
{
155+
break;
156+
}
148157
auto resultWatchdog = IOX_POSIX_CALL(sd_notify)(0, "WATCHDOG=1").successReturnValue(1).evaluate();
149158
if (resultWatchdog.has_error())
150159
{
@@ -153,13 +162,16 @@ class SystemdServiceHandler final : public ISystemd
153162
+ resultWatchdog.get_error().getHumanReadableErrnum());
154163
return;
155164
}
156-
std::this_thread::sleep_for(std::chrono::seconds(1));
165+
/* maybe it's better to use sleep than mutex */
166+
// std::this_thread::sleep_for(std::chrono::seconds(1));
157167
}
158168
});
159169
}
160170
}
161171

162172
private:
173+
std::condition_variable watchdogNotifyCondition; // 48
174+
std::mutex watchdogMutex; // 40
163175
std::thread m_listenThreadWatchdog; // 8
164176
public:
165177
static constexpr uint16_t SIZE_STRING = 4096; // 2

0 commit comments

Comments
 (0)