Fixes for callback hangings in Linux
This commit is contained in:
parent
46e3d8f61f
commit
0f7937711f
3 changed files with 29 additions and 6 deletions
|
@ -27,7 +27,9 @@ namespace TelldusCore {
|
||||||
class PrivateData;
|
class PrivateData;
|
||||||
PrivateData *d;
|
PrivateData *d;
|
||||||
bool listIsSignalled();
|
bool listIsSignalled();
|
||||||
|
#ifndef _WINDOWS
|
||||||
|
bool isSignalled();
|
||||||
|
#endif
|
||||||
friend class Event;
|
friend class Event;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,8 +4,10 @@
|
||||||
// Copyright: See COPYING file that comes with this distribution
|
// Copyright: See COPYING file that comes with this distribution
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
|
#include <errno.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <list>
|
#include <list>
|
||||||
#include "common/EventHandler.h"
|
#include "common/EventHandler.h"
|
||||||
#include "common/Event.h"
|
#include "common/Event.h"
|
||||||
|
@ -20,6 +22,7 @@ public:
|
||||||
pthread_mutex_t mutex;
|
pthread_mutex_t mutex;
|
||||||
std::list<EventRef> eventList;
|
std::list<EventRef> eventList;
|
||||||
TelldusCore::Mutex listMutex;
|
TelldusCore::Mutex listMutex;
|
||||||
|
bool isSignalled;
|
||||||
};
|
};
|
||||||
|
|
||||||
EventHandler::EventHandler() {
|
EventHandler::EventHandler() {
|
||||||
|
@ -27,6 +30,7 @@ EventHandler::EventHandler() {
|
||||||
pthread_cond_init(&d->event, NULL);
|
pthread_cond_init(&d->event, NULL);
|
||||||
pthread_cond_init(&d->event, NULL);
|
pthread_cond_init(&d->event, NULL);
|
||||||
pthread_mutex_init(&d->mutex, NULL);
|
pthread_mutex_init(&d->mutex, NULL);
|
||||||
|
d->isSignalled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
EventHandler::~EventHandler(void) {
|
EventHandler::~EventHandler(void) {
|
||||||
|
@ -49,6 +53,10 @@ EventRef EventHandler::addEvent() {
|
||||||
return event;
|
return event;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool EventHandler::isSignalled() {
|
||||||
|
return d->isSignalled;
|
||||||
|
}
|
||||||
|
|
||||||
bool EventHandler::listIsSignalled() {
|
bool EventHandler::listIsSignalled() {
|
||||||
TelldusCore::MutexLocker locker(&d->listMutex);
|
TelldusCore::MutexLocker locker(&d->listMutex);
|
||||||
|
|
||||||
|
@ -63,6 +71,7 @@ bool EventHandler::listIsSignalled() {
|
||||||
|
|
||||||
void EventHandler::signal(Event *event) {
|
void EventHandler::signal(Event *event) {
|
||||||
pthread_mutex_lock(&d->mutex);
|
pthread_mutex_lock(&d->mutex);
|
||||||
|
d->isSignalled = true;
|
||||||
// event->setSignaled();
|
// event->setSignaled();
|
||||||
pthread_cond_signal(&d->event);
|
pthread_cond_signal(&d->event);
|
||||||
pthread_mutex_unlock(&d->mutex);
|
pthread_mutex_unlock(&d->mutex);
|
||||||
|
@ -70,12 +79,22 @@ void EventHandler::signal(Event *event) {
|
||||||
|
|
||||||
bool EventHandler::waitForAny() {
|
bool EventHandler::waitForAny() {
|
||||||
pthread_mutex_lock(&d->mutex);
|
pthread_mutex_lock(&d->mutex);
|
||||||
while(!listIsSignalled()) {
|
int ret;
|
||||||
pthread_cond_wait(&d->event, &d->mutex);
|
while(!isSignalled()) {
|
||||||
|
timeval now;
|
||||||
|
gettimeofday(&now, NULL);
|
||||||
|
long int abstime_ns_large = now.tv_usec*1000 + 60000000000; //add 60 seconds wait (5 seconds before)?
|
||||||
|
timespec abstime = { now.tv_sec + (abstime_ns_large / 1000000000), abstime_ns_large % 1000000000 };
|
||||||
|
ret = pthread_cond_timedwait(&d->event, &d->mutex, &abstime);
|
||||||
|
if (ret == ETIMEDOUT){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(!listIsSignalled()){
|
||||||
|
d->isSignalled = false;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&d->mutex);
|
pthread_mutex_unlock(&d->mutex);
|
||||||
|
return listIsSignalled();
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace TelldusCore
|
} // namespace TelldusCore
|
||||||
|
|
|
@ -55,7 +55,9 @@ void Thread::startAndLock(Mutex *lock) {
|
||||||
d->threadStarted = handler.addEvent();
|
d->threadStarted = handler.addEvent();
|
||||||
d->mutex = lock;
|
d->mutex = lock;
|
||||||
this->start();
|
this->start();
|
||||||
handler.waitForAny();
|
while (!handler.waitForAny()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
d->threadStarted.reset();
|
d->threadStarted.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue