adapted tests
This commit is contained in:
@@ -14,10 +14,12 @@
|
||||
namespace Input
|
||||
{
|
||||
|
||||
EventNotifier::EventNotifier()
|
||||
: is_running(true)
|
||||
EventNotifier::EventNotifier() :
|
||||
is_running(true)
|
||||
{
|
||||
notify_thread = std::thread(&EventNotifier::notify, this);
|
||||
this->notify_thread = std::thread(&EventNotifier::notify, this);
|
||||
|
||||
this->event_queue = new BlockingQueue<Event>();
|
||||
|
||||
CLOG(INFO, INPUT_LOGGER) << "Created EventNotifier and started thread";
|
||||
}
|
||||
@@ -27,33 +29,35 @@ EventNotifier::~EventNotifier()
|
||||
is_running = false;
|
||||
|
||||
Event end_event(0, 0, "END");
|
||||
event_queue.push(end_event);
|
||||
event_queue->push(end_event);
|
||||
|
||||
notify_thread.join();
|
||||
|
||||
delete this->event_queue;
|
||||
}
|
||||
|
||||
void EventNotifier::register_event_handler(IEventHandler* handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
event_handler.insert(handler);
|
||||
event_handlers.insert(handler);
|
||||
}
|
||||
|
||||
void EventNotifier::unregister_event_handler(IEventHandler* handler)
|
||||
{
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
event_handler.erase(handler);
|
||||
event_handlers.erase(handler);
|
||||
}
|
||||
|
||||
void EventNotifier::distribute_event(Event& event)
|
||||
{
|
||||
event_queue.push(event);
|
||||
event_queue->push(event);
|
||||
}
|
||||
|
||||
void EventNotifier::notify()
|
||||
{
|
||||
while(is_running)
|
||||
{
|
||||
Event event = event_queue.pop();
|
||||
Event event = event_queue->pop();
|
||||
|
||||
// TODO schoener machen
|
||||
if(event.name == "END")
|
||||
@@ -62,8 +66,8 @@ void EventNotifier::notify()
|
||||
}
|
||||
|
||||
// getting a guard and calling all registered handlers
|
||||
std::lock_guard<std::mutex> event_handler_guard(event_handler_mutex);
|
||||
for(auto handler : event_handler)
|
||||
std::lock_guard event_handler_guard(event_handler_mutex);
|
||||
for(auto handler : event_handlers)
|
||||
{
|
||||
boost::thread handler_caller(boost::bind(&IEventHandler::handle, handler, event));
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include <mutex>
|
||||
|
||||
#include "utilities/BlockingQueue.hpp"
|
||||
#include "utilities/IBlockingQueue.h"
|
||||
#include "Event.h"
|
||||
#include "EventHandler.h"
|
||||
|
||||
@@ -39,8 +40,8 @@ private:
|
||||
void notify();
|
||||
|
||||
private:
|
||||
BlockingQueue<Event> event_queue;
|
||||
std::set<IEventHandler*> event_handler;
|
||||
IBlockingQueue<Event>* event_queue;
|
||||
std::set<IEventHandler*> event_handlers;
|
||||
|
||||
bool is_running;
|
||||
std::thread notify_thread;
|
||||
|
||||
@@ -13,62 +13,41 @@
|
||||
#include <future>
|
||||
#include <boost/heap/priority_queue.hpp>
|
||||
|
||||
#include "IBlockingQueue.h"
|
||||
|
||||
using namespace boost;
|
||||
|
||||
template <typename T>
|
||||
class BlockingQueue
|
||||
class BlockingQueue : public IBlockingQueue<T>
|
||||
{
|
||||
private:
|
||||
std::mutex mutex;
|
||||
heap::priority_queue<T, heap::stable<true>> p_queue;
|
||||
std::mutex mutex;
|
||||
std::condition_variable wait_condition;
|
||||
|
||||
std::promise<T> promise;
|
||||
std::shared_future<T> future_pop;
|
||||
heap::priority_queue<T, heap::stable<true>> p_queue;
|
||||
|
||||
public:
|
||||
BlockingQueue()
|
||||
{
|
||||
this->future_pop = promise.get_future();
|
||||
}
|
||||
|
||||
void push(const T& value)
|
||||
{
|
||||
std::lock_guard lock(this->mutex);
|
||||
if(this->p_queue.empty())
|
||||
void push(T const& value)
|
||||
{
|
||||
this->promise.set_value(value);
|
||||
return;
|
||||
}
|
||||
p_queue.push(value);
|
||||
}
|
||||
std::lock_guard lock(this->mutex);
|
||||
|
||||
T pop()
|
||||
{
|
||||
std::unique_lock lock(this->mutex);
|
||||
p_queue.push(value);
|
||||
|
||||
auto status = future_pop.wait_for(std::chrono::seconds(0));
|
||||
if(status == std::future_status::ready)
|
||||
{
|
||||
return future_pop.get();
|
||||
wait_condition.notify_one();
|
||||
}
|
||||
|
||||
if (not this->p_queue.empty())
|
||||
T pop()
|
||||
{
|
||||
std::unique_lock lock(this->mutex);
|
||||
while(this->p_queue.empty())
|
||||
{
|
||||
wait_condition.wait(lock);
|
||||
}
|
||||
|
||||
auto element = p_queue.top();
|
||||
p_queue.pop();
|
||||
return element;
|
||||
}
|
||||
|
||||
lock.unlock();
|
||||
|
||||
return future_pop.get();
|
||||
}
|
||||
|
||||
private:
|
||||
T get_first()
|
||||
{
|
||||
std::unique_lock lock(this->mutex);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user