diff --git a/api/icsneocpp/eventmanager.cpp b/api/icsneocpp/eventmanager.cpp index c9357fb..757e1d5 100644 --- a/api/icsneocpp/eventmanager.cpp +++ b/api/icsneocpp/eventmanager.cpp @@ -15,12 +15,23 @@ void EventManager::ResetInstance() { singleton = std::unique_ptr(new EventManager()); } +// If this thread is not in the map, add it to be ignored +// If it is, set it to be ignored void EventManager::downgradeErrorsOnCurrentThread() { - downgradedThreads.insert(std::this_thread::get_id()); + auto i = downgradedThreads.find(std::this_thread::get_id()); + if(i != downgradedThreads.end()) { + i->second = true; + } else { + downgradedThreads.insert({std::this_thread::get_id(), true}); + } } +// If this thread exists in the map, turn off downgrading void EventManager::cancelErrorDowngradingOnCurrentThread() { - downgradedThreads.erase(std::this_thread::get_id()); + auto i = downgradedThreads.find(std::this_thread::get_id()); + if(i != downgradedThreads.end()) { + i->second = false; + } } void EventManager::get(std::vector& eventOutput, size_t max, EventFilter filter) { diff --git a/communication/multichannelcommunication.cpp b/communication/multichannelcommunication.cpp index ca93353..cef64a1 100644 --- a/communication/multichannelcommunication.cpp +++ b/communication/multichannelcommunication.cpp @@ -113,11 +113,14 @@ void MultiChannelCommunication::readTask() { continue; } + // We want callbacks to be able to access errors + EventManager::GetInstance().cancelErrorDowngradingOnCurrentThread(); for(auto& cb : messageCallbacks) { // We might have closed while reading or processing if(!closing) { cb.second.callIfMatch(msg); } } + EventManager::GetInstance().downgradeErrorsOnCurrentThread(); } } diff --git a/include/icsneo/api/eventmanager.h b/include/icsneo/api/eventmanager.h index 2b94a4d..b1fec3b 100644 --- a/include/icsneo/api/eventmanager.h +++ b/include/icsneo/api/eventmanager.h @@ -2,7 +2,6 @@ #define __ICSNEO_API_EVENTMANAGER_H_ #include -#include #include #include #include @@ -80,7 +79,7 @@ private: // Used by functions for threadsafety mutable std::mutex mutex; - std::set downgradedThreads; + std::map downgradedThreads; // Stores all events std::list events; @@ -92,7 +91,8 @@ private: void add_internal(APIEvent event) { if(event.getSeverity() == APIEvent::Severity::Error) { // if the error was added on a thread that downgrades errors (non-user thread) - if(std::find(downgradedThreads.begin(), downgradedThreads.end(), std::this_thread::get_id()) != downgradedThreads.end()) { + auto i = downgradedThreads.find(std::this_thread::get_id()); + if(i != downgradedThreads.end() && i->second) { event.downgradeFromError(); add_internal_event(event); } else