From d0c6aba0fcb718764ea04764cbbb5d08354fa649 Mon Sep 17 00:00:00 2001 From: EricLiu2000 Date: Thu, 27 Jun 2019 14:16:36 -0400 Subject: [PATCH] Revert "Changed list of events to vector, switched back to using lock_guard instead of shared/unique_locks" This reverts commit 2dac03b3e5b43c65a3a9e09d27955c45af9eecad. --- api/icsneocpp/eventmanager.cpp | 28 ++++++++-------------------- include/icsneo/api/eventmanager.h | 17 ++++++++++------- 2 files changed, 18 insertions(+), 27 deletions(-) diff --git a/api/icsneocpp/eventmanager.cpp b/api/icsneocpp/eventmanager.cpp index 1481395..a917678 100644 --- a/api/icsneocpp/eventmanager.cpp +++ b/api/icsneocpp/eventmanager.cpp @@ -16,7 +16,7 @@ void EventManager::ResetInstance() { } void EventManager::get(std::vector& eventOutput, size_t max, EventFilter filter) { - std::lock_guard lk(mutex); + std::unique_lock lk(mutex); if(max == 0) // A limit of 0 indicates no limit max = (size_t)-1; @@ -41,7 +41,7 @@ void EventManager::get(std::vector& eventOutput, size_t max, EventFilt * If no error was found, return a NoErrorFound Info event */ APIEvent EventManager::getLastError() { - std::lock_guard lk(mutex); + std::unique_lock lk(mutex); auto it = lastUserErrors.find(std::this_thread::get_id()); if(it == lastUserErrors.end()) { @@ -54,15 +54,10 @@ APIEvent EventManager::getLastError() { } void EventManager::discard(EventFilter filter) { - std::lock_guard lk(mutex); - - auto it = events.begin(); - while(it != events.end()) { - if(filter.match(*it)) - it = events.erase(it); - else - it++; - } + std::unique_lock lk(mutex); + events.remove_if([&filter](const APIEvent& event) { + return filter.match(event); + }); } size_t EventManager::count_internal(EventFilter filter) const { @@ -78,15 +73,8 @@ size_t EventManager::count_internal(EventFilter filter) const { * Returns true if any events were removed in the process of doing so. */ bool EventManager::enforceLimit() { - // Remove all TooManyEvents before checking. TODO: is this worth doing? not efficient. maybe start from the end and erase any from there - auto filter = EventFilter(APIEvent::Type::TooManyEvents); - auto it = events.begin(); - while(it != events.end()) { - if(filter.match(*it)) - it = events.erase(it); - else - it++; - } + // Remove all TooManyEvents before checking + events.remove_if([](icsneo::APIEvent err){ return err.getType() == APIEvent::Type::TooManyEvents; }); // We are not overflowing if(events.size() < eventLimit) diff --git a/include/icsneo/api/eventmanager.h b/include/icsneo/api/eventmanager.h index dae4e94..8e77284 100644 --- a/include/icsneo/api/eventmanager.h +++ b/include/icsneo/api/eventmanager.h @@ -2,8 +2,11 @@ #define __ICSNEO_API_EVENTMANAGER_H_ #include +#include #include +#include #include +#include #include #include #include "icsneo/api/event.h" @@ -19,7 +22,7 @@ public: static void ResetInstance(); size_t eventCount(EventFilter filter = EventFilter()) const { - std::lock_guard lk(mutex); + std::shared_lock lk(mutex); return count_internal(filter); }; @@ -35,11 +38,11 @@ public: APIEvent getLastError(); void add(APIEvent event) { - std::lock_guard lk(mutex); + std::unique_lock lk(mutex); add_internal(event); } void add(APIEvent::Type type, APIEvent::Severity severity, const Device* forDevice = nullptr) { - std::lock_guard lk(mutex); + std::unique_lock lk(mutex); add_internal(APIEvent(type, severity, forDevice)); } @@ -54,14 +57,14 @@ public: return; } - std::lock_guard lk(mutex); + std::unique_lock lk(mutex); eventLimit = newLimit; if(enforceLimit()) add_internal(APIEvent(APIEvent::Type::TooManyEvents, APIEvent::Severity::EventWarning)); } size_t getEventLimit() const { - std::lock_guard lk(mutex); + std::shared_lock lk(mutex); return eventLimit; } @@ -71,10 +74,10 @@ private: EventManager& operator=(const EventManager &other); // Used by functions for threadsafety - mutable std::mutex mutex; + mutable std::shared_mutex mutex; // Stores all events - std::vector events; + std::list events; std::map lastUserErrors; size_t eventLimit = 10000;