From f835823de3cb555de8879826f58bb9b954d1d9e1 Mon Sep 17 00:00:00 2001 From: csoler Date: Mon, 25 May 2015 15:11:42 +0000 Subject: [PATCH] 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 --- libretroshare/src/util/rsthreads.cc | 26 ++++++++++++++++++++++---- libretroshare/src/util/rsthreads.h | 18 +++++++++++++++--- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 71a71e7ce..e8c018a30 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -66,6 +66,7 @@ void *RsThread::rsthread_init(void* p) RsThread::RsThread () : mMutex("RsThread") { sem_init(&mHasStoppedSemaphore,0,1) ; + sem_init(&mShouldStopSemaphore,0,0) ; #ifdef WINDOWS_SYS memset (&mTid, 0, sizeof(mTid)); @@ -82,6 +83,14 @@ bool RsThread::isRunning() return !sval ; } +bool RsThread::shouldStop() +{ + int sval =0; + sem_getvalue(&mShouldStopSemaphore,&sval) ; + + return sval > 0; +} + void RsTickingThread::shutdown() { #ifdef DEBUG_THREADS @@ -99,6 +108,11 @@ void RsTickingThread::shutdown() return ; } + ask_for_stop() ; +} + +void RsThread::ask_for_stop() +{ #ifdef DEBUG_THREADS std::cerr << " calling stop" << std::endl; #endif @@ -152,6 +166,13 @@ RsTickingThread::RsTickingThread () sem_init(&mShouldStopSemaphore,0,0) ; } +void RsSingleJobThread::runloop() +{ + sem_init(&mShouldStopSemaphore,0,0) ; + + run() ; +} + void RsTickingThread::runloop() { #ifdef DEBUG_THREADS @@ -162,10 +183,7 @@ void RsTickingThread::runloop() while(1) { - int sval =0; - sem_getvalue(&mShouldStopSemaphore,&sval) ; - - if(sval > 0) + if(shouldStop()) { #ifdef DEBUG_THREADS std::cerr << "pqithreadstreamer::run(): asked to stop." << std::endl; diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index 720705728..5a3927fd2 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -182,12 +182,26 @@ class RsThread virtual ~RsThread() {} void start() ; + + // Returns true of the thread is still running. + 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: virtual void runloop() =0; /* called once the thread is started. Should be overloaded by subclasses. */ sem_t mHasStoppedSemaphore; + sem_t mShouldStopSemaphore; static void *rsthread_init(void*) ; RsMutex mMutex; @@ -207,8 +221,6 @@ public: private: virtual void runloop() ; /* called once the thread is started. Should be overloaded by subclasses. */ - - sem_t mShouldStopSemaphore; }; class RsSingleJobThread: public RsThread @@ -217,7 +229,7 @@ public: virtual void run() =0; protected: - virtual void runloop() { run(); } /* called once the thread is started. Should be overloaded by subclasses. */ + virtual void runloop() ; }; class RsQueueThread: public RsTickingThread