diff --git a/FlippR-Driver/src/utilities/BlockingQueue.hpp b/FlippR-Driver/src/utilities/BlockingQueue.hpp index 62bff58..6cd12bb 100644 --- a/FlippR-Driver/src/utilities/BlockingQueue.hpp +++ b/FlippR-Driver/src/utilities/BlockingQueue.hpp @@ -10,6 +10,7 @@ #include #include +#include #include using namespace boost; @@ -18,33 +19,56 @@ template class BlockingQueue { private: - std::mutex d_mutex; - std::condition_variable d_condition; - heap::priority_queue> p_queue; +std::mutex mutex; +heap::priority_queue> p_queue; + +std::promise promise; +std::shared_future future_pop; public: - void push(T const& value) +BlockingQueue() +{ + this->future_pop = promise.get_future(); +} + +void push(const T& value) +{ + std::lock_guard lock(this->mutex); + if(this->p_queue.empty()) { - std::unique_lock lock(this->d_mutex); - p_queue.push(value); - this->d_condition.notify_one(); + this->promise.set_value(value); + return; + } + p_queue.push(value); +} + +T pop() +{ + std::unique_lock lock(this->mutex); + + auto status = future_pop.wait_for(std::chrono::seconds(0)); + if(status == std::future_status::ready) + { + return future_pop.get(); } - T pop() + if (not this->p_queue.empty()) { - // TODO die queue funzt net weil wir nich pushen koennen wenn wir im pop warten - // TODO denk ma ueber future nach - std::unique_lock lock(this->d_mutex); - this->d_condition.wait(lock, [=]{ return !this->p_queue.empty(); }); - T rc = *this->p_queue.end(); - this->p_queue.pop(); - return rc; + auto element = p_queue.top(); + p_queue.pop(); + return element; } - void release() - { - this->d_condition.notify_one(); - } + lock.unlock(); + + return future_pop.get(); +} + +private: + T get_first() + { + std::unique_lock lock(this->mutex); + } }; diff --git a/FlippR-Driver/tests/input/TestDetector.cpp b/FlippR-Driver/tests/input/TestDetector.cpp index eadc7b2..998bd10 100644 --- a/FlippR-Driver/tests/input/TestDetector.cpp +++ b/FlippR-Driver/tests/input/TestDetector.cpp @@ -6,7 +6,7 @@ */ -#include "../catch.hpp" +#include "catch.hpp" #include "fakeit.hpp" #include @@ -60,11 +60,11 @@ SCENARIO("Creating a Detector object", "") AND_WHEN("an event can be found at gpio interface") { - std::this_thread::sleep_for(std::chrono::milliseconds(50)); - THEN("only the fitting event should be distributed by event notifier") - { - REQUIRE((bool)Verify(Method(event_notifier_mock, distribute_event).Using(event2))); - } + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + THEN("after some time the only the fitting event should be distributed by event notifier") + { + REQUIRE((bool)Verify(Method(event_notifier_mock, distribute_event).Using(event2))); + } } } }