132 lines
3.4 KiB
C
132 lines
3.4 KiB
C
![]() |
#ifndef LIBAPITHREAD_H
|
|||
|
#define LIBAPITHREAD_H
|
|||
|
|
|||
|
|
|||
|
#include <thread>
|
|||
|
#include <atomic>
|
|||
|
#include <mutex>
|
|||
|
#include <chrono>
|
|||
|
#include <condition_variable>
|
|||
|
#include <spdlog/async.h>
|
|||
|
#include "LibapiQueue.h"
|
|||
|
#include "LibapiMsg.h"
|
|||
|
|
|||
|
#define MACRO_FRAMES_RATE_DEFINE(x) \
|
|||
|
static int count##x = 0; \
|
|||
|
static int count_last##x = 0;\
|
|||
|
auto start##x = std::chrono::high_resolution_clock::now();\
|
|||
|
|
|||
|
#define MACRO_FRAMES_RATE_DEFINE_END(x, log)\
|
|||
|
count##x++;\
|
|||
|
auto finish##x = std::chrono::high_resolution_clock::now();\
|
|||
|
std::chrono::duration<double> elapsed##x = finish##x - start##x;\
|
|||
|
if (elapsed##x.count() > 1.0)\
|
|||
|
{\
|
|||
|
start##x = std::chrono::high_resolution_clock::now();\
|
|||
|
spdlog::info(log##" : frames per second is {}", count##x);\
|
|||
|
count##x = 0;\
|
|||
|
}\
|
|||
|
|
|||
|
#define MACRO_START_TIME(thread_name)\
|
|||
|
auto start = std::chrono::high_resolution_clock::now();\
|
|||
|
|
|||
|
|
|||
|
#define MACRO_RECORD_RUN_TIME(thread_name)\
|
|||
|
auto end = std::chrono::high_resolution_clock::now();\
|
|||
|
// m_elapsed = end - m_start;\
|
|||
|
// spdlog::info(pThisThread->get_name() + ":Elapsed Time:{} ms\n", m_elapsed.count());\
|
|||
|
|
|||
|
class LibapiThread;
|
|||
|
class IRunnable
|
|||
|
{
|
|||
|
public:
|
|||
|
virtual int OnProcess(LibapiThread* pthread, void* pMsg) = 0;
|
|||
|
virtual void OnEndProcess(LibapiThread* pthread, void* pMsg) = 0;
|
|||
|
virtual void OnDeleteMsg(LibapiThread* pthread, void* pMsg) = 0;
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
class BaseRunnable : IRunnable
|
|||
|
{
|
|||
|
public:
|
|||
|
std::chrono::steady_clock::time_point m_start;
|
|||
|
// std::chrono::steady_clock::time_point m_end;
|
|||
|
std::chrono::duration<double, std::milli> m_elapsed;
|
|||
|
public:
|
|||
|
virtual int OnProcess(LibapiThread* pthread, void* pMsg) { return 0; };
|
|||
|
virtual void OnEndProcess(LibapiThread* pthread, void* pMsg) {};
|
|||
|
virtual void OnDeleteMsg(LibapiThread* pthread, void* pMsg) {};
|
|||
|
public:
|
|||
|
void get_start_time()
|
|||
|
{
|
|||
|
auto start = std::chrono::high_resolution_clock::now();
|
|||
|
// m_start = start;
|
|||
|
}
|
|||
|
|
|||
|
// long get_used_time()
|
|||
|
// {
|
|||
|
// m_end = std::chrono::high_resolution_clock::now();
|
|||
|
// std::chrono::duration<double> elapsex = m_end - m_start;
|
|||
|
// long seconds = static_cast<long>(elapsex.count());
|
|||
|
|
|||
|
// return seconds;
|
|||
|
// }
|
|||
|
};
|
|||
|
|
|||
|
class IDeleteMsgCallBack
|
|||
|
{
|
|||
|
public:
|
|||
|
virtual void delete_msg(void* pMsg) = 0;
|
|||
|
};
|
|||
|
|
|||
|
class LibapiThread
|
|||
|
{
|
|||
|
public:
|
|||
|
static void delay_microseconds(int microseconds);
|
|||
|
static void delay_second(double second);
|
|||
|
|
|||
|
public:
|
|||
|
LibapiThread(int id, std::string name, BaseRunnable* runnable);
|
|||
|
virtual ~LibapiThread();
|
|||
|
|
|||
|
enum State
|
|||
|
{
|
|||
|
Stoped, ///<停止状态,包括从未启动过和启动后被停止
|
|||
|
Running, ///<运行状态
|
|||
|
Paused ///<暂停状态
|
|||
|
};
|
|||
|
|
|||
|
State state() const;
|
|||
|
void start();
|
|||
|
void stop();
|
|||
|
void join();
|
|||
|
void pause();
|
|||
|
void resume();
|
|||
|
bool push(void* pMsg); //push到本线程
|
|||
|
//void push_to(int queue_id, void* pMsg); //从本线程push到其他线程
|
|||
|
void* pop();
|
|||
|
void notify();
|
|||
|
int get_id();
|
|||
|
void set_id(int& id);
|
|||
|
std::string get_name();
|
|||
|
void set_name(std::string& name);
|
|||
|
LibapiQueue<void*>* get_queue();
|
|||
|
|
|||
|
protected:
|
|||
|
virtual void run();
|
|||
|
|
|||
|
protected:
|
|||
|
int m_id;
|
|||
|
std::string m_name;
|
|||
|
std::thread* m_pthread;
|
|||
|
BaseRunnable* m_prunnable;
|
|||
|
std::mutex m_mutex;
|
|||
|
std::mutex m_msg_mutex;
|
|||
|
std::condition_variable m_condition;
|
|||
|
std::atomic_bool m_pauseFlag; ///<暂停标识
|
|||
|
std::atomic_bool m_stopFlag; ///<停止标识
|
|||
|
State m_state;
|
|||
|
LibapiQueue<void*>* m_queue;
|
|||
|
};
|
|||
|
|
|||
|
#endif // LIBAPITHREAD_H
|