tried to improve the RsSemaphore class to avoid and display more info about the deadlock problem

This commit is contained in:
Cyril Soler 2016-05-11 12:42:59 -04:00
parent e891e2be2e
commit 383e40324d
2 changed files with 52 additions and 40 deletions

View File

@ -117,7 +117,7 @@ void RsThread::ask_for_stop()
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
THREAD_DEBUG << " calling stop" << std::endl; THREAD_DEBUG << " calling stop" << std::endl;
#endif #endif
mShouldStopSemaphore.post(); mShouldStopSemaphore.set(1);
} }
void RsTickingThread::fullstop() void RsTickingThread::fullstop()
@ -177,7 +177,6 @@ void RsTickingThread::runloop()
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
THREAD_DEBUG << "pqithreadstream::runloop()" << std::endl; THREAD_DEBUG << "pqithreadstream::runloop()" << std::endl;
#endif #endif
mShouldStopSemaphore.set(0) ;
while(1) while(1)
{ {
@ -186,7 +185,7 @@ void RsTickingThread::runloop()
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
THREAD_DEBUG << "pqithreadstreamer::runloop(): asked to stop. setting hasStopped=1, and returning. Thread ends." << std::endl; THREAD_DEBUG << "pqithreadstreamer::runloop(): asked to stop. setting hasStopped=1, and returning. Thread ends." << std::endl;
#endif #endif
mHasStoppedSemaphore.post(); mHasStoppedSemaphore.set(1);
return ; return ;
} }

View File

@ -173,53 +173,66 @@ class RsStackMutex
// This class handles a Mutex-based semaphore, that makes it cross plateform. // This class handles a Mutex-based semaphore, that makes it cross plateform.
class RsSemaphore class RsSemaphore
{ {
class RsSemStruct class RsSemStruct
{ {
public: public:
RsSemStruct() : mtx("Semaphore mutex"), val(0) {} RsSemStruct() : mtx("Semaphore mutex"), val(0) {}
RsMutex mtx ; RsMutex mtx ;
uint32_t val ; uint32_t val ;
}; };
public: public:
RsSemaphore() RsSemaphore()
{ {
s = new RsSemStruct ; s = new RsSemStruct ;
} }
~RsSemaphore() ~RsSemaphore()
{ {
delete s ; delete s ;
} }
void set(uint32_t i) void set(uint32_t i)
{ {
RS_STACK_MUTEX(s->mtx) ; RS_STACK_MUTEX(s->mtx) ;
s->val = i ; s->val = i ;
} }
void post() void post()
{ {
RS_STACK_MUTEX(s->mtx) ; RS_STACK_MUTEX(s->mtx) ;
++(s->val) ; ++(s->val) ;
} }
uint32_t value() uint32_t value()
{ {
RS_STACK_MUTEX(s->mtx) ; RS_STACK_MUTEX(s->mtx) ;
return s->val ; return s->val ;
} }
void wait() void wait()
{ {
while(value() == 0) static const uint32_t max_waiting_time_before_warning=1000 *5 ; // 5 secs
usleep(1000) ; uint32_t tries=0;
post() ; while(true)
} {
usleep(1000) ;
if(++tries >= max_waiting_time_before_warning)
std::cerr << "(EE) Semaphore waiting for too long. Something is probably wrong in the code." << std::endl;
RS_STACK_MUTEX(s->mtx) ;
if(s->val > 0)
{
--(s->val) ;
return ;
}
}
}
private: private:
RsSemStruct *s ; RsSemStruct *s ;
}; };
class RsThread; class RsThread;