diff options
author | Ben Caimano <ben.caimano@10gen.com> | 2019-03-13 15:05:35 -0400 |
---|---|---|
committer | Ben Caimano <ben.caimano@10gen.com> | 2019-03-13 21:43:35 -0400 |
commit | 0549ffda6c6563e7148973b61a673e910de78c85 (patch) | |
tree | 145659123eb3ba961d411e527da59b0bdc8cca5f | |
parent | 3a562b6908a737823b85a5dc94e12076b9dcfe60 (diff) | |
download | mongo-0549ffda6c6563e7148973b61a673e910de78c85.tar.gz |
SERVER-40111 Switch ThreadPool classes from try-catch to noexcept
-rw-r--r-- | src/mongo/executor/network_interface_thread_pool.cpp | 9 | ||||
-rw-r--r-- | src/mongo/executor/network_interface_thread_pool.h | 2 | ||||
-rw-r--r-- | src/mongo/util/concurrency/thread_pool.cpp | 46 | ||||
-rw-r--r-- | src/mongo/util/concurrency/thread_pool.h | 2 | ||||
-rw-r--r-- | src/mongo/util/concurrency/thread_pool_test_common.cpp | 8 |
5 files changed, 24 insertions, 43 deletions
diff --git a/src/mongo/executor/network_interface_thread_pool.cpp b/src/mongo/executor/network_interface_thread_pool.cpp index 4620d9bc19f..0fa5ed7632e 100644 --- a/src/mongo/executor/network_interface_thread_pool.cpp +++ b/src/mongo/executor/network_interface_thread_pool.cpp @@ -148,7 +148,7 @@ void NetworkInterfaceThreadPool::_consumeTasks(stdx::unique_lock<stdx::mutex> lk invariant(ret.isOK() || ErrorCodes::isShutdownError(ret.code())); } -void NetworkInterfaceThreadPool::_consumeTasksInline(stdx::unique_lock<stdx::mutex> lk) { +void NetworkInterfaceThreadPool::_consumeTasksInline(stdx::unique_lock<stdx::mutex> lk) noexcept { _consumeState = ConsumeState::kConsuming; const auto consumingTasksGuard = makeGuard([&] { _consumeState = ConsumeState::kNeutral; }); @@ -162,12 +162,7 @@ void NetworkInterfaceThreadPool::_consumeTasksInline(stdx::unique_lock<stdx::mut const auto lkGuard = makeGuard([&] { lk.lock(); }); for (auto&& task : tasks) { - try { - task(); - } catch (...) { - severe() << "Exception escaped task in network interface thread pool"; - std::terminate(); - } + task(); } tasks.clear(); diff --git a/src/mongo/executor/network_interface_thread_pool.h b/src/mongo/executor/network_interface_thread_pool.h index da9146f6fe6..712ad2d6df7 100644 --- a/src/mongo/executor/network_interface_thread_pool.h +++ b/src/mongo/executor/network_interface_thread_pool.h @@ -61,7 +61,7 @@ public: private: void _consumeTasks(stdx::unique_lock<stdx::mutex> lk); - void _consumeTasksInline(stdx::unique_lock<stdx::mutex> lk); + void _consumeTasksInline(stdx::unique_lock<stdx::mutex> lk) noexcept; void _dtorImpl(); NetworkInterface* const _net; diff --git a/src/mongo/util/concurrency/thread_pool.cpp b/src/mongo/util/concurrency/thread_pool.cpp index 755ad56bffd..81f1e3c2285 100644 --- a/src/mongo/util/concurrency/thread_pool.cpp +++ b/src/mongo/util/concurrency/thread_pool.cpp @@ -130,14 +130,8 @@ void ThreadPool::_shutdown_inlock() { } void ThreadPool::join() { - try { - stdx::unique_lock<stdx::mutex> lk(_mutex); - _join_inlock(&lk); - } catch (...) { - severe() << "Exception escaped join in thread pool " << _options.poolName << ": " - << exceptionToStatus(); - std::terminate(); - } + stdx::unique_lock<stdx::mutex> lk(_mutex); + _join_inlock(&lk); } void ThreadPool::_join_inlock(stdx::unique_lock<stdx::mutex>* lk) { @@ -244,13 +238,7 @@ void ThreadPool::_workerThreadBody(ThreadPool* pool, const std::string& threadNa pool->_options.onCreateThread(threadName); const auto poolName = pool->_options.poolName; LOG(1) << "starting thread in pool " << poolName; - try { - pool->_consumeTasks(); - } catch (...) { - severe() << "Exception reached top of stack in thread pool " << poolName << ": " - << exceptionToStatus(); - std::terminate(); - } + pool->_consumeTasks(); // At this point, another thread may have destroyed "pool", if this thread chose to detach // itself and remove itself from pool->_threads before releasing pool->_mutex. Do not access @@ -337,24 +325,18 @@ void ThreadPool::_consumeTasks() { fassertFailedNoTrace(28703); } -void ThreadPool::_doOneTask(stdx::unique_lock<stdx::mutex>* lk) { +void ThreadPool::_doOneTask(stdx::unique_lock<stdx::mutex>* lk) noexcept { invariant(!_pendingTasks.empty()); - try { - LOG(3) << "Executing a task on behalf of pool " << _options.poolName; - Task task = std::move(_pendingTasks.front()); - _pendingTasks.pop_front(); - --_numIdleThreads; - lk->unlock(); - task(); - lk->lock(); - ++_numIdleThreads; - if (_pendingTasks.empty() && _threads.size() == _numIdleThreads) { - _poolIsIdle.notify_all(); - } - } catch (...) { - severe() << "Exception escaped task in thread pool " << _options.poolName << ": " - << exceptionToStatus(); - std::terminate(); + LOG(3) << "Executing a task on behalf of pool " << _options.poolName; + Task task = std::move(_pendingTasks.front()); + _pendingTasks.pop_front(); + --_numIdleThreads; + lk->unlock(); + task(); + lk->lock(); + ++_numIdleThreads; + if (_pendingTasks.empty() && _threads.size() == _numIdleThreads) { + _poolIsIdle.notify_all(); } } diff --git a/src/mongo/util/concurrency/thread_pool.h b/src/mongo/util/concurrency/thread_pool.h index 16e9ada80bd..b95956c4832 100644 --- a/src/mongo/util/concurrency/thread_pool.h +++ b/src/mongo/util/concurrency/thread_pool.h @@ -201,7 +201,7 @@ private: * Executes one task from _pendingTasks. "lk" must own _mutex, and _pendingTasks must have at * least one entry. */ - void _doOneTask(stdx::unique_lock<stdx::mutex>* lk); + void _doOneTask(stdx::unique_lock<stdx::mutex>* lk) noexcept; /** * Changes the lifecycle state (_state) of the pool and wakes up any threads waiting for a state diff --git a/src/mongo/util/concurrency/thread_pool_test_common.cpp b/src/mongo/util/concurrency/thread_pool_test_common.cpp index 70a0c41ef19..5df135ac19c 100644 --- a/src/mongo/util/concurrency/thread_pool_test_common.cpp +++ b/src/mongo/util/concurrency/thread_pool_test_common.cpp @@ -151,11 +151,15 @@ COMMON_THREAD_POOL_DEATH_TEST(DieOnDoubleStartUp, "it has already started") { pool.startup(); } -COMMON_THREAD_POOL_DEATH_TEST(DieWhenExceptionBubblesUp, "Exception escaped task in") { +namespace { +constexpr auto kExceptionMessage = "No good very bad exception"; +} + +COMMON_THREAD_POOL_DEATH_TEST(DieWhenExceptionBubblesUp, kExceptionMessage) { auto& pool = getThreadPool(); pool.startup(); ASSERT_OK(pool.schedule([] { - uassertStatusOK(Status({ErrorCodes::BadValue, "No good very bad exception"})); + uassertStatusOK(Status({ErrorCodes::BadValue, kExceptionMessage})); })); pool.shutdown(); pool.join(); |