moved the stop order up to RsThread to ease the test for stopping order in single job threads

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@8290 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2015-05-25 15:11:42 +00:00
parent 5b2ba1e81c
commit f835823de3
2 changed files with 37 additions and 7 deletions

View File

@ -66,6 +66,7 @@ void *RsThread::rsthread_init(void* p)
RsThread::RsThread () : mMutex("RsThread") RsThread::RsThread () : mMutex("RsThread")
{ {
sem_init(&mHasStoppedSemaphore,0,1) ; sem_init(&mHasStoppedSemaphore,0,1) ;
sem_init(&mShouldStopSemaphore,0,0) ;
#ifdef WINDOWS_SYS #ifdef WINDOWS_SYS
memset (&mTid, 0, sizeof(mTid)); memset (&mTid, 0, sizeof(mTid));
@ -82,6 +83,14 @@ bool RsThread::isRunning()
return !sval ; return !sval ;
} }
bool RsThread::shouldStop()
{
int sval =0;
sem_getvalue(&mShouldStopSemaphore,&sval) ;
return sval > 0;
}
void RsTickingThread::shutdown() void RsTickingThread::shutdown()
{ {
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
@ -99,6 +108,11 @@ void RsTickingThread::shutdown()
return ; return ;
} }
ask_for_stop() ;
}
void RsThread::ask_for_stop()
{
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
std::cerr << " calling stop" << std::endl; std::cerr << " calling stop" << std::endl;
#endif #endif
@ -152,6 +166,13 @@ RsTickingThread::RsTickingThread ()
sem_init(&mShouldStopSemaphore,0,0) ; sem_init(&mShouldStopSemaphore,0,0) ;
} }
void RsSingleJobThread::runloop()
{
sem_init(&mShouldStopSemaphore,0,0) ;
run() ;
}
void RsTickingThread::runloop() void RsTickingThread::runloop()
{ {
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
@ -162,10 +183,7 @@ void RsTickingThread::runloop()
while(1) while(1)
{ {
int sval =0; if(shouldStop())
sem_getvalue(&mShouldStopSemaphore,&sval) ;
if(sval > 0)
{ {
#ifdef DEBUG_THREADS #ifdef DEBUG_THREADS
std::cerr << "pqithreadstreamer::run(): asked to stop." << std::endl; std::cerr << "pqithreadstreamer::run(): asked to stop." << std::endl;

View File

@ -182,12 +182,26 @@ class RsThread
virtual ~RsThread() {} virtual ~RsThread() {}
void start() ; void start() ;
// Returns true of the thread is still running.
bool isRunning(); bool isRunning();
// Returns true if the thread received a stopping order and hasn't yet stopped.
bool shouldStop();
// Can be called to set the stopping flags. The stop will not be handled
// by RsThread itself, but in subclasses. If you derive your own subclass,
// you need to call shouldStop() in order to check for a possible stopping order.
void ask_for_stop();
protected: protected:
virtual void runloop() =0; /* called once the thread is started. Should be overloaded by subclasses. */ virtual void runloop() =0; /* called once the thread is started. Should be overloaded by subclasses. */
sem_t mHasStoppedSemaphore; sem_t mHasStoppedSemaphore;
sem_t mShouldStopSemaphore;
static void *rsthread_init(void*) ; static void *rsthread_init(void*) ;
RsMutex mMutex; RsMutex mMutex;
@ -207,8 +221,6 @@ public:
private: private:
virtual void runloop() ; /* called once the thread is started. Should be overloaded by subclasses. */ virtual void runloop() ; /* called once the thread is started. Should be overloaded by subclasses. */
sem_t mShouldStopSemaphore;
}; };
class RsSingleJobThread: public RsThread class RsSingleJobThread: public RsThread
@ -217,7 +229,7 @@ public:
virtual void run() =0; virtual void run() =0;
protected: protected:
virtual void runloop() { run(); } /* called once the thread is started. Should be overloaded by subclasses. */ virtual void runloop() ;
}; };
class RsQueueThread: public RsTickingThread class RsQueueThread: public RsTickingThread