XNSim/XNCore/XNEventManager_p.h

243 lines
5.6 KiB
C
Raw Permalink Normal View History

2025-04-28 12:25:20 +08:00
#pragma once
#include "XNBaseFrameObject_p.h"
#include "XNEventManager.h"
#include <functional>
#include <sys/types.h>
#include <bits/pthreadtypes.h>
#include <bits/sched.h>
#include <vector>
#include <queue>
#include <mutex>
#include <condition_variable>
2025-05-20 15:39:40 +08:00
#include <string>
#include <any>
#include <map>
#include <list>
2025-04-28 12:25:20 +08:00
// 事件处理器信息结构
struct EventHandlerInfo {
2025-05-20 15:39:40 +08:00
std::function<void(const std::any &)> callback; // 回调函数
uint32_t objectId; // 对象ID
uint32_t localId; // 本地ID
2025-04-28 12:25:20 +08:00
bool isAsync; // 是否异步处理
XNEvent::Priority priority; // 事件优先级
2025-05-20 15:39:40 +08:00
uint32_t threadPriority; // 线程优先级
2025-04-28 12:25:20 +08:00
// 获取全局处理器ID
2025-05-20 15:39:40 +08:00
uint32_t GetHandlerId() const { return (objectId << 16) | (localId & 0xFFFF); }
2025-04-28 12:25:20 +08:00
// 从全局处理器ID中提取对象ID
2025-05-20 15:39:40 +08:00
static uint32_t GetObjectId(uint32_t handlerId) { return handlerId >> 16; }
2025-04-28 12:25:20 +08:00
// 从全局处理器ID中提取本地ID
2025-05-20 15:39:40 +08:00
static uint32_t GetLocalId(uint32_t handlerId) { return handlerId & 0xFFFF; }
2025-04-28 12:25:20 +08:00
};
// 事件任务基类
2025-05-20 15:39:40 +08:00
class BaseEventTask
2025-04-28 12:25:20 +08:00
{
public:
2025-05-20 15:39:40 +08:00
/**
* @brief
* @param name
* @param data
* @param callback
* @param manager
*/
BaseEventTask(const std::string &name, const std::any &data,
std::function<void(const std::any &)> callback, XNEventManager *manager)
2025-04-28 12:25:20 +08:00
: eventName(name), eventData(data), eventCallback(callback), eventManager(manager)
{
}
virtual ~BaseEventTask() = default;
2025-05-20 15:39:40 +08:00
/**
* @brief
*/
virtual void execute() = 0;
2025-04-28 12:25:20 +08:00
protected:
2025-05-20 15:39:40 +08:00
std::string eventName;
std::any eventData;
std::function<void(const std::any &)> eventCallback;
2025-04-28 12:25:20 +08:00
XNEventManager *eventManager;
};
2025-05-20 15:39:40 +08:00
// 异步事件任务
class AsyncEventTask : public BaseEventTask
2025-04-28 12:25:20 +08:00
{
public:
2025-05-20 15:39:40 +08:00
/**
* @brief
* @param name
* @param data
* @param callback
* @param manager
*/
AsyncEventTask(const std::string &name, const std::any &data,
std::function<void(const std::any &)> callback, XNEventManager *manager)
: BaseEventTask(name, data, callback, manager)
2025-04-28 12:25:20 +08:00
{
}
2025-05-20 15:39:40 +08:00
/**
* @brief
*/
void execute() override
2025-04-28 12:25:20 +08:00
{
try {
eventCallback(eventData);
if (eventManager) {
2025-05-20 15:39:40 +08:00
eventManager->EventProcessed(eventName, true);
2025-04-28 12:25:20 +08:00
}
} catch (const std::exception &e) {
2025-05-20 15:39:40 +08:00
LOG_ERROR("Async event handler exception for " + eventName + ": " + e.what());
2025-04-28 12:25:20 +08:00
if (eventManager) {
2025-05-20 15:39:40 +08:00
eventManager->EventProcessed(eventName, false);
2025-04-28 12:25:20 +08:00
}
}
}
2025-05-20 15:39:40 +08:00
};
2025-04-28 12:25:20 +08:00
2025-05-20 15:39:40 +08:00
// 实时事件任务
class RTEventTask : public BaseEventTask
{
public:
/**
* @brief
* @param name
* @param data
* @param callback
* @param manager
*/
RTEventTask(const std::string &name, const std::any &data,
std::function<void(const std::any &)> callback, XNEventManager *manager)
: BaseEventTask(name, data, callback, manager)
{
}
/**
* @brief
*/
void execute() override
{
try {
eventCallback(eventData);
if (eventManager) {
eventManager->EventProcessed(eventName, true);
}
} catch (const std::exception &e) {
LOG_ERROR("RT event handler exception for " + eventName + ": " + e.what());
if (eventManager) {
eventManager->EventProcessed(eventName, false);
}
}
}
2025-04-28 12:25:20 +08:00
};
// 实时线程管理器
class RTThreadManager
{
public:
RTThreadManager() : running(false) {}
~RTThreadManager() { stop(); }
void start(int threadCount, int priority, int cpuCore)
{
running = true;
for (int i = 0; i < threadCount; ++i) {
pthread_t thread;
pthread_create(&thread, nullptr, threadFunction, this);
// 设置线程优先级
struct sched_param param;
param.sched_priority = priority;
pthread_setschedparam(thread, SCHED_FIFO, &param);
// 设置CPU亲和性
cpu_set_t cpuset;
CPU_ZERO(&cpuset);
CPU_SET(cpuCore, &cpuset);
pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
threads.push_back(thread);
}
}
void stop()
{
running = false;
taskCond.notify_all();
for (auto thread : threads) {
pthread_join(thread, nullptr);
}
threads.clear();
}
void addTask(RTEventTask *task)
{
std::lock_guard<std::mutex> lock(taskMutex);
taskQueue.push(task);
taskCond.notify_one();
}
private:
static void *threadFunction(void *arg)
{
auto manager = static_cast<RTThreadManager *>(arg);
manager->processTask();
return nullptr;
}
void processTask()
{
while (running) {
RTEventTask *task = nullptr;
{
std::unique_lock<std::mutex> lock(taskMutex);
taskCond.wait(lock, [this] { return !taskQueue.empty() || !running; });
if (!running)
break;
task = taskQueue.front();
taskQueue.pop();
}
if (task) {
task->execute();
delete task;
}
}
}
bool running;
std::vector<pthread_t> threads;
std::queue<RTEventTask *> taskQueue;
std::mutex taskMutex;
std::condition_variable taskCond;
};
// 事件管理器的私有实现类
2025-05-20 15:39:40 +08:00
struct XNEventManagerPrivate : public XNBaseFrameObjectPrivate {
2025-04-28 12:25:20 +08:00
// 存储事件及其对应的处理器信息列表
// key: 事件名称
// value: 该事件对应的所有处理器信息列表
2025-05-20 15:39:40 +08:00
std::map<std::string, std::list<EventHandlerInfo>> eventHandlers;
2025-04-28 12:25:20 +08:00
// 处理器ID到事件名称的反向映射用于快速查找
2025-05-20 15:39:40 +08:00
std::map<int, std::string> handlerToEvent;
2025-04-28 12:25:20 +08:00
// 本地ID计数器
int localIdCounter = 0;
// 互斥锁,用于保护事件处理器表的线程安全访问
2025-05-20 15:39:40 +08:00
std::mutex eventMutex;
2025-04-28 12:25:20 +08:00
2025-05-20 15:39:40 +08:00
// 线程池相关
std::vector<std::thread> workerThreads;
std::queue<BaseEventTask *> taskQueue;
std::mutex taskMutex;
std::condition_variable taskCond;
bool running = true;
2025-04-28 12:25:20 +08:00
RTThreadManager rtManager;
};