JSON API service and RsThread fixups

This commit is contained in:
Gioacchino Mazzurco 2020-01-08 14:24:46 +01:00
parent f4d7fe4dde
commit f12ec2b535
No known key found for this signature in database
GPG key ID: A1FBCA3872E87051
6 changed files with 88 additions and 53 deletions

View file

@ -207,15 +207,14 @@ bool ConvertUtf16ToUtf8(const std::wstring& source, std::string& dest)
bool is_alphanumeric(char c)
{
return (c>='0' && c<'9') || (c>='a' && c<='z') || (c>='A' && c<='Z') ;
return (c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z');
}
bool is_alphanumeric(const std::string& s)
{
for(uint32_t i=0;i<s.size();++i)
if(!is_alphanumeric(s[i]))
return false;
return true;
for( uint32_t i=0; i < s.size(); ++i)
if(!is_alphanumeric(s[i])) return false;
return true;
}
} } // librs::util

View file

@ -85,7 +85,7 @@ int RS_pthread_setname_np(pthread_t __target_thread, const char *__buf) {
return nullptr;
}
RsThread::RsThread() : mHasStopped(true), mShouldStop(false)
void RsThread::resetTid()
{
#ifdef WINDOWS_SYS
memset (&mTid, 0, sizeof(mTid));
@ -94,6 +94,9 @@ RsThread::RsThread() : mHasStopped(true), mShouldStop(false)
#endif
}
RsThread::RsThread() : mHasStopped(true), mShouldStop(false), mLastTid()
{ resetTid(); }
bool RsThread::isRunning() { return !mHasStopped; }
bool RsThread::shouldStop() { return mShouldStop; }
@ -102,13 +105,13 @@ void RsThread::askForStop()
{
/* Call onStopRequested() only once even if askForStop() is called multiple
* times */
if(!mShouldStop.exchange(true))
RsThread::async([&](){ onStopRequested(); });
if(!mShouldStop.exchange(true)) onStopRequested();
}
void RsThread::wrapRun()
{
run();
resetTid();
mHasStopped = true;
}
@ -122,7 +125,8 @@ void RsThread::fullstop()
RsErr() << __PRETTY_FUNCTION__ << " called by same thread. This should "
<< "never happen! this: " << static_cast<void*>(this)
<< std::hex << ", callerTid: " << callerTid
<< ", mTid: " << mTid << std::dec << std::endl;
<< ", mTid: " << mTid << std::dec
<< ", mFullName: " << mFullName << std::endl;
print_stacktrace();
return;
}
@ -134,9 +138,9 @@ void RsThread::fullstop()
std::this_thread::sleep_for(std::chrono::milliseconds(200));
++i;
if(!(i%5))
RsInfo() << __PRETTY_FUNCTION__ << " " << i*0.2 << " seconds passed"
<< " waiting for thread: " << mTid << " " << mFullName
<< " to stop" << std::endl;
RsDbg() << __PRETTY_FUNCTION__ << " " << i*0.2 << " seconds passed"
<< " waiting for thread: " << std::hex << mLastTid
<< std::dec << " " << mFullName << " to stop" << std::endl;
}
}
@ -158,6 +162,9 @@ bool RsThread::start(const std::string& threadName)
return false;
}
/* Store an extra copy of thread id for debugging */
mLastTid = mTid;
/* Store thread full name as PThread is not able to keep it entirely */
mFullName = threadName;
@ -266,10 +273,10 @@ double RsStackMutex::getCurrentTS()
RsThread::~RsThread()
{
if(isRunning())
if(!mHasStopped)
{
RsErr() << __PRETTY_FUNCTION__ << " deleting thread: " << mTid << " "
<< mFullName << " that is still "
RsErr() << __PRETTY_FUNCTION__ << " deleting thread: " << mLastTid
<< " " << mFullName << " that is still "
<< "running! Something seems very wrong here and RetroShare is "
<< "likely to crash because of this." << std::endl;
print_stacktrace();

View file

@ -264,9 +264,19 @@ private:
/// Store the id of the corresponding pthread
pthread_t mTid;
void resetTid();
/// Store thread full name
/** Store thread full name for debugging because PThread is limited to 15
* char thread names */
std::string mFullName;
/** Store a copy of thread id which is never reset to 0 after initialization
* due to RsThread functioning. After RsThread initialization this member is
* only re-written with a new tread id in start(...).
* This is useful for debugging because mTid is reset at the end of wrapRun
* and that might happens concurrently (or just before) a debug message
* being printed, thus causing the debug message to print a mangled value.*/
pthread_t mLastTid;
};
/**