From b3fece25da6e706f5f3590839135320049a11af1 Mon Sep 17 00:00:00 2001 From: sehraf Date: Wed, 1 Jun 2016 14:58:12 +0200 Subject: [PATCH] introduce thread naming --- libresapi/src/api/RsControlModule.cpp | 2 +- libretroshare/src/ft/ftserver.cc | 9 ++++----- libretroshare/src/util/rsthreads.cc | 22 ++++++++++++++++++++-- libretroshare/src/util/rsthreads.h | 2 +- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/libresapi/src/api/RsControlModule.cpp b/libresapi/src/api/RsControlModule.cpp index 43a5c2241..71848301e 100644 --- a/libresapi/src/api/RsControlModule.cpp +++ b/libresapi/src/api/RsControlModule.cpp @@ -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; diff --git a/libretroshare/src/ft/ftserver.cc b/libretroshare/src/ft/ftserver.cc index 2bcb53a5f..4656fe4a5 100644 --- a/libretroshare/src/ft/ftserver.cc +++ b/libretroshare/src/ft/ftserver.cc @@ -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() diff --git a/libretroshare/src/util/rsthreads.cc b/libretroshare/src/util/rsthreads.cc index 970b241a2..f0e37f7fe 100644 --- a/libretroshare/src/util/rsthreads.cc +++ b/libretroshare/src/util/rsthreads.cc @@ -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) ; } } diff --git a/libretroshare/src/util/rsthreads.h b/libretroshare/src/util/rsthreads.h index ac08a9404..e089671d3 100644 --- a/libretroshare/src/util/rsthreads.h +++ b/libretroshare/src/util/rsthreads.h @@ -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.