Forráskód Böngészése

提交最新修改的LinuxEvent

lwk 1 hete
szülő
commit
95d306031e
1 módosított fájl, 27 hozzáadás és 29 törlés
  1. 27 29
      include/LinuxEvent.cpp

+ 27 - 29
include/LinuxEvent.cpp

@@ -71,42 +71,42 @@ void LinuxEvent::Initialize(bool initialState) {
 }
 
 void LinuxEvent::SetEvent() {
-    if (this == nullptr) {  // 关键空指针检查
-        std::cerr << "[CRITICAL] SetEvent called with NULL pointer!" << std::endl;
-        return;  
-    }
-    auto self = shared_from_this();
-
-    std::lock_guard<std::mutex> lock(m_mutex);
-  
-    // 检查析构状态
-    if (m_destructing.load()) {
-        std::cerr << getLogPrefix() << "SetEvent ABORTED: Object is being destroyed" << std::endl;
+    // 三重安全检查
+    if (this == nullptr || m_destructing.load() || m_fd == -1) {
         return;
     }
 
-    const uint64_t value = 1;
-    ssize_t result = write(m_fd, &value, sizeof(value));
-    if (result == -1 && errno != EAGAIN) {
-        throw std::system_error(errno, std::system_category(), "eventfd write failed");
-    }
+    try {
+        std::lock_guard<std::mutex> lock(m_mutex);
 
-    m_state.store(true);
+        // 再次检查防止竞态条件
+        if (m_destructing.load() || m_fd == -1) {
+            return;
+        }
+
+        const uint64_t value = 1;
+        ssize_t result = write(m_fd, &value, sizeof(value));
+        if (result == sizeof(value)) {
+            m_state.store(true);
+        }
+    }
+    catch (...) {
+        // 捕获所有异常,确保不会崩溃
+    }
 }
 
 void LinuxEvent::ResetEvent() {
-    auto self = shared_from_this();
-
-    std::lock_guard<std::mutex> lock(m_mutex);
+    if (this == nullptr || m_destructing.load() || m_fd == -1) return;
+   
+    try {
+        std::lock_guard<std::mutex> lock(m_mutex);
+        if (m_destructing.load() || m_fd == -1) return;
 
-    if (m_destructing.load()) {
-        std::cerr << getLogPrefix() << "ResetEvent ABORTED: Object is being destroyed" << std::endl;
-        return;
+        uint64_t value;
+        while (read(m_fd, &value, sizeof(value)) == sizeof(value));
+        m_state.store(false);
     }
-
-    uint64_t value;
-    while (read(m_fd, &value, sizeof(value)) == sizeof(value));
-    m_state.store(false);
+    catch (...) {}
 }
 
 bool LinuxEvent::IsSet() const {
@@ -119,8 +119,6 @@ int LinuxEvent::GetFD() const {
 }
 
 bool LinuxEvent::Wait(unsigned long timeoutMs) {
-    auto self = shared_from_this();
-
     if (m_destructing.load() || m_fd == -1) {
         return false;
     }