Merge pull request #398 from sehraf/pr-thread-naming

Introduce thread names to simplify debugging
This commit is contained in:
Cyril Soler 2016-06-01 09:44:38 -04:00
commit 5b67654530
4 changed files with 26 additions and 9 deletions

View File

@ -30,7 +30,7 @@ RsControlModule::RsControlModule(int argc, char **argv, StateTokenServer* sts, A
this->argv = argv;
// start worker thread
if(full_control)
start();
start("RS ctrl module");
else
mRunState = RUNNING_OK_NO_FULL_CONTROL;

View File

@ -188,19 +188,18 @@ void ftServer::StartupThreads()
/* self contained threads */
/* startup ExtraList Thread */
mFtExtra->start();
mFtExtra->start("RS ft extra lst");
/* startup Monitor Thread */
/* startup the FileMonitor (after cache load) */
/* start it up */
mFiMon->start();
mFiMon->start("RS ft monitor");
/* Controller thread */
mFtController->start();
mFtController->start("RS ft ctrl");
/* Dataplex */
mFtDataplex->start();
mFtDataplex->start("RS ft dataplex");
}
void ftServer::StopThreads()

View File

@ -83,6 +83,7 @@ RsThread::RsThread()
mHasStoppedSemaphore.set(1) ;
mShouldStopSemaphore.set(0) ;
}
bool RsThread::isRunning()
{
// do we need a mutex for this ?
@ -142,7 +143,8 @@ void RsTickingThread::fullstop()
THREAD_DEBUG << " finished!" << std::endl;
#endif
}
void RsThread::start()
void RsThread::start(const std::string &threadName)
{
pthread_t tid;
void *data = (void *)this ;
@ -158,11 +160,27 @@ void RsThread::start()
// -> the new thread will see mIsRunning() = true
if( 0 == (err=pthread_create(&tid, 0, &rsthread_init, data)))
{
mTid = tid;
// set name
if(!threadName.empty()) {
// thread names are restricted to 16 characters including the terminating null byte
if(threadName.length() > 15)
{
#ifdef DEBUG_THREADS
THREAD_DEBUG << "RsThread::start called with to long name '" << name << "' truncating..." << std::endl;
#endif
pthread_setname_np(mTid, threadName.substr(0, 15).c_str());
} else {
pthread_setname_np(mTid, threadName.c_str());
}
}
}
else
{
THREAD_DEBUG << "Fatal error: pthread_create could not create a thread. Error returned: " << err << " !!!!!!!" << std::endl;
mHasStoppedSemaphore.set(1) ;
mHasStoppedSemaphore.set(1) ;
}
}

View File

@ -245,7 +245,7 @@ class RsThread
RsThread();
virtual ~RsThread() {}
void start() ;
void start(const std::string &threadName = "");
// Returns true of the thread is still running.