example/heartbeat: Add signal handling
This makes the example a little more interesting as we now have multiple
sources and we can analyze memory safety with valgrind by just sending a
SIGINT to quit cleanly.
Change-Id: Id15fe58f798d3b137f91228025f48a5e4cd5fc50
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/example/heartbeat.cpp b/example/heartbeat.cpp
index c4adaac..f7390d7 100644
--- a/example/heartbeat.cpp
+++ b/example/heartbeat.cpp
@@ -2,17 +2,26 @@
#include <cstdio>
#include <sdeventplus/clock.hpp>
#include <sdeventplus/event.hpp>
+#include <sdeventplus/source/signal.hpp>
#include <sdeventplus/source/time.hpp>
+#include <stdplus/signal.hpp>
#include <string>
#include <utility>
using sdeventplus::Event;
using sdeventplus::source::Enabled;
+using sdeventplus::source::Signal;
constexpr auto clockId = sdeventplus::ClockId::RealTime;
using Clock = sdeventplus::Clock<clockId>;
using Time = sdeventplus::source::Time<clockId>;
+void intCb(Signal& signal, const struct signalfd_siginfo*)
+{
+ printf("Exiting\n");
+ signal.get_event().exit(0);
+}
+
int main(int argc, char* argv[])
{
if (argc != 2)
@@ -36,5 +45,7 @@
};
Time time(event, Clock(event).now(), std::chrono::seconds{1},
std::move(hbFunc));
+ stdplus::signal::block(SIGINT);
+ Signal signal(event, SIGINT, intCb);
return event.loop();
}