William A. Kennington III | fa9431d | 2018-09-25 14:28:31 -0700 | [diff] [blame] | 1 | /** |
| 2 | * A simple example of a repeating timer that prints out a message for |
| 3 | * each timer expiration. |
| 4 | */ |
| 5 | |
| 6 | #include <chrono> |
| 7 | #include <cstdio> |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 8 | #include <functional> |
William A. Kennington III | fa9431d | 2018-09-25 14:28:31 -0700 | [diff] [blame] | 9 | #include <sdeventplus/clock.hpp> |
| 10 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 11 | #include <sdeventplus/source/signal.hpp> |
William A. Kennington III | fa9431d | 2018-09-25 14:28:31 -0700 | [diff] [blame] | 12 | #include <sdeventplus/utility/timer.hpp> |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 13 | #include <stdplus/signal.hpp> |
William A. Kennington III | fa9431d | 2018-09-25 14:28:31 -0700 | [diff] [blame] | 14 | #include <string> |
| 15 | |
| 16 | using sdeventplus::Clock; |
| 17 | using sdeventplus::ClockId; |
| 18 | using sdeventplus::Event; |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 19 | using sdeventplus::source::Signal; |
William A. Kennington III | fa9431d | 2018-09-25 14:28:31 -0700 | [diff] [blame] | 20 | |
| 21 | constexpr auto clockId = ClockId::RealTime; |
| 22 | using Timer = sdeventplus::utility::Timer<clockId>; |
| 23 | |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 24 | void intCb(Signal& signal, const struct signalfd_siginfo*) |
| 25 | { |
| 26 | printf("Exiting\n"); |
| 27 | signal.get_event().exit(0); |
| 28 | } |
| 29 | |
William A. Kennington III | fa9431d | 2018-09-25 14:28:31 -0700 | [diff] [blame] | 30 | int main(int argc, char* argv[]) |
| 31 | { |
| 32 | if (argc != 2) |
| 33 | { |
| 34 | fprintf(stderr, "Usage: %s [seconds]\n", argv[0]); |
| 35 | return 1; |
| 36 | } |
| 37 | |
| 38 | unsigned interval = std::stoul(argv[1]); |
| 39 | fprintf(stderr, "Beating every %u seconds\n", interval); |
| 40 | |
| 41 | auto event = Event::get_default(); |
William A. Kennington III | b315a2a | 2019-06-27 14:41:36 -0700 | [diff] [blame] | 42 | Timer timer( |
| 43 | event, [](Timer&) { printf("Beat\n"); }, |
| 44 | std::chrono::seconds{interval}); |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 45 | stdplus::signal::block(SIGINT); |
William A. Kennington III | 1b49b6f | 2019-03-23 21:07:03 -0700 | [diff] [blame^] | 46 | Signal(event, SIGINT, intCb).set_floating(true); |
William A. Kennington III | fa9431d | 2018-09-25 14:28:31 -0700 | [diff] [blame] | 47 | return event.loop(); |
| 48 | } |