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.
144 lines
3.9 KiB
C
144 lines
3.9 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: thread_pool.h
|
||
5 years ago
|
* Description: Implements a pool of threads. Pending tasks to execute are
|
||
|
* identified by a pointer.
|
||
8 years ago
|
* Reference:
|
||
|
*****************************************************************************/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_THREAD_POOL_H
|
||
|
#define SRSRAN_THREAD_POOL_H
|
||
8 years ago
|
|
||
4 years ago
|
#include "srsran/adt/move_callback.h"
|
||
|
#include "srsran/srslog/srslog.h"
|
||
5 years ago
|
#include <condition_variable>
|
||
|
#include <functional>
|
||
|
#include <memory>
|
||
|
#include <mutex>
|
||
|
#include <queue>
|
||
|
#include <stack>
|
||
8 years ago
|
#include <stdint.h>
|
||
|
#include <string>
|
||
|
#include <vector>
|
||
|
|
||
4 years ago
|
#include "srsran/common/threads.h"
|
||
8 years ago
|
|
||
4 years ago
|
namespace srsran {
|
||
8 years ago
|
|
||
|
class thread_pool
|
||
|
{
|
||
|
public:
|
||
|
class worker : public thread
|
||
|
{
|
||
|
public:
|
||
6 years ago
|
worker();
|
||
5 years ago
|
~worker() = default;
|
||
|
void setup(uint32_t id, thread_pool* parent, uint32_t prio = 0, uint32_t mask = 255);
|
||
|
uint32_t get_id();
|
||
|
void release();
|
||
5 years ago
|
|
||
|
protected:
|
||
8 years ago
|
virtual void work_imp() = 0;
|
||
5 years ago
|
|
||
|
private:
|
||
5 years ago
|
uint32_t my_id = 0;
|
||
|
thread_pool* my_parent = nullptr;
|
||
|
|
||
5 years ago
|
void run_thread();
|
||
|
void wait_to_start();
|
||
|
void finished();
|
||
8 years ago
|
};
|
||
5 years ago
|
|
||
|
thread_pool(uint32_t nof_workers);
|
||
|
void init_worker(uint32_t id, worker*, uint32_t prio = 0, uint32_t mask = 255);
|
||
|
void stop();
|
||
5 years ago
|
worker* wait_worker_id(uint32_t id);
|
||
5 years ago
|
worker* wait_worker(uint32_t tti);
|
||
|
worker* wait_worker_nb(uint32_t tti);
|
||
|
void start_worker(worker*);
|
||
|
void start_worker(uint32_t id);
|
||
|
worker* get_worker(uint32_t id);
|
||
8 years ago
|
uint32_t get_nof_workers();
|
||
|
|
||
|
private:
|
||
5 years ago
|
bool find_finished_worker(uint32_t tti, uint32_t* id);
|
||
|
|
||
5 years ago
|
typedef enum { STOP, IDLE, START_WORK, WORKER_READY, WORKING } worker_status;
|
||
8 years ago
|
|
||
5 years ago
|
std::vector<worker*> workers = {};
|
||
|
uint32_t nof_workers = 0;
|
||
|
uint32_t max_workers = 0;
|
||
|
bool running = false;
|
||
|
std::condition_variable cvar_queue = {};
|
||
|
std::mutex mutex_queue = {};
|
||
|
std::vector<worker_status> status = {};
|
||
|
std::vector<std::condition_variable> cvar_worker = {};
|
||
8 years ago
|
};
|
||
5 years ago
|
|
||
|
class task_thread_pool
|
||
|
{
|
||
4 years ago
|
using task_t = srsran::move_callback<void()>;
|
||
5 years ago
|
|
||
|
public:
|
||
4 years ago
|
task_thread_pool(uint32_t nof_workers = 1, bool start_deferred = false, int32_t prio_ = -1, uint32_t mask_ = 255);
|
||
|
task_thread_pool(const task_thread_pool&) = delete;
|
||
|
task_thread_pool(task_thread_pool&&) = delete;
|
||
|
task_thread_pool& operator=(const task_thread_pool&) = delete;
|
||
|
task_thread_pool& operator=(task_thread_pool&&) = delete;
|
||
5 years ago
|
~task_thread_pool();
|
||
4 years ago
|
|
||
5 years ago
|
void stop();
|
||
4 years ago
|
void start(int32_t prio_ = -1, uint32_t mask_ = 255);
|
||
|
void set_nof_workers(uint32_t nof_workers);
|
||
5 years ago
|
|
||
|
void push_task(task_t&& task);
|
||
4 years ago
|
uint32_t nof_pending_tasks() const;
|
||
5 years ago
|
size_t nof_workers() const { return workers.size(); }
|
||
5 years ago
|
|
||
|
private:
|
||
|
class worker_t : public thread
|
||
|
{
|
||
|
public:
|
||
|
explicit worker_t(task_thread_pool* parent_, uint32_t id);
|
||
5 years ago
|
void stop();
|
||
5 years ago
|
bool is_running() const { return running; }
|
||
|
uint32_t id() const { return id_; }
|
||
|
|
||
|
void run_thread() override;
|
||
|
|
||
|
private:
|
||
|
bool wait_task(task_t* task);
|
||
|
|
||
|
task_thread_pool* parent = nullptr;
|
||
|
uint32_t id_ = 0;
|
||
|
bool running = false;
|
||
|
};
|
||
|
|
||
4 years ago
|
int32_t prio = -1;
|
||
|
uint32_t mask = 255;
|
||
|
srslog::basic_logger& logger;
|
||
|
|
||
|
std::queue<task_t> pending_tasks;
|
||
|
std::vector<std::unique_ptr<worker_t> > workers;
|
||
|
mutable std::mutex queue_mutex;
|
||
|
std::condition_variable cv_empty;
|
||
|
bool running = false;
|
||
5 years ago
|
};
|
||
|
|
||
4 years ago
|
srsran::task_thread_pool& get_background_workers();
|
||
4 years ago
|
|
||
4 years ago
|
} // namespace srsran
|
||
5 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_THREAD_POOL_H
|