Fix RsThread when nothing to do and run finish before start.

This commit is contained in:
Phenom 2021-11-03 15:28:19 +01:00
parent 7be575045b
commit 5e9bc9c2b8
3 changed files with 11 additions and 14 deletions

View File

@ -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<RsGxsGroupId> grpIds;
GxsMsgReq msgIds;

View File

@ -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<void*>(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;

View File

@ -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<bool> mHasStopped;