From a7eb167f96add4442283b33920671a2510997c72 Mon Sep 17 00:00:00 2001 From: csoler Date: Tue, 30 May 2017 20:57:20 +0200 Subject: [PATCH] fixed a bug in RsThread which made finished threads unaware of their state --- libretroshare/src/util/rsthreads.cc | 16 +++++++++++----- libretroshare/src/util/rsthreads.h | 1 + 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 8cd4a26d6..1db86f17e 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -61,6 +61,16 @@ int RS_pthread_setname_np(pthread_t __target_thread, const char *__buf) { #include #endif +void RsThread::go() +{ + mShouldStopSemaphore.set(0) ; + mHasStoppedSemaphore.set(0) ; + + runloop(); + + mHasStoppedSemaphore.set(1); + mShouldStopSemaphore.set(0); +} void *RsThread::rsthread_init(void* p) { RsThread *thread = (RsThread *) p; @@ -76,7 +86,7 @@ void *RsThread::rsthread_init(void* p) std::cerr << "[Thread ID:" << std::hex << pthread_self() << std::dec << "] thread is started. Calling runloop()..." << std::endl; #endif - thread -> runloop(); + thread->go(); return NULL; } RsThread::RsThread() @@ -216,14 +226,11 @@ RsTickingThread::RsTickingThread() void RsSingleJobThread::runloop() { - mHasStoppedSemaphore.set(0) ; run() ; } void RsTickingThread::runloop() { - mHasStoppedSemaphore.set(0) ; // first time we are 100% the thread is actually running. - #ifdef DEBUG_THREADS THREAD_DEBUG << "RsTickingThread::runloop(). Setting stopped=0" << std::endl; #endif @@ -235,7 +242,6 @@ void RsTickingThread::runloop() #ifdef DEBUG_THREADS THREAD_DEBUG << "pqithreadstreamer::runloop(): asked to stop. setting hasStopped=1, and returning. Thread ends." << std::endl; #endif - mHasStoppedSemaphore.set(1); return ; } diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index e089671d3..0dfbc4c44 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -263,6 +263,7 @@ class RsThread protected: virtual void runloop() =0; /* called once the thread is started. Should be overloaded by subclasses. */ + void go() ; // this one calls runloop and also sets the flags correctly when the thread is finished running. RsSemaphore mHasStoppedSemaphore; RsSemaphore mShouldStopSemaphore;