allow for pre-reservation of timers vector and priority queue

master
Francisco Paisana 5 years ago
parent 50a1c9d557
commit 551192705e

@ -29,6 +29,7 @@
#ifndef SRSLTE_TIMERS_H #ifndef SRSLTE_TIMERS_H
#define SRSLTE_TIMERS_H #define SRSLTE_TIMERS_H
#include <algorithm>
#include <functional> #include <functional>
#include <limits> #include <limits>
#include <queue> #include <queue>
@ -284,6 +285,16 @@ public:
uint32_t timer_id; uint32_t timer_id;
}; };
explicit timers2(uint32_t capacity = 64)
{
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);
}
void step_all() void step_all()
{ {
cur_time++; cur_time++;
@ -325,6 +336,10 @@ public:
} }
uint32_t get_cur_time() const { return cur_time; } uint32_t get_cur_time() const { return cur_time; }
uint32_t nof_timers() const
{
return std::count_if(timer_list.begin(), timer_list.end(), [](const timer_impl& t) { return t.active; });
}
private: private:
struct timer_run { struct timer_run {

@ -37,85 +37,92 @@ int timers2_test()
timers2 timers; timers2 timers;
uint32_t dur = 5; uint32_t dur = 5;
timers2::unique_timer t = timers.get_unique_timer(); {
TESTASSERT(not t.is_running() and not t.is_expired()) timers2::unique_timer t = timers.get_unique_timer();
TESTASSERT(t.id() == 0) TESTASSERT(not t.is_running() and not t.is_expired())
timers2::unique_timer t2 = timers.get_unique_timer(); TESTASSERT(t.id() == 0)
TESTASSERT(not t2.is_running() and not t2.is_expired()) timers2::unique_timer t2 = timers.get_unique_timer();
TESTASSERT(t2.id() == 1) TESTASSERT(not t2.is_running() and not t2.is_expired())
TESTASSERT(t2.id() == 1)
TESTASSERT(timers.nof_timers() == 2)
// TEST: Run multiple times with the same duration // TEST: Run multiple times with the same duration
bool callback_called = false; bool callback_called = false;
t.set(dur, [&callback_called](int) { callback_called = true; }); t.set(dur, [&callback_called](int) { callback_called = true; });
for (uint32_t runs = 0; runs < 3; ++runs) { TESTASSERT(timers.get_cur_time() == 0)
callback_called = false; for (uint32_t runs = 0; runs < 3; ++runs) {
TESTASSERT(not t.is_running()) callback_called = false;
t.run(); TESTASSERT(not t.is_running())
TESTASSERT(t.is_running() and not t.is_expired()) t.run();
for (uint32_t i = 0; i < dur; ++i) {
timers.step_all();
TESTASSERT(t.is_running() and not t.is_expired()) TESTASSERT(t.is_running() and not t.is_expired())
for (uint32_t i = 0; i < dur; ++i) {
timers.step_all();
TESTASSERT(t.is_running() and not t.is_expired())
}
timers.step_all();
TESTASSERT(not t.is_running() and t.is_expired())
TESTASSERT(callback_called)
} }
timers.step_all(); TESTASSERT(timers.get_cur_time() == 3 * (1 + dur))
TESTASSERT(not t.is_running() and t.is_expired())
TESTASSERT(callback_called)
}
// TEST: interrupt a timer. check if callback was called // TEST: interrupt a timer. check if callback was called
callback_called = false; callback_called = false;
t.run(); t.run();
timers.step_all();
TESTASSERT(t.is_running())
t.stop();
TESTASSERT(not t.is_running())
for (uint32_t i = 0; i < dur; ++i) {
timers.step_all(); timers.step_all();
TESTASSERT(t.is_running())
t.stop();
TESTASSERT(not t.is_running()) TESTASSERT(not t.is_running())
} for (uint32_t i = 0; i < dur; ++i) {
TESTASSERT(not callback_called) timers.step_all();
TESTASSERT(not t.is_running())
}
TESTASSERT(not callback_called)
// TEST: call timer::run() when it is already running. Check if duration gets extended. // TEST: call timer::run() when it is already running. Check if duration gets extended.
callback_called = false; callback_called = false;
t.run(); t.run();
timers.step_all();
TESTASSERT(t.is_running())
t.run(); // re-run
for (uint32_t i = 0; i < dur; ++i) {
timers.step_all(); timers.step_all();
TESTASSERT(t.is_running()) TESTASSERT(t.is_running())
} t.run(); // re-run
timers.step_all(); for (uint32_t i = 0; i < dur; ++i) {
TESTASSERT(not t.is_running()) timers.step_all();
TESTASSERT(callback_called) TESTASSERT(t.is_running())
}
timers.step_all();
TESTASSERT(not t.is_running())
TESTASSERT(callback_called)
// TEST: ordering of timers is respected // TEST: ordering of timers is respected
timers2::unique_timer t3 = timers.get_unique_timer(); timers2::unique_timer t3 = timers.get_unique_timer();
TESTASSERT(t3.id() == 2) TESTASSERT(t3.id() == 2)
int first_id = -1, last_id = -1; int first_id = -1, last_id = -1;
auto callback = [&first_id, &last_id](int id) { auto callback = [&first_id, &last_id](int id) {
if (first_id < 0) { if (first_id < 0) {
printf("First timer id=%d\n", id); printf("First timer id=%d\n", id);
first_id = id; first_id = id;
}
last_id = id;
};
t.set(4, callback);
t2.set(2, callback);
t3.set(6, callback);
t.run();
t2.run();
t3.run();
for (uint32_t i = 0; i < 6; ++i) {
timers.step_all();
TESTASSERT(i >= 4 or t.is_running())
TESTASSERT(i >= 2 or t2.is_running())
TESTASSERT(t3.is_running())
} }
last_id = id;
};
t.set(4, callback);
t2.set(2, callback);
t3.set(6, callback);
t.run();
t2.run();
t3.run();
for (uint32_t i = 0; i < 6; ++i) {
timers.step_all(); timers.step_all();
TESTASSERT(i >= 4 or t.is_running()) TESTASSERT(t.is_expired() and t2.is_expired() and t3.is_expired())
TESTASSERT(i >= 2 or t2.is_running()) TESTASSERT(first_id == 1)
TESTASSERT(t3.is_running()) printf("Last timer id=%d\n", last_id);
TESTASSERT(last_id == 2)
} }
timers.step_all(); // TEST: timer dtor is called and removes "timer" from "timers"
TESTASSERT(t.is_expired() and t2.is_expired() and t3.is_expired()) TESTASSERT(timers.nof_timers() == 0)
TESTASSERT(first_id == 1)
printf("Last timer id=%d\n", last_id);
TESTASSERT(last_id == 2)
printf("Success\n"); printf("Success\n");

Loading…
Cancel
Save