#ifndef LINUX_EVENT_H #define LINUX_EVENT_H #include #include #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 std::enable_shared_from_this { public: enum EventType { AUTO_RESET, MANUAL_RESET }; explicit LinuxEvent(EventType type = MANUAL_RESET); ~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(); // 获取生命周期跟踪ID uint64_t getObjectId() const { return m_objectId; } private: std::string getCurrentTimestamp() const; std::string getLogPrefix() const; void Initialize(bool initialState); mutable std::mutex m_mutex; int m_fd; EventType m_type; std::atomic m_state; std::string m_name; // 用于命名事件 const uint64_t m_objectId; std::atomic m_destructing{ false }; // 用于命名事件管理的静态成员 static std::mutex s_namedEventsMutex; static std::unordered_map> s_namedEvents; static std::atomic s_objectCounter; }; #endif // LINUX_EVENT_H