diff options
author | Scott Hernandez <scotthernandez@gmail.com> | 2014-11-03 14:40:09 -0500 |
---|---|---|
committer | Scott Hernandez <scotthernandez@gmail.com> | 2014-11-04 13:06:25 -0500 |
commit | 38de3485ec850c8e21fcfab94e9c378ac73d6346 (patch) | |
tree | 9f05b495d8670cc9850248b484b254c0c020c0e9 /src/mongo/util/concurrency | |
parent | 8f1f165734da24f5b44ae468a59b0cb9746dfec7 (diff) | |
download | mongo-38de3485ec850c8e21fcfab94e9c378ac73d6346.tar.gz |
SERVER-15780 more/improve thread naming
Diffstat (limited to 'src/mongo/util/concurrency')
-rw-r--r-- | src/mongo/util/concurrency/thread_pool.cpp | 27 | ||||
-rw-r--r-- | src/mongo/util/concurrency/thread_pool.h | 7 |
2 files changed, 23 insertions, 11 deletions
diff --git a/src/mongo/util/concurrency/thread_pool.cpp b/src/mongo/util/concurrency/thread_pool.cpp index 4117b663962..741b46d34c7 100644 --- a/src/mongo/util/concurrency/thread_pool.cpp +++ b/src/mongo/util/concurrency/thread_pool.cpp @@ -36,8 +36,9 @@ #include <boost/thread/thread.hpp> -#include "mongo/util/log.h" #include "mongo/util/concurrency/mvar.h" +#include "mongo/util/log.h" +#include "mongo/util/mongoutils/str.h" namespace mongo { namespace threadpool { @@ -45,10 +46,10 @@ namespace mongo { // Worker thread class Worker : boost::noncopyable { public: - explicit Worker(ThreadPool& owner) + explicit Worker(ThreadPool& owner, const std::string& threadName) : _owner(owner) , _is_done(true) - , _thread(stdx::bind(&Worker::loop, this)) + , _thread(stdx::bind(&Worker::loop, this, threadName)) {} // destructor will block until current operation is completed @@ -72,7 +73,8 @@ namespace mongo { bool _is_done; // only used for error detection boost::thread _thread; - void loop() { + void loop(const std::string& threadName) { + setThreadName(threadName); while (true) { Task task = _task.take(); if (!task) @@ -96,21 +98,28 @@ namespace mongo { } }; - ThreadPool::ThreadPool(int nThreads) + ThreadPool::ThreadPool(int nThreads, const std::string& threadNamePrefix) : _mutex("ThreadPool"), _tasksRemaining(0) - , _nThreads(nThreads) { + , _nThreads(nThreads) + , _threadNamePrefix(threadNamePrefix) { startThreads(); } - ThreadPool::ThreadPool(const DoNotStartThreadsTag&, int nThreads) + ThreadPool::ThreadPool(const DoNotStartThreadsTag&, + int nThreads, + const std::string& threadNamePrefix) : _mutex("ThreadPool"), _tasksRemaining(0) - , _nThreads(nThreads) { + , _nThreads(nThreads) + , _threadNamePrefix(threadNamePrefix) { } void ThreadPool::startThreads() { scoped_lock lock(_mutex); for (int i = 0; i < _nThreads; ++i) { - Worker* worker = new Worker(*this); + const std::string threadName(_threadNamePrefix.empty() ? + _threadNamePrefix : + str::stream() << _threadNamePrefix << i); + Worker* worker = new Worker(*this, threadName); if (_tasks.empty()) { _freeWorkers.push_front(worker); } diff --git a/src/mongo/util/concurrency/thread_pool.h b/src/mongo/util/concurrency/thread_pool.h index ebdea0ca283..c454b18cba7 100644 --- a/src/mongo/util/concurrency/thread_pool.h +++ b/src/mongo/util/concurrency/thread_pool.h @@ -48,8 +48,10 @@ namespace mongo { public: struct DoNotStartThreadsTag {}; - explicit ThreadPool(int nThreads=8); - explicit ThreadPool(const DoNotStartThreadsTag&, int nThreads=8); + explicit ThreadPool(int nThreads=8, const std::string& threadNamePrefix=""); + explicit ThreadPool(const DoNotStartThreadsTag&, + int nThreads=8, + const std::string& threadNamePrefix=""); // blocks until all tasks are complete (tasks_remaining() == 0) // You should not call schedule while in the destructor @@ -90,6 +92,7 @@ namespace mongo { std::list<Task> _tasks; //used as FIFO queue (push_back, pop_front) int _tasksRemaining; // in queue + currently processing int _nThreads; // only used for sanity checking. could be removed in the future. + const std::string _threadNamePrefix; // used for logging/diagnostics // should only be called by a worker from the worker's thread void task_done(Worker* worker); |