diff options
| author | William A. Kennington III <wak@google.com> | 2019-03-23 21:05:48 -0700 |
|---|---|---|
| committer | William A. Kennington III <wak@google.com> | 2019-03-29 15:10:15 -0700 |
| commit | 2584f9d7b91e2d14ddc9bdcb45fc5133a083b522 (patch) | |
| tree | e6cb265a5088b3fbc3ace4568d45c588c9ef5bcc /example | |
| parent | 9ce0d10bd5f10c9aa218817df879388d7d169379 (diff) | |
| download | sdeventplus-2584f9d7b91e2d14ddc9bdcb45fc5133a083b522.tar.gz sdeventplus-2584f9d7b91e2d14ddc9bdcb45fc5133a083b522.zip | |
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>
Diffstat (limited to 'example')
| -rw-r--r-- | example/Makefile.am | 4 | ||||
| -rw-r--r-- | example/heartbeat.cpp | 11 | ||||
| -rw-r--r-- | example/heartbeat_timer.cpp | 12 | ||||
| -rw-r--r-- | example/meson.build | 3 |
4 files changed, 27 insertions, 3 deletions
diff --git a/example/Makefile.am b/example/Makefile.am index fcdbaa5..c9a96fc 100644 --- a/example/Makefile.am +++ b/example/Makefile.am @@ -12,10 +12,10 @@ follow_LDADD = $(SDEVENTPLUS_LIBS) noinst_PROGRAMS += heartbeat heartbeat_SOURCES = heartbeat.cpp -heartbeat_LDADD = $(SDEVENTPLUS_LIBS) +heartbeat_LDADD = $(SDEVENTPLUS_LIBS) $(STDPLUS_LIBS) noinst_PROGRAMS += heartbeat_timer heartbeat_timer_SOURCES = heartbeat_timer.cpp -heartbeat_timer_LDADD = $(SDEVENTPLUS_LIBS) +heartbeat_timer_LDADD = $(SDEVENTPLUS_LIBS) $(STDPLUS_LIBS) endif 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 @@ int main(int argc, char* argv[]) }; 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(); } diff --git a/example/heartbeat_timer.cpp b/example/heartbeat_timer.cpp index c8354e8..63c33c0 100644 --- a/example/heartbeat_timer.cpp +++ b/example/heartbeat_timer.cpp @@ -5,18 +5,28 @@ #include <chrono> #include <cstdio> +#include <functional> #include <sdeventplus/clock.hpp> #include <sdeventplus/event.hpp> +#include <sdeventplus/source/signal.hpp> #include <sdeventplus/utility/timer.hpp> +#include <stdplus/signal.hpp> #include <string> using sdeventplus::Clock; using sdeventplus::ClockId; using sdeventplus::Event; +using sdeventplus::source::Signal; constexpr auto clockId = ClockId::RealTime; using Timer = sdeventplus::utility::Timer<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) @@ -31,5 +41,7 @@ int main(int argc, char* argv[]) auto event = Event::get_default(); Timer timer(event, [](Timer&) { printf("Beat\n"); }, std::chrono::seconds{interval}); + stdplus::signal::block(SIGINT); + Signal signal(event, SIGINT, intCb); return event.loop(); } diff --git a/example/meson.build b/example/meson.build index 1f18444..ea04ff5 100644 --- a/example/meson.build +++ b/example/meson.build @@ -9,5 +9,6 @@ foreach example : examples executable(example, example + '.cpp', include_directories: includes, implicit_include_directories: false, - link_with: sdeventplus) + link_with: sdeventplus, + dependencies: stdplus) endforeach |

