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.
204 lines
4.6 KiB
C
204 lines
4.6 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
|
*
|
||
|
*/
|
||
|
|
||
4 years ago
|
#ifndef SRSRAN_THREADS_H
|
||
|
#define SRSRAN_THREADS_H
|
||
5 years ago
|
|
||
8 years ago
|
#include <pthread.h>
|
||
|
#include <stdint.h>
|
||
5 years ago
|
#include <stdio.h>
|
||
8 years ago
|
#include <sys/timerfd.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
7 years ago
|
// Default priority for all threads below UHD threads
|
||
|
#define DEFAULT_PRIORITY 60
|
||
|
|
||
8 years ago
|
#ifdef __cplusplus
|
||
5 years ago
|
extern "C" {
|
||
8 years ago
|
#endif // __cplusplus
|
||
|
|
||
5 years ago
|
bool threads_new_rt(pthread_t* thread, void* (*start_routine)(void*), void* arg);
|
||
|
bool threads_new_rt_prio(pthread_t* thread, void* (*start_routine)(void*), void* arg, int prio_offset);
|
||
|
bool threads_new_rt_cpu(pthread_t* thread, void* (*start_routine)(void*), void* arg, int cpu, int prio_offset);
|
||
|
bool threads_new_rt_mask(pthread_t* thread, void* (*start_routine)(void*), void* arg, int mask, int prio_offset);
|
||
|
void threads_print_self();
|
||
8 years ago
|
|
||
|
#ifdef __cplusplus
|
||
5 years ago
|
}
|
||
6 years ago
|
|
||
|
#include <string>
|
||
|
|
||
4 years ago
|
namespace srsran {
|
||
5 years ago
|
|
||
8 years ago
|
class thread
|
||
|
{
|
||
7 years ago
|
public:
|
||
6 years ago
|
thread(const std::string& name_) : _thread(0), name(name_) {}
|
||
5 years ago
|
|
||
5 years ago
|
thread(const thread&) = delete;
|
||
5 years ago
|
|
||
5 years ago
|
thread(thread&& other) noexcept
|
||
|
{
|
||
|
_thread = other._thread;
|
||
|
name = std::move(other.name);
|
||
|
other._thread = 0;
|
||
|
other.name = "";
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
virtual ~thread() = default;
|
||
|
|
||
5 years ago
|
thread& operator=(const thread&) = delete;
|
||
5 years ago
|
|
||
5 years ago
|
thread& operator=(thread&&) noexcept = delete;
|
||
5 years ago
|
|
||
|
bool start(int prio = -1) { return threads_new_rt_prio(&_thread, thread_function_entry, this, prio); }
|
||
|
|
||
|
bool start_cpu(int prio, int cpu) { return threads_new_rt_cpu(&_thread, thread_function_entry, this, cpu, prio); }
|
||
|
|
||
|
bool start_cpu_mask(int prio, int mask)
|
||
6 years ago
|
{
|
||
|
return threads_new_rt_mask(&_thread, thread_function_entry, this, mask, prio);
|
||
|
}
|
||
5 years ago
|
|
||
6 years ago
|
void print_priority() { threads_print_self(); }
|
||
5 years ago
|
|
||
6 years ago
|
void set_name(const std::string& name_)
|
||
|
{
|
||
|
name = name_;
|
||
|
pthread_setname_np(pthread_self(), name.c_str());
|
||
8 years ago
|
}
|
||
5 years ago
|
|
||
5 years ago
|
void wait_thread_finish() { pthread_join(_thread, NULL); }
|
||
5 years ago
|
|
||
5 years ago
|
void thread_cancel() { pthread_cancel(_thread); }
|
||
6 years ago
|
|
||
5 years ago
|
static std::string get_name()
|
||
|
{
|
||
|
const uint32_t MAX_LEN = 16;
|
||
|
char name[MAX_LEN] = {};
|
||
|
const pthread_t tid = pthread_self();
|
||
|
if (pthread_getname_np(tid, name, MAX_LEN)) {
|
||
|
perror("Could not get pthread name");
|
||
|
}
|
||
|
return std::string(name);
|
||
|
}
|
||
|
|
||
8 years ago
|
protected:
|
||
6 years ago
|
virtual void run_thread() = 0;
|
||
|
|
||
8 years ago
|
private:
|
||
6 years ago
|
static void* thread_function_entry(void* _this)
|
||
|
{
|
||
|
pthread_setname_np(pthread_self(), ((thread*)_this)->name.c_str());
|
||
|
((thread*)_this)->run_thread();
|
||
|
return NULL;
|
||
|
}
|
||
5 years ago
|
|
||
6 years ago
|
pthread_t _thread;
|
||
|
std::string name;
|
||
8 years ago
|
};
|
||
|
|
||
5 years ago
|
class periodic_thread : public thread
|
||
8 years ago
|
{
|
||
|
public:
|
||
6 years ago
|
periodic_thread(const std::string name_) : thread(name_) {}
|
||
5 years ago
|
|
||
5 years ago
|
void start_periodic(int period_us_, int priority = -1)
|
||
|
{
|
||
7 years ago
|
run_enable = true;
|
||
5 years ago
|
period_us = period_us_;
|
||
8 years ago
|
start(priority);
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
void stop_thread()
|
||
|
{
|
||
7 years ago
|
run_enable = false;
|
||
|
wait_thread_finish();
|
||
|
}
|
||
5 years ago
|
|
||
|
protected:
|
||
|
virtual void run_period() = 0;
|
||
|
|
||
8 years ago
|
private:
|
||
5 years ago
|
int wakeups_missed;
|
||
|
int timer_fd;
|
||
|
int period_us;
|
||
7 years ago
|
bool run_enable;
|
||
5 years ago
|
|
||
5 years ago
|
void run_thread()
|
||
|
{
|
||
8 years ago
|
if (make_periodic()) {
|
||
|
return;
|
||
|
}
|
||
5 years ago
|
while (run_enable) {
|
||
8 years ago
|
run_period();
|
||
7 years ago
|
if (run_enable) {
|
||
|
wait_period();
|
||
|
}
|
||
8 years ago
|
}
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
int make_periodic()
|
||
|
{
|
||
|
int ret = -1;
|
||
|
unsigned int ns;
|
||
|
unsigned int sec;
|
||
8 years ago
|
struct itimerspec itval;
|
||
|
|
||
|
/* Create the timer */
|
||
5 years ago
|
ret = timerfd_create(CLOCK_MONOTONIC, 0);
|
||
8 years ago
|
wakeups_missed = 0;
|
||
5 years ago
|
timer_fd = ret;
|
||
8 years ago
|
if (ret > 0) {
|
||
|
/* Make the timer periodic */
|
||
5 years ago
|
sec = period_us / 1e6;
|
||
|
ns = (period_us - (sec * 1000000)) * 1000;
|
||
|
itval.it_interval.tv_sec = sec;
|
||
8 years ago
|
itval.it_interval.tv_nsec = ns;
|
||
5 years ago
|
itval.it_value.tv_sec = sec;
|
||
|
itval.it_value.tv_nsec = ns;
|
||
|
ret = timerfd_settime(timer_fd, 0, &itval, NULL);
|
||
8 years ago
|
if (ret < 0) {
|
||
|
perror("timerfd_settime");
|
||
|
}
|
||
|
} else {
|
||
|
perror("timerfd_create");
|
||
|
}
|
||
|
return ret;
|
||
|
}
|
||
5 years ago
|
|
||
5 years ago
|
void wait_period()
|
||
|
{
|
||
8 years ago
|
unsigned long long missed;
|
||
5 years ago
|
int ret;
|
||
8 years ago
|
|
||
|
/* Wait for the next timer event. If we have missed any the
|
||
|
number is written to "missed" */
|
||
5 years ago
|
ret = read(timer_fd, &missed, sizeof(missed));
|
||
|
if (ret == -1) {
|
||
|
perror("read timer");
|
||
8 years ago
|
return;
|
||
|
}
|
||
|
|
||
|
/* "missed" should always be >= 1, but just to be sure, check it is not 0 anyway */
|
||
|
if (missed > 0) {
|
||
|
wakeups_missed += (missed - 1);
|
||
|
}
|
||
|
}
|
||
5 years ago
|
};
|
||
8 years ago
|
|
||
4 years ago
|
} // namespace srsran
|
||
5 years ago
|
|
||
5 years ago
|
#endif // __cplusplus
|
||
8 years ago
|
|
||
4 years ago
|
#endif // SRSRAN_THREADS_H
|