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.
157 lines
4.7 KiB
C
157 lines
4.7 KiB
C
4 years ago
|
/**
|
||
5 years ago
|
*
|
||
4 years ago
|
* \section COPYRIGHT
|
||
5 years ago
|
*
|
||
4 years ago
|
* Copyright 2013-2021 Software Radio Systems Limited
|
||
5 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.
|
||
5 years ago
|
*
|
||
|
*/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_TASK_SCHEDULER_H
|
||
|
#define SRSRAN_TASK_SCHEDULER_H
|
||
5 years ago
|
|
||
|
#include "interfaces_common.h"
|
||
|
#include "multiqueue.h"
|
||
|
#include "thread_pool.h"
|
||
5 years ago
|
#include "timers.h"
|
||
5 years ago
|
|
||
4 years ago
|
namespace srsran {
|
||
5 years ago
|
|
||
5 years ago
|
class task_scheduler
|
||
5 years ago
|
{
|
||
|
public:
|
||
4 years ago
|
explicit task_scheduler(uint32_t default_extern_tasks_size = 512, uint32_t nof_timers_prealloc = 100) :
|
||
|
external_tasks{default_extern_tasks_size}, timers{nof_timers_prealloc}
|
||
5 years ago
|
{
|
||
|
background_queue_id = external_tasks.add_queue();
|
||
|
}
|
||
4 years ago
|
task_scheduler(const task_scheduler&) = delete;
|
||
|
task_scheduler(task_scheduler&&) = delete;
|
||
|
task_scheduler& operator=(const task_scheduler&) = delete;
|
||
|
task_scheduler& operator=(task_scheduler&&) = delete;
|
||
5 years ago
|
|
||
4 years ago
|
void stop() { external_tasks.reset(); }
|
||
5 years ago
|
|
||
4 years ago
|
srsran::unique_timer get_unique_timer() { return timers.get_unique_timer(); }
|
||
5 years ago
|
|
||
|
//! Creates new queue for tasks coming from external thread
|
||
4 years ago
|
srsran::task_queue_handle make_task_queue() { return external_tasks.get_queue_handler(); }
|
||
|
srsran::task_queue_handle make_task_queue(uint32_t qsize) { return external_tasks.get_queue_handler(qsize); }
|
||
5 years ago
|
|
||
|
//! Delays a task processing by duration_ms
|
||
4 years ago
|
template <typename F>
|
||
|
void defer_callback(uint32_t duration_ms, F&& func)
|
||
|
{
|
||
|
timers.defer_callback(duration_ms, std::forward<F>(func));
|
||
|
}
|
||
5 years ago
|
|
||
|
//! Enqueues internal task to be run in next tic
|
||
4 years ago
|
void defer_task(srsran::move_task_t func) { internal_tasks.push_back(std::move(func)); }
|
||
5 years ago
|
|
||
|
//! Defer the handling of the result of a background task to next tic
|
||
4 years ago
|
void notify_background_task_result(srsran::move_task_t task)
|
||
5 years ago
|
{
|
||
|
// run the notification in next tic
|
||
|
external_tasks.push(background_queue_id, std::move(task));
|
||
|
}
|
||
|
|
||
|
//! Updates timers, and run any pending internal tasks.
|
||
|
// CAUTION: Should be called in main thread
|
||
5 years ago
|
void tic() { timers.step_all(); }
|
||
5 years ago
|
|
||
|
//! Processes the next task in the multiqueue.
|
||
|
// CAUTION: This is a blocking call
|
||
5 years ago
|
bool run_next_task()
|
||
5 years ago
|
{
|
||
4 years ago
|
srsran::move_task_t task{};
|
||
5 years ago
|
if (external_tasks.wait_pop(&task) >= 0) {
|
||
|
task();
|
||
5 years ago
|
run_all_internal_tasks();
|
||
5 years ago
|
return true;
|
||
|
}
|
||
5 years ago
|
run_all_internal_tasks();
|
||
5 years ago
|
return false;
|
||
|
}
|
||
|
|
||
|
//! Processes the next task in the multiqueue if it exists.
|
||
5 years ago
|
void run_pending_tasks()
|
||
5 years ago
|
{
|
||
5 years ago
|
run_all_internal_tasks();
|
||
4 years ago
|
srsran::move_task_t task{};
|
||
5 years ago
|
while (external_tasks.try_pop(&task) >= 0) {
|
||
5 years ago
|
task();
|
||
5 years ago
|
run_all_internal_tasks();
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
srsran::timer_handler* get_timer_handler() { return &timers; }
|
||
5 years ago
|
|
||
|
private:
|
||
|
void run_all_internal_tasks()
|
||
|
{
|
||
|
// Perform pending stack deferred tasks
|
||
4 years ago
|
// Note: Using a deque because tasks can enqueue new tasks, which would lead to
|
||
|
// reference invalidation in case of vector
|
||
|
while (not internal_tasks.empty()) {
|
||
|
internal_tasks.front()();
|
||
|
internal_tasks.pop_front();
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
int background_queue_id = -1; ///< Queue for handling the outcomes of tasks run in the background
|
||
4 years ago
|
srsran::task_multiqueue external_tasks;
|
||
|
srsran::timer_handler timers;
|
||
|
std::deque<srsran::move_task_t> internal_tasks; ///< enqueues stack tasks from within main thread. Avoids locking
|
||
5 years ago
|
};
|
||
|
|
||
5 years ago
|
//! Task scheduler handle given to classes/functions running within the main control thread
|
||
5 years ago
|
class task_sched_handle
|
||
|
{
|
||
|
public:
|
||
|
task_sched_handle(task_scheduler* sched_) : sched(sched_) {}
|
||
|
|
||
4 years ago
|
srsran::unique_timer get_unique_timer() { return sched->get_unique_timer(); }
|
||
|
void notify_background_task_result(srsran::move_task_t task)
|
||
5 years ago
|
{
|
||
|
sched->notify_background_task_result(std::move(task));
|
||
|
}
|
||
4 years ago
|
template <typename F>
|
||
|
void defer_callback(uint32_t duration_ms, F&& func)
|
||
5 years ago
|
{
|
||
4 years ago
|
sched->defer_callback(duration_ms, std::forward<F>(func));
|
||
5 years ago
|
}
|
||
4 years ago
|
void defer_task(srsran::move_task_t func) { sched->defer_task(std::move(func)); }
|
||
5 years ago
|
|
||
|
private:
|
||
|
task_scheduler* sched;
|
||
|
};
|
||
|
|
||
5 years ago
|
//! Task scheduler handle given to classes/functions running outside of main control thread
|
||
|
class ext_task_sched_handle
|
||
|
{
|
||
|
public:
|
||
|
ext_task_sched_handle(task_scheduler* sched_) : sched(sched_) {}
|
||
|
|
||
4 years ago
|
srsran::unique_timer get_unique_timer() { return sched->get_unique_timer(); }
|
||
|
void notify_background_task_result(srsran::move_task_t task)
|
||
5 years ago
|
{
|
||
|
sched->notify_background_task_result(std::move(task));
|
||
|
}
|
||
4 years ago
|
srsran::task_queue_handle make_task_queue() { return sched->make_task_queue(); }
|
||
4 years ago
|
template <typename F>
|
||
|
void defer_callback(uint32_t duration_ms, F&& func)
|
||
5 years ago
|
{
|
||
4 years ago
|
sched->defer_callback(duration_ms, std::forward<F>(func));
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
private:
|
||
|
task_scheduler* sched;
|
||
|
};
|
||
|
|
||
4 years ago
|
} // namespace srsran
|
||
5 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_TASK_SCHEDULER_H
|