unit test normalization
This commit is contained in:
@@ -1,46 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <list>
|
||||
|
||||
#include "threading/sync/lockable.hpp"
|
||||
#include "threading/sync/spinlock.hpp"
|
||||
|
||||
template <typename value_type, typename lock_type = SpinLock>
|
||||
class LinkedList : public Lockable<lock_type>
|
||||
{
|
||||
public:
|
||||
std::size_t size() const
|
||||
{
|
||||
auto guard = this->acquire_unique();
|
||||
return data.size();
|
||||
}
|
||||
|
||||
void push_front(const value_type &value)
|
||||
{
|
||||
auto guard = this->acquire_unique();
|
||||
data.push_front(value);
|
||||
}
|
||||
|
||||
void push_front(value_type &&value)
|
||||
{
|
||||
auto guard = this->acquire_unique();
|
||||
data.push_front(std::forward<value_type>(value));
|
||||
}
|
||||
|
||||
void pop_front()
|
||||
{
|
||||
auto guard = this->acquire_unique();
|
||||
data.pop_front();
|
||||
}
|
||||
|
||||
// value_type& as return value
|
||||
// would not be concurrent
|
||||
value_type front()
|
||||
{
|
||||
auto guard = this->acquire_unique();
|
||||
return data.front();
|
||||
}
|
||||
|
||||
private:
|
||||
std::list<value_type> data;
|
||||
};
|
||||
@@ -8,13 +8,15 @@
|
||||
|
||||
#include "logging/default.hpp"
|
||||
|
||||
/** @class Timer
|
||||
* @brief The timer contains counter and handler.
|
||||
/**
|
||||
* @class Timer
|
||||
*
|
||||
* With every clock interval the counter should be decresed for
|
||||
* delta count. Delta count is one for now but it should be a variable in the
|
||||
* near future. The handler is function that will be called when counter
|
||||
* becomes zero or smaller than zero.
|
||||
* @brief The timer contains counter and handler.
|
||||
*
|
||||
* With every clock interval the counter should be decresed for
|
||||
* delta count. Delta count is one for now but it should be a variable in the
|
||||
* near future. The handler is function that will be called when counter
|
||||
* becomes zero or smaller than zero.
|
||||
*/
|
||||
struct Timer
|
||||
{
|
||||
@@ -48,14 +50,16 @@ struct Timer
|
||||
* the process method.
|
||||
*/
|
||||
|
||||
/** @class TimerSet
|
||||
* @brief Trivial timer container implementation.
|
||||
/**
|
||||
* @class TimerSet
|
||||
*
|
||||
* Internal data stucture for storage of timers is std::set. So, the
|
||||
* related timer complexities are:
|
||||
* insertion: O(log(n))
|
||||
* deletion: O(log(n))
|
||||
* process: O(n)
|
||||
* @brief Trivial timer container implementation.
|
||||
*
|
||||
* Internal data stucture for storage of timers is std::set. So, the
|
||||
* related timer complexities are:
|
||||
* insertion: O(log(n))
|
||||
* deletion: O(log(n))
|
||||
* process: O(n)
|
||||
*/
|
||||
class TimerSet
|
||||
{
|
||||
@@ -70,6 +74,11 @@ public:
|
||||
timers.erase(timer);
|
||||
}
|
||||
|
||||
uint64_t size() const
|
||||
{
|
||||
return timers.size();
|
||||
}
|
||||
|
||||
void process()
|
||||
{
|
||||
for (auto it = timers.begin(); it != timers.end(); ) {
|
||||
@@ -87,10 +96,17 @@ private:
|
||||
std::set<std::shared_ptr<Timer>> timers;
|
||||
};
|
||||
|
||||
/** @class TimerScheduler
|
||||
* @brief TimerScheduler is a manager class and its responsibility is to
|
||||
* take care of the time and call the timer_container process method in the
|
||||
* appropriate time.
|
||||
/**
|
||||
* @class TimerScheduler
|
||||
*
|
||||
* @brief TimerScheduler is a manager class and its responsibility is to
|
||||
* take care of the time and call the timer_container process method in the
|
||||
* appropriate time.
|
||||
*
|
||||
* @tparam timer_container_type implements a strategy how the timers
|
||||
* are processed
|
||||
* @tparam delta_time_type type of a time distance between two events
|
||||
* @tparam delta_time granularity between the two events, default value is 1
|
||||
*/
|
||||
template <
|
||||
typename timer_container_type,
|
||||
@@ -99,19 +115,47 @@ template <
|
||||
> class TimerScheduler
|
||||
{
|
||||
public:
|
||||
|
||||
/**
|
||||
* Adds a timer.
|
||||
*
|
||||
* @param timer shared pointer to the timer object \ref Timer
|
||||
*/
|
||||
void add(Timer::sptr timer)
|
||||
{
|
||||
timer_container.add(timer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a timer.
|
||||
*
|
||||
* @param timer shared pointer to the timer object \ref Timer
|
||||
*/
|
||||
void remove(Timer::sptr timer)
|
||||
{
|
||||
timer_container.remove(timer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the number of pending timers. The exact number has to be
|
||||
* provided by a timer_container.
|
||||
*
|
||||
* @return uint64_t the number of pending timers.
|
||||
*/
|
||||
uint64_t size() const
|
||||
{
|
||||
return timer_container.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a separate thread which responsibility is to run the process method
|
||||
* at the appropriate time (every delta_time from the beginning of
|
||||
* processing.
|
||||
*/
|
||||
void run()
|
||||
{
|
||||
is_running.store(true);
|
||||
|
||||
run_thread = std::thread([this]() {
|
||||
while (is_running.load()) {
|
||||
std::this_thread::sleep_for(delta_time_type(delta_time));
|
||||
@@ -121,11 +165,17 @@ public:
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the whole processing.
|
||||
*/
|
||||
void stop()
|
||||
{
|
||||
is_running.store(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins the processing thread.
|
||||
*/
|
||||
~TimerScheduler()
|
||||
{
|
||||
run_thread.join();
|
||||
|
||||
Reference in New Issue
Block a user