blob: d9203acb28fc7a23ea1e7adf8353233f7dbde10f [file] [log] [blame]
William A. Kennington IIIfa9431d2018-09-25 14:28:31 -07001/**
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 III2584f9d2019-03-23 21:05:48 -07008#include <functional>
William A. Kennington IIIfa9431d2018-09-25 14:28:31 -07009#include <sdeventplus/clock.hpp>
10#include <sdeventplus/event.hpp>
William A. Kennington III2584f9d2019-03-23 21:05:48 -070011#include <sdeventplus/source/signal.hpp>
William A. Kennington IIIfa9431d2018-09-25 14:28:31 -070012#include <sdeventplus/utility/timer.hpp>
William A. Kennington III2584f9d2019-03-23 21:05:48 -070013#include <stdplus/signal.hpp>
William A. Kennington IIIfa9431d2018-09-25 14:28:31 -070014#include <string>
15
16using sdeventplus::Clock;
17using sdeventplus::ClockId;
18using sdeventplus::Event;
William A. Kennington III2584f9d2019-03-23 21:05:48 -070019using sdeventplus::source::Signal;
William A. Kennington IIIfa9431d2018-09-25 14:28:31 -070020
21constexpr auto clockId = ClockId::RealTime;
22using Timer = sdeventplus::utility::Timer<clockId>;
23
William A. Kennington III2584f9d2019-03-23 21:05:48 -070024void intCb(Signal& signal, const struct signalfd_siginfo*)
25{
26 printf("Exiting\n");
27 signal.get_event().exit(0);
28}
29
William A. Kennington IIIfa9431d2018-09-25 14:28:31 -070030int 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 IIIb315a2a2019-06-27 14:41:36 -070042 Timer timer(
43 event, [](Timer&) { printf("Beat\n"); },
44 std::chrono::seconds{interval});
William A. Kennington III2584f9d2019-03-23 21:05:48 -070045 stdplus::signal::block(SIGINT);
William A. Kennington III1b49b6f2019-03-23 21:07:03 -070046 Signal(event, SIGINT, intCb).set_floating(true);
William A. Kennington IIIfa9431d2018-09-25 14:28:31 -070047 return event.loop();
48}