Skip to content

Commit d7b8c38

Browse files
committed
support: implementation of io timer source
1 parent d6092d0 commit d7b8c38

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
#pragma once
12+
13+
#include "srsran/support/io/io_broker.h"
14+
#include "srsran/support/io/unique_fd.h"
15+
#include "srsran/support/timers.h"
16+
17+
namespace srsran {
18+
19+
/// \brief Interface for a timer source.
20+
class io_timer_source
21+
{
22+
public:
23+
io_timer_source(timer_manager& tick_sink_,
24+
io_broker& broker_,
25+
std::chrono::milliseconds tick_period = std::chrono::milliseconds{1});
26+
27+
private:
28+
void read_time();
29+
30+
timer_manager& tick_sink;
31+
io_broker& broker;
32+
std::chrono::milliseconds tick_period;
33+
srslog::basic_logger& logger;
34+
35+
unique_fd timer_fd;
36+
37+
io_broker::subscriber io_sub;
38+
};
39+
40+
} // namespace srsran

lib/support/network/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_library(srsran_network
1010
transport_layer_address.cpp
1111
io_broker_factory.cpp
1212
io_broker_epoll.cpp
13+
io_timer_source.cpp
1314
sctp_socket.cpp
1415
sockets.cpp)
1516
target_link_libraries(srsran_network srslog)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)