diff --git a/telldus-core/service/EventHandler.h b/telldus-core/service/EventHandler.h index aa53e44d..40226c9b 100644 --- a/telldus-core/service/EventHandler.h +++ b/telldus-core/service/EventHandler.h @@ -20,6 +20,7 @@ protected: private: class PrivateData; PrivateData *d; + bool listIsSignalled(); friend class Event; }; diff --git a/telldus-core/service/EventHandler_unix.cpp b/telldus-core/service/EventHandler_unix.cpp index d74861fb..2bde8f1c 100644 --- a/telldus-core/service/EventHandler_unix.cpp +++ b/telldus-core/service/EventHandler_unix.cpp @@ -1,6 +1,8 @@ #include "EventHandler.h" #include "Event.h" +#include "Mutex.h" #include "Thread.h" +#include #include #include @@ -8,12 +10,12 @@ class EventHandler::PrivateData { public: pthread_cond_t event; pthread_mutex_t mutex; - bool hasEvent; + std::list eventList; + TelldusCore::Mutex listMutex; }; EventHandler::EventHandler() { d = new PrivateData; - d->hasEvent = false; pthread_cond_init(&d->event, NULL); pthread_mutex_init(&d->mutex, NULL); } @@ -21,32 +23,52 @@ EventHandler::EventHandler() { EventHandler::~EventHandler(void) { pthread_mutex_destroy(&d->mutex); pthread_cond_destroy(&d->event); + + std::list::const_iterator it = d->eventList.begin(); + for(; it != d->eventList.end(); ++it) { + delete(*it); + } + delete d; } Event *EventHandler::addEvent() { Event *event = new Event(this); + TelldusCore::MutexLocker locker(&d->listMutex); + d->eventList.push_back(event); return event; } +bool EventHandler::listIsSignalled(){ + TelldusCore::MutexLocker locker(&d->listMutex); + + std::list::const_iterator it = d->eventList.begin(); + for(; it != d->eventList.end(); ++it) { + if((*it)->isSignaled()){ + return true; + } + } + return false; +} + bool EventHandler::removeEvent(EventBase *event) { + TelldusCore::MutexLocker locker(&d->listMutex); + d->eventList.remove((Event*)event); return true; } void EventHandler::signal(Event *event) { pthread_mutex_lock(&d->mutex); //event->setSignaled(); - d->hasEvent = true; pthread_cond_signal(&d->event); pthread_mutex_unlock(&d->mutex); } bool EventHandler::waitForAny() { pthread_mutex_lock(&d->mutex); - while(!d->hasEvent) { + while(!listIsSignalled()) { pthread_cond_wait(&d->event, &d->mutex); } - d->hasEvent = false; pthread_mutex_unlock(&d->mutex); return true;