diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 0a9db1e58..da8973eb1 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -322,12 +322,12 @@ void RsGenExchange::tick() { mIntegrityCheck = new RsGxsIntegrityCheck( mDataStore, this, *mSerialiser, mGixs); - mIntegrityCheck->start("gxs integrity"); - mChecking = true; + std::string thisName = typeid(*this).name(); + mChecking = mIntegrityCheck->start("gxs IC4 "+thisName); } } - if(mIntegrityCheck->isDone()) + if(mIntegrityCheck->isDone() || !mChecking) { std::vector grpIds; GxsMsgReq msgIds; diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 035ad214a..85f96b69a 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -92,11 +92,11 @@ void RsThread::resetTid() #ifdef WINDOWS_SYS memset (&mTid, 0, sizeof(mTid)); #else - mTid = 0; + mTid = pthread_t(); //Thread identifiers should be considered opaque and can be null. #endif } -RsThread::RsThread() : mHasStopped(true), mShouldStop(false), mLastTid() +RsThread::RsThread() : mInitMtx("RsThread"), mHasStopped(true), mShouldStop(false), mLastTid() #ifdef RS_THREAD_FORCE_STOP , mStopTimeout(0) #endif @@ -115,6 +115,7 @@ void RsThread::askForStop() void RsThread::wrapRun() { + {RS_STACK_MUTEX(mInitMtx);} // Waiting Init done. run(); resetTid(); mHasStopped = true; @@ -184,6 +185,8 @@ bool RsThread::start(const std::string& threadName) // Atomically check if the thread was already started and set it as running if(mHasStopped.exchange(false)) { + RS_STACK_MUTEX(mInitMtx); // Block thread starting to run + mShouldStop = false; int pError = pthread_create( &mTid, nullptr, &rsthread_init, static_cast(this) ); @@ -196,15 +199,6 @@ bool RsThread::start(const std::string& threadName) print_stacktrace(); return false; } - if(!mTid) - { - RsErr() << __PRETTY_FUNCTION__ << " pthread_create could not create" - << " new thread: " << threadName << " mTid: " << mTid - << std::endl; - mHasStopped = true; - print_stacktrace(); - return false; - } /* Store an extra copy of thread id for debugging */ mLastTid = mTid; diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index 82221ebbb..c33a608a8 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -261,6 +261,9 @@ private: /** Call @see run() setting the appropriate flags around it*/ void wrapRun(); + // To be sure Init (pthread_setname_np) is done before continue thread. Else can finish before and crash. + RsMutex mInitMtx; + /// True if thread is stopped, false otherwise std::atomic mHasStopped;