mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
330 lines
8.7 KiB
C
330 lines
8.7 KiB
C
4 years ago
|
/**
|
||
8 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
8 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
8 years ago
|
*
|
||
4 years ago
|
* By using this file, you agree to the terms and conditions set
|
||
|
* forth in the LICENSE file which can be found at the top level of
|
||
|
* the distribution.
|
||
8 years ago
|
*
|
||
|
*/
|
||
|
|
||
|
/******************************************************************************
|
||
|
* File: timers.h
|
||
|
* Description: Manually incremented timers. Call a callback function upon
|
||
|
* expiry.
|
||
|
* Reference:
|
||
|
*****************************************************************************/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_TIMERS_H
|
||
|
#define SRSRAN_TIMERS_H
|
||
8 years ago
|
|
||
4 years ago
|
#include "srsran/adt/move_callback.h"
|
||
|
#include "srsran/phy/utils/debug.h"
|
||
5 years ago
|
#include <algorithm>
|
||
5 years ago
|
#include <functional>
|
||
5 years ago
|
#include <limits>
|
||
5 years ago
|
#include <mutex>
|
||
5 years ago
|
#include <queue>
|
||
8 years ago
|
#include <stdint.h>
|
||
5 years ago
|
#include <stdio.h>
|
||
8 years ago
|
#include <time.h>
|
||
5 years ago
|
#include <vector>
|
||
8 years ago
|
|
||
4 years ago
|
namespace srsran {
|
||
5 years ago
|
|
||
|
class timer_callback
|
||
8 years ago
|
{
|
||
5 years ago
|
public:
|
||
|
virtual void timer_expired(uint32_t timer_id) = 0;
|
||
|
};
|
||
|
|
||
|
class timer_handler
|
||
5 years ago
|
{
|
||
5 years ago
|
constexpr static uint32_t MAX_TIMER_DURATION = std::numeric_limits<uint32_t>::max() / 4;
|
||
|
constexpr static uint32_t MAX_TIMER_VALUE = std::numeric_limits<uint32_t>::max() / 2;
|
||
|
|
||
5 years ago
|
struct timer_impl {
|
||
4 years ago
|
timer_handler* parent;
|
||
|
uint32_t duration = 0, timeout = 0;
|
||
|
bool running = false;
|
||
|
bool active = false;
|
||
4 years ago
|
srsran::move_callback<void(uint32_t)> callback;
|
||
5 years ago
|
|
||
5 years ago
|
explicit timer_impl(timer_handler* parent_) : parent(parent_) {}
|
||
|
|
||
|
uint32_t id() const { return std::distance((const timer_handler::timer_impl*)&parent->timer_list[0], this); }
|
||
|
|
||
|
bool is_running() const { return active and running and timeout > 0; }
|
||
|
|
||
5 years ago
|
bool is_expired() const { return active and not running and timeout > 0 and timeout <= parent->cur_time; }
|
||
|
|
||
5 years ago
|
uint32_t time_elapsed() const { return std::min(duration, parent->cur_time - (timeout - duration)); }
|
||
5 years ago
|
|
||
5 years ago
|
bool set(uint32_t duration_)
|
||
5 years ago
|
{
|
||
5 years ago
|
if (duration_ > MAX_TIMER_DURATION) {
|
||
4 years ago
|
ERROR("Error: timer durations above %u are not supported", MAX_TIMER_DURATION);
|
||
5 years ago
|
return false;
|
||
|
}
|
||
5 years ago
|
if (not active) {
|
||
4 years ago
|
ERROR("Error: setting inactive timer id=%d", id());
|
||
5 years ago
|
return false;
|
||
5 years ago
|
}
|
||
|
duration = duration_;
|
||
5 years ago
|
if (is_running()) {
|
||
|
// if already running, just extends timer lifetime
|
||
|
run();
|
||
|
}
|
||
5 years ago
|
return true;
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
bool set(uint32_t duration_, srsran::move_callback<void(uint32_t)> callback_)
|
||
5 years ago
|
{
|
||
5 years ago
|
if (set(duration_)) {
|
||
|
callback = std::move(callback_);
|
||
|
return true;
|
||
5 years ago
|
}
|
||
5 years ago
|
return false;
|
||
5 years ago
|
}
|
||
|
|
||
|
void run()
|
||
|
{
|
||
5 years ago
|
std::unique_lock<std::mutex> lock(parent->mutex);
|
||
5 years ago
|
if (not active) {
|
||
4 years ago
|
ERROR("Error: calling run() for inactive timer id=%d", id());
|
||
5 years ago
|
return;
|
||
|
}
|
||
|
timeout = parent->cur_time + duration;
|
||
5 years ago
|
parent->running_timers.emplace(id(), timeout);
|
||
5 years ago
|
running = true;
|
||
|
}
|
||
|
|
||
5 years ago
|
void stop()
|
||
|
{
|
||
|
running = false; // invalidates trigger
|
||
5 years ago
|
if (not is_expired()) {
|
||
|
timeout = 0; // if it has already expired, then do not alter is_expired() state
|
||
|
}
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
void clear()
|
||
|
{
|
||
5 years ago
|
stop();
|
||
5 years ago
|
duration = 0;
|
||
|
active = false;
|
||
4 years ago
|
callback = srsran::move_callback<void(uint32_t)>();
|
||
5 years ago
|
// leave run_id unchanged. Since the timeout was changed, we shall not get spurious triggering
|
||
5 years ago
|
}
|
||
|
|
||
|
void trigger()
|
||
|
{
|
||
|
if (is_running()) {
|
||
5 years ago
|
running = false;
|
||
4 years ago
|
if (not callback.is_empty()) {
|
||
5 years ago
|
callback(id());
|
||
|
}
|
||
5 years ago
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
public:
|
||
|
class unique_timer
|
||
|
{
|
||
|
public:
|
||
5 years ago
|
unique_timer() : timer_id(std::numeric_limits<decltype(timer_id)>::max()) {}
|
||
5 years ago
|
explicit unique_timer(timer_handler* parent_, uint32_t timer_id_) : parent(parent_), timer_id(timer_id_) {}
|
||
|
|
||
5 years ago
|
unique_timer(const unique_timer&) = delete;
|
||
5 years ago
|
|
||
5 years ago
|
unique_timer(unique_timer&& other) noexcept : parent(other.parent), timer_id(other.timer_id)
|
||
|
{
|
||
|
other.parent = nullptr;
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
~unique_timer()
|
||
|
{
|
||
5 years ago
|
if (parent != nullptr) {
|
||
5 years ago
|
// does not call callback
|
||
|
impl()->clear();
|
||
|
}
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
unique_timer& operator=(const unique_timer&) = delete;
|
||
5 years ago
|
|
||
|
unique_timer& operator=(unique_timer&& other) noexcept
|
||
5 years ago
|
{
|
||
|
if (this != &other) {
|
||
|
timer_id = other.timer_id;
|
||
|
parent = other.parent;
|
||
|
other.parent = nullptr;
|
||
|
}
|
||
|
return *this;
|
||
|
}
|
||
|
|
||
5 years ago
|
bool is_valid() const { return parent != nullptr; }
|
||
|
|
||
4 years ago
|
void set(uint32_t duration_, move_callback<void(uint32_t)> callback_)
|
||
|
{
|
||
|
impl()->set(duration_, std::move(callback_));
|
||
|
}
|
||
5 years ago
|
|
||
|
void set(uint32_t duration_) { impl()->set(duration_); }
|
||
|
|
||
5 years ago
|
bool is_set() const { return (impl()->duration != 0); }
|
||
|
|
||
5 years ago
|
bool is_running() const { return impl()->is_running(); }
|
||
|
|
||
|
bool is_expired() const { return impl()->is_expired(); }
|
||
|
|
||
5 years ago
|
uint32_t time_elapsed() const { return impl()->time_elapsed(); }
|
||
5 years ago
|
|
||
5 years ago
|
void run() { impl()->run(); }
|
||
|
|
||
|
void stop() { impl()->stop(); }
|
||
|
|
||
5 years ago
|
void release()
|
||
|
{
|
||
|
impl()->clear();
|
||
|
parent = nullptr;
|
||
|
}
|
||
|
|
||
5 years ago
|
uint32_t id() const { return timer_id; }
|
||
|
|
||
5 years ago
|
uint32_t duration() const { return impl()->duration; }
|
||
|
|
||
5 years ago
|
private:
|
||
5 years ago
|
timer_impl* impl() { return &parent->timer_list[timer_id]; }
|
||
|
|
||
5 years ago
|
const timer_impl* impl() const { return &parent->timer_list[timer_id]; }
|
||
|
|
||
5 years ago
|
timer_handler* parent = nullptr;
|
||
5 years ago
|
uint32_t timer_id;
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
explicit timer_handler(uint32_t capacity = 64)
|
||
5 years ago
|
{
|
||
|
timer_list.reserve(capacity);
|
||
|
// reserve a priority queue using a vector
|
||
|
std::vector<timer_run> v;
|
||
|
v.reserve(capacity);
|
||
|
std::priority_queue<timer_run> q(std::less<timer_run>(), std::move(v));
|
||
|
running_timers = std::move(q);
|
||
|
}
|
||
|
|
||
5 years ago
|
void step_all()
|
||
|
{
|
||
5 years ago
|
std::unique_lock<std::mutex> lock(mutex);
|
||
5 years ago
|
cur_time++;
|
||
5 years ago
|
while (not running_timers.empty()) {
|
||
|
uint32_t next_timeout = running_timers.top().timeout;
|
||
|
timer_impl* ptr = &timer_list[running_timers.top().timer_id];
|
||
|
if (not ptr->is_running() or next_timeout != ptr->timeout) {
|
||
|
// remove timers that were explicitly stopped, or re-run, to avoid unnecessary priority_queue growth
|
||
|
running_timers.pop();
|
||
|
continue;
|
||
|
}
|
||
|
if (cur_time < next_timeout) {
|
||
|
break;
|
||
|
}
|
||
5 years ago
|
// if the timer_run and timer_impl timeouts do not match, it means that timer_impl::timeout was overwritten.
|
||
|
// in such case, do not trigger
|
||
5 years ago
|
uint32_t timeout = running_timers.top().timeout;
|
||
|
running_timers.pop();
|
||
|
|
||
|
if (ptr->timeout == timeout) {
|
||
|
// unlock mutex, it could be that the callback tries to run a timer too
|
||
|
lock.unlock();
|
||
|
|
||
|
// Call callback
|
||
5 years ago
|
ptr->trigger();
|
||
5 years ago
|
|
||
|
// Lock again to keep protecting the queue
|
||
|
lock.lock();
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void stop_all()
|
||
|
{
|
||
|
// does not call callback
|
||
|
while (not running_timers.empty()) {
|
||
|
running_timers.pop();
|
||
|
}
|
||
5 years ago
|
for (auto& i : timer_list) {
|
||
|
i.running = false;
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
unique_timer get_unique_timer() { return unique_timer(this, alloc_timer()); }
|
||
5 years ago
|
|
||
|
uint32_t get_cur_time() const { return cur_time; }
|
||
5 years ago
|
|
||
5 years ago
|
uint32_t nof_timers() const
|
||
|
{
|
||
|
return std::count_if(timer_list.begin(), timer_list.end(), [](const timer_impl& t) { return t.active; });
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
uint32_t nof_running_timers() const
|
||
|
{
|
||
|
return std::count_if(timer_list.begin(), timer_list.end(), [](const timer_impl& t) { return t.is_running(); });
|
||
|
}
|
||
|
|
||
5 years ago
|
template <typename F>
|
||
|
void defer_callback(uint32_t duration, const F& func)
|
||
|
{
|
||
4 years ago
|
uint32_t id = alloc_timer();
|
||
4 years ago
|
srsran::move_callback<void(uint32_t)> c = [func, this, id](uint32_t tid) {
|
||
5 years ago
|
func();
|
||
|
// auto-deletes timer
|
||
|
timer_list[id].clear();
|
||
|
};
|
||
|
timer_list[id].set(duration, std::move(c));
|
||
|
timer_list[id].run();
|
||
|
}
|
||
|
|
||
5 years ago
|
private:
|
||
|
struct timer_run {
|
||
|
uint32_t timer_id;
|
||
|
uint32_t timeout;
|
||
5 years ago
|
|
||
|
timer_run(uint32_t timer_id_, uint32_t timeout_) : timer_id(timer_id_), timeout(timeout_) {}
|
||
|
|
||
5 years ago
|
bool operator<(const timer_run& other) const
|
||
|
{
|
||
5 years ago
|
// returns true, if other.timeout is lower than timeout, accounting for wrap around
|
||
5 years ago
|
if (timeout > other.timeout) {
|
||
5 years ago
|
return (timeout - other.timeout) < MAX_TIMER_VALUE / 2;
|
||
5 years ago
|
}
|
||
5 years ago
|
return (other.timeout - timeout) > MAX_TIMER_VALUE / 2;
|
||
5 years ago
|
}
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
uint32_t alloc_timer()
|
||
|
{
|
||
|
uint32_t i = 0;
|
||
|
for (; i < timer_list.size(); ++i) {
|
||
|
if (not timer_list[i].active) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if (i == timer_list.size()) {
|
||
|
timer_list.emplace_back(this);
|
||
|
}
|
||
|
timer_list[i].active = true;
|
||
|
return i;
|
||
|
}
|
||
|
|
||
5 years ago
|
std::vector<timer_impl> timer_list;
|
||
|
std::priority_queue<timer_run> running_timers;
|
||
|
uint32_t cur_time = 0;
|
||
5 years ago
|
std::mutex mutex; // Protect priority queue
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
using unique_timer = timer_handler::unique_timer;
|
||
|
|
||
4 years ago
|
} // namespace srsran
|
||
5 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_TIMERS_H
|