#ifndef LINUX_EVENT_H #define LINUX_EVENT_H #include #include #include #include #include #include #include #include #include #include #include #include // 辅助函数:生成带时间戳的日志前缀 inline std::string getLogPrefix() { auto now = std::chrono::system_clock::now(); std::time_t time = std::chrono::system_clock::to_time_t(now); char timeStr[26]; ctime_r(&time, timeStr); timeStr[24] = '\0'; // 移除换行符 return "[" + std::string(timeStr) + "] [WARNING] "; } class LinuxEvent { public: enum EventType { AUTO_RESET, MANUAL_RESET }; LinuxEvent(EventType type = MANUAL_RESET, bool initialState = false); ~LinuxEvent(); // 禁止拷贝和赋值 LinuxEvent(const LinuxEvent&) = delete; LinuxEvent& operator=(const LinuxEvent&) = delete; void SetEvent(); void ResetEvent(); bool IsSet() const; int GetFD() const; bool Wait(unsigned long timeoutMs = (unsigned long)-1); static int WaitForMultipleEvents(LinuxEvent** events, int count, unsigned long timeoutMs); static int WaitForMultipleEvents(std::vector>& events, unsigned long timeoutMs); // 跨进程支持 static std::shared_ptr OpenEvent(const char* name, bool inheritHandle = false); static std::shared_ptr CreateEvent(EventType type, bool initialState, const char* name =""); static void CleanupNamedEvents(); private: mutable std::mutex m_mutex; int m_fd; EventType m_type; bool m_state; std::string m_name; // 用于命名事件 // 用于命名事件管理的静态成员 static std::mutex s_namedEventsMutex; static std::unordered_map> s_namedEvents; }; #endif // LINUX_EVENT_H