Merge pull request #2499 from PhenomRetroShare/Fix_RsThread

Fix RsThread when nothing to do and run finish before start.
This commit is contained in:
G10h4ck 2021-11-04 18:47:12 +01:00 committed by GitHub
commit 9b2504d407
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 14 deletions

View File

@ -322,12 +322,12 @@ void RsGenExchange::tick()
{ {
mIntegrityCheck = new RsGxsIntegrityCheck( mDataStore, this, mIntegrityCheck = new RsGxsIntegrityCheck( mDataStore, this,
*mSerialiser, mGixs); *mSerialiser, mGixs);
mIntegrityCheck->start("gxs integrity"); std::string thisName = typeid(*this).name();
mChecking = true; mChecking = mIntegrityCheck->start("gxs IC4 "+thisName);
} }
} }
if(mIntegrityCheck->isDone()) if(mIntegrityCheck->isDone() || !mChecking)
{ {
std::vector<RsGxsGroupId> grpIds; std::vector<RsGxsGroupId> grpIds;
GxsMsgReq msgIds; GxsMsgReq msgIds;

View File

@ -92,11 +92,11 @@ void RsThread::resetTid()
#ifdef WINDOWS_SYS #ifdef WINDOWS_SYS
memset (&mTid, 0, sizeof(mTid)); memset (&mTid, 0, sizeof(mTid));
#else #else
mTid = 0; mTid = pthread_t(); //Thread identifiers should be considered opaque and can be null.
#endif #endif
} }
RsThread::RsThread() : mHasStopped(true), mShouldStop(false), mLastTid() RsThread::RsThread() : mInitMtx("RsThread"), mHasStopped(true), mShouldStop(false), mLastTid()
#ifdef RS_THREAD_FORCE_STOP #ifdef RS_THREAD_FORCE_STOP
, mStopTimeout(0) , mStopTimeout(0)
#endif #endif
@ -115,6 +115,7 @@ void RsThread::askForStop()
void RsThread::wrapRun() void RsThread::wrapRun()
{ {
{RS_STACK_MUTEX(mInitMtx);} // Waiting Init done.
run(); run();
resetTid(); resetTid();
mHasStopped = true; 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 // Atomically check if the thread was already started and set it as running
if(mHasStopped.exchange(false)) if(mHasStopped.exchange(false))
{ {
RS_STACK_MUTEX(mInitMtx); // Block thread starting to run
mShouldStop = false; mShouldStop = false;
int pError = pthread_create( int pError = pthread_create(
&mTid, nullptr, &rsthread_init, static_cast<void*>(this) ); &mTid, nullptr, &rsthread_init, static_cast<void*>(this) );
@ -196,15 +199,6 @@ bool RsThread::start(const std::string& threadName)
print_stacktrace(); print_stacktrace();
return false; 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 */ /* Store an extra copy of thread id for debugging */
mLastTid = mTid; mLastTid = mTid;

View File

@ -261,6 +261,9 @@ private:
/** Call @see run() setting the appropriate flags around it*/ /** Call @see run() setting the appropriate flags around it*/
void wrapRun(); 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 /// True if thread is stopped, false otherwise
std::atomic<bool> mHasStopped; std::atomic<bool> mHasStopped;