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

View File

@ -213,10 +213,23 @@ public:
void wait()
{
while(value() == 0)
usleep(1000) ;
static const uint32_t max_waiting_time_before_warning=1000 *5 ; // 5 secs
uint32_t tries=0;
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 ;
}
}
post() ;
}
private:
RsSemStruct *s ;