use srslte::move_callback instead of std::function in timers to avoid allocations

master
Francisco 4 years ago committed by Francisco Paisana
parent 5a1bf28fe1
commit 0f9d73012f

@ -172,7 +172,7 @@ public:
R operator()(Args&&... args) const noexcept { return oper_ptr->call(&buffer, std::forward<Args>(args)...); } R operator()(Args&&... args) const noexcept { return oper_ptr->call(&buffer, std::forward<Args>(args)...); }
bool is_empty() const { return oper_ptr == empty_table; } bool is_empty() const { return oper_ptr == &empty_table; }
bool is_in_small_buffer() const { return oper_ptr->is_in_small_buffer(); } bool is_in_small_buffer() const { return oper_ptr->is_in_small_buffer(); }
private: private:

@ -20,6 +20,7 @@
#ifndef SRSLTE_TIMERS_H #ifndef SRSLTE_TIMERS_H
#define SRSLTE_TIMERS_H #define SRSLTE_TIMERS_H
#include "srslte/adt/move_callback.h"
#include "srslte/phy/utils/debug.h" #include "srslte/phy/utils/debug.h"
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
@ -45,11 +46,11 @@ class timer_handler
constexpr static uint32_t MAX_TIMER_VALUE = std::numeric_limits<uint32_t>::max() / 2; constexpr static uint32_t MAX_TIMER_VALUE = std::numeric_limits<uint32_t>::max() / 2;
struct timer_impl { struct timer_impl {
timer_handler* parent; timer_handler* parent;
uint32_t duration = 0, timeout = 0; uint32_t duration = 0, timeout = 0;
bool running = false; bool running = false;
bool active = false; bool active = false;
std::function<void(uint32_t)> callback; srslte::move_callback<void(uint32_t)> callback;
explicit timer_impl(timer_handler* parent_) : parent(parent_) {} explicit timer_impl(timer_handler* parent_) : parent(parent_) {}
@ -79,7 +80,7 @@ class timer_handler
return true; return true;
} }
bool set(uint32_t duration_, std::function<void(int)> callback_) bool set(uint32_t duration_, srslte::move_callback<void(uint32_t)> callback_)
{ {
if (set(duration_)) { if (set(duration_)) {
callback = std::move(callback_); callback = std::move(callback_);
@ -113,7 +114,7 @@ class timer_handler
stop(); stop();
duration = 0; duration = 0;
active = false; active = false;
callback = std::function<void(uint32_t)>(); callback = srslte::move_callback<void(uint32_t)>();
// leave run_id unchanged. Since the timeout was changed, we shall not get spurious triggering // leave run_id unchanged. Since the timeout was changed, we shall not get spurious triggering
} }
@ -121,7 +122,7 @@ class timer_handler
{ {
if (is_running()) { if (is_running()) {
running = false; running = false;
if (callback) { if (not callback.is_empty()) {
callback(id()); callback(id());
} }
} }
@ -164,7 +165,10 @@ public:
bool is_valid() const { return parent != nullptr; } bool is_valid() const { return parent != nullptr; }
void set(uint32_t duration_, const std::function<void(int)>& callback_) { impl()->set(duration_, callback_); } void set(uint32_t duration_, move_callback<void(uint32_t)> callback_)
{
impl()->set(duration_, std::move(callback_));
}
void set(uint32_t duration_) { impl()->set(duration_); } void set(uint32_t duration_) { impl()->set(duration_); }
@ -270,8 +274,8 @@ public:
template <typename F> template <typename F>
void defer_callback(uint32_t duration, const F& func) void defer_callback(uint32_t duration, const F& func)
{ {
uint32_t id = alloc_timer(); uint32_t id = alloc_timer();
std::function<void(uint32_t)> c = [func, this, id](uint32_t tid) { srslte::move_callback<void(uint32_t)> c = [func, this, id](uint32_t tid) {
func(); func();
// auto-deletes timer // auto-deletes timer
timer_list[id].clear(); timer_list[id].clear();

@ -48,10 +48,10 @@ public:
// Getter for the number of discard timers. Used for debugging. // Getter for the number of discard timers. Used for debugging.
size_t nof_discard_timers() const; size_t nof_discard_timers() const;
bool add_sdu(uint32_t sn, bool add_sdu(uint32_t sn,
const srslte::unique_byte_buffer_t& sdu, const srslte::unique_byte_buffer_t& sdu,
uint32_t discard_timeout, uint32_t discard_timeout,
const std::function<void(uint32_t)>& callback); srslte::move_callback<void(uint32_t)> callback);
unique_byte_buffer_t& operator[](uint32_t sn) unique_byte_buffer_t& operator[](uint32_t sn)
{ {

@ -782,10 +782,10 @@ undelivered_sdus_queue::undelivered_sdus_queue(srslte::task_sched_handle task_sc
} }
} }
bool undelivered_sdus_queue::add_sdu(uint32_t sn, bool undelivered_sdus_queue::add_sdu(uint32_t sn,
const srslte::unique_byte_buffer_t& sdu, const srslte::unique_byte_buffer_t& sdu,
uint32_t discard_timeout, uint32_t discard_timeout,
const std::function<void(uint32_t)>& callback) srslte::move_callback<void(uint32_t)> callback)
{ {
assert(not has_sdu(sn) && "Cannot add repeated SNs"); assert(not has_sdu(sn) && "Cannot add repeated SNs");
@ -824,7 +824,7 @@ bool undelivered_sdus_queue::add_sdu(uint32_t sn,
sdus[sn].sdu->N_bytes = sdu->N_bytes; sdus[sn].sdu->N_bytes = sdu->N_bytes;
memcpy(sdus[sn].sdu->msg, sdu->msg, sdu->N_bytes); memcpy(sdus[sn].sdu->msg, sdu->msg, sdu->N_bytes);
if (discard_timeout > 0) { if (discard_timeout > 0) {
sdus[sn].discard_timer.set(discard_timeout, callback); sdus[sn].discard_timer.set(discard_timeout, std::move(callback));
sdus[sn].discard_timer.run(); sdus[sn].discard_timer.run();
} }
sdus[sn].sdu->set_timestamp(); // Metrics sdus[sn].sdu->set_timestamp(); // Metrics

Loading…
Cancel
Save