mirror of
https://github.com/RetroShare/RetroShare.git
synced 2025-01-28 00:07:09 -05:00
tried to improve the RsSemaphore class to avoid and display more info about the deadlock problem
This commit is contained in:
parent
e891e2be2e
commit
383e40324d
@ -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 ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user