Add thread naming to Workers

This commit is contained in:
Vinnie Falco
2013-08-12 09:58:46 -07:00
parent 1a9019f5f5
commit 68560c9ea7
2 changed files with 19 additions and 7 deletions

View File

@@ -17,8 +17,9 @@
*/ */
//============================================================================== //==============================================================================
Workers::Workers (Callback& callback, int numberOfThreads) Workers::Workers (Callback& callback, String const& threadNames, int numberOfThreads)
: m_callback (callback) : m_callback (callback)
, m_threadNames (threadNames)
, m_allPaused (true, true) , m_allPaused (true, true)
, m_semaphore (0) , m_semaphore (0)
, m_numberOfThreads (0) , m_numberOfThreads (0)
@@ -66,7 +67,7 @@ void Workers::setNumberOfThreads (int numberOfThreads)
} }
else else
{ {
worker = new Worker (*this); worker = new Worker (*this, m_threadNames);
} }
m_everyone.push_front (worker); m_everyone.push_front (worker);
@@ -130,8 +131,8 @@ void Workers::deleteWorkers (LockFreeStack <Worker>& stack)
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
Workers::Worker::Worker (Workers& workers) Workers::Worker::Worker (Workers& workers, String const& threadName)
: Thread ("Worker") : Thread (threadName)
, m_workers (workers) , m_workers (workers)
{ {
startThread (); startThread ();
@@ -186,6 +187,9 @@ void Workers::Worker::run ()
++m_workers.m_runningTaskCount; ++m_workers.m_runningTaskCount;
m_workers.m_callback.processTask (); m_workers.m_callback.processTask ();
--m_workers.m_runningTaskCount; --m_workers.m_runningTaskCount;
// Put the name back in case the callback changed it
Thread::setCurrentThreadName (m_threadName);
} }
// Any worker that goes into the paused list must // Any worker that goes into the paused list must
@@ -200,6 +204,8 @@ void Workers::Worker::run ()
if (--m_workers.m_activeCount == 0) if (--m_workers.m_activeCount == 0)
m_workers.m_allPaused.signal (); m_workers.m_allPaused.signal ();
Thread::setCurrentThreadName (m_threadName + " (paused)");
// [1] We will be here when the paused list is popped // [1] We will be here when the paused list is popped
// //
// We block on our event object, a requirement of being // We block on our event object, a requirement of being
@@ -246,7 +252,7 @@ public:
TestCallback cb (threadCount); TestCallback cb (threadCount);
Workers w (cb, 0); Workers w (cb, "Test", 0);
expect (w.getNumberOfThreads () == 0); expect (w.getNumberOfThreads () == 0);
w.setNumberOfThreads (threadCount); w.setNumberOfThreads (threadCount);

View File

@@ -43,8 +43,12 @@ public:
A number of initial threads may be optionally specified. The A number of initial threads may be optionally specified. The
default is to create one thread per CPU. default is to create one thread per CPU.
@param threadNames The name given to each created worker thread.
*/ */
explicit Workers (Callback& callback, int numberOfThreads = SystemStats::getNumCpus ()); explicit Workers (Callback& callback,
String const& threadNames = "Worker",
int numberOfThreads = SystemStats::getNumCpus ());
~Workers (); ~Workers ();
@@ -108,7 +112,7 @@ private:
, public Thread , public Thread
{ {
public: public:
explicit Worker (Workers& workers); Worker (Workers& workers, String const& threadName);
~Worker (); ~Worker ();
@@ -117,6 +121,7 @@ private:
private: private:
Workers& m_workers; Workers& m_workers;
String m_threadName;
}; };
private: private:
@@ -125,6 +130,7 @@ private:
private: private:
Callback& m_callback; Callback& m_callback;
WaitableEvent m_allPaused; // signaled when all threads paused WaitableEvent m_allPaused; // signaled when all threads paused
String m_threadNames; // The name to give each thread
Semaphore m_semaphore; // each pending task is 1 resource Semaphore m_semaphore; // each pending task is 1 resource
int m_numberOfThreads; // how many we want active now int m_numberOfThreads; // how many we want active now
Atomic <int> m_activeCount; // to know when all are paused Atomic <int> m_activeCount; // to know when all are paused