List with multiple events in linux eventhandler
This commit is contained in:
parent
d7dd125037
commit
af19b80d2e
2 changed files with 28 additions and 5 deletions
|
@ -20,6 +20,7 @@ protected:
|
|||
private:
|
||||
class PrivateData;
|
||||
PrivateData *d;
|
||||
bool listIsSignalled();
|
||||
|
||||
friend class Event;
|
||||
};
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include "EventHandler.h"
|
||||
#include "Event.h"
|
||||
#include "Mutex.h"
|
||||
#include "Thread.h"
|
||||
#include <list>
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -8,12 +10,12 @@ class EventHandler::PrivateData {
|
|||
public:
|
||||
pthread_cond_t event;
|
||||
pthread_mutex_t mutex;
|
||||
bool hasEvent;
|
||||
std::list<Event *> 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<Event *>::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<Event *>::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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue