|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2021-2024 Software Radio Systems Limited |
| 4 | + * |
| 5 | + * By using this file, you agree to the terms and conditions set |
| 6 | + * forth in the LICENSE file which can be found at the top level of |
| 7 | + * the distribution. |
| 8 | + * |
| 9 | + */ |
| 10 | + |
| 11 | +#include "srsran/support/io/io_timer_source.h" |
| 12 | +#include "srsran/support/error_handling.h" |
| 13 | +#include "srsran/support/io/io_broker.h" |
| 14 | +#include "srsran/support/timers.h" |
| 15 | +#include <sys/timerfd.h> |
| 16 | + |
| 17 | +using namespace srsran; |
| 18 | + |
| 19 | +io_timer_source::io_timer_source(timer_manager& tick_sink_, |
| 20 | + io_broker& broker_, |
| 21 | + std::chrono::milliseconds tick_period_) : |
| 22 | + tick_sink(tick_sink_), broker(broker_), tick_period(tick_period_), logger(srslog::fetch_basic_logger("IO-EPOLL")) |
| 23 | +{ |
| 24 | + timer_fd = unique_fd{timerfd_create(CLOCK_REALTIME, 0)}; |
| 25 | + if (not timer_fd.is_open()) { |
| 26 | + report_fatal_error_if_not("Failed to create timer source (errno={})", strerror(errno)); |
| 27 | + } |
| 28 | + |
| 29 | + struct itimerspec timerspec = {{0, 0}, |
| 30 | + {std::chrono::duration_cast<std::chrono::seconds>(tick_period).count(), |
| 31 | + std::chrono::duration_cast<std::chrono::nanoseconds>(tick_period).count()}}; |
| 32 | + timerfd_settime(timer_fd.value(), 0, &timerspec, nullptr); |
| 33 | + |
| 34 | + io_sub = broker.register_fd( |
| 35 | + timer_fd.value(), [this]() { read_time(); }, [this](io_broker::error_code ev) { io_sub.reset(); }); |
| 36 | +} |
| 37 | + |
| 38 | +void io_timer_source::read_time() |
| 39 | +{ |
| 40 | + char read_buffer[8]; |
| 41 | + int n = read(timer_fd.value(), read_buffer, sizeof(read_buffer)); |
| 42 | + if (n < 0) { |
| 43 | + logger.error("Failed to read timerfd (errno={})", strerror(errno)); |
| 44 | + return; |
| 45 | + } |
| 46 | + if (n == 0) { |
| 47 | + logger.warning("Timerfd read returned 0"); |
| 48 | + } |
| 49 | + |
| 50 | + // Tick timers. |
| 51 | + tick_sink.tick(); |
| 52 | +} |
0 commit comments