thread_pool.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: vitus@google.com (Michael Vitus)
  30. #include "ceres/thread_pool.h"
  31. #include <cmath>
  32. #include <limits>
  33. #include "ceres/internal/config.h"
  34. namespace ceres::internal {
  35. namespace {
  36. // Constrain the total number of threads to the amount the hardware can support.
  37. int GetNumAllowedThreads(int requested_num_threads) {
  38. return std::min(requested_num_threads, ThreadPool::MaxNumThreadsAvailable());
  39. }
  40. } // namespace
  41. int ThreadPool::MaxNumThreadsAvailable() {
  42. const int num_hardware_threads = std::thread::hardware_concurrency();
  43. // hardware_concurrency() can return 0 if the value is not well defined or not
  44. // computable.
  45. return num_hardware_threads == 0 ? std::numeric_limits<int>::max()
  46. : num_hardware_threads;
  47. }
  48. ThreadPool::ThreadPool() = default;
  49. ThreadPool::ThreadPool(int num_threads) { Resize(num_threads); }
  50. ThreadPool::~ThreadPool() {
  51. std::lock_guard<std::mutex> lock(thread_pool_mutex_);
  52. // Signal the thread workers to stop and wait for them to finish all scheduled
  53. // tasks.
  54. Stop();
  55. for (std::thread& thread : thread_pool_) {
  56. thread.join();
  57. }
  58. }
  59. void ThreadPool::Resize(int num_threads) {
  60. std::lock_guard<std::mutex> lock(thread_pool_mutex_);
  61. const int num_current_threads = thread_pool_.size();
  62. if (num_current_threads >= num_threads) {
  63. return;
  64. }
  65. const int create_num_threads =
  66. GetNumAllowedThreads(num_threads) - num_current_threads;
  67. for (int i = 0; i < create_num_threads; ++i) {
  68. thread_pool_.emplace_back(&ThreadPool::ThreadMainLoop, this);
  69. }
  70. }
  71. void ThreadPool::AddTask(const std::function<void()>& func) {
  72. task_queue_.Push(func);
  73. }
  74. int ThreadPool::Size() {
  75. std::lock_guard<std::mutex> lock(thread_pool_mutex_);
  76. return thread_pool_.size();
  77. }
  78. void ThreadPool::ThreadMainLoop() {
  79. std::function<void()> task;
  80. while (task_queue_.Wait(&task)) {
  81. task();
  82. }
  83. }
  84. void ThreadPool::Stop() { task_queue_.StopWaiters(); }
  85. } // namespace ceres::internal