dynamic_thread_pool.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include "src/cpp/server/dynamic_thread_pool.h"
  19. #include <mutex>
  20. #include <thread>
  21. #include <grpc/support/log.h>
  22. namespace grpc {
  23. DynamicThreadPool::DynamicThread::DynamicThread(DynamicThreadPool* pool)
  24. : pool_(pool),
  25. thd_(new std::thread(&DynamicThreadPool::DynamicThread::ThreadFunc,
  26. this)) {}
  27. DynamicThreadPool::DynamicThread::~DynamicThread() {
  28. thd_->join();
  29. thd_.reset();
  30. }
  31. void DynamicThreadPool::DynamicThread::ThreadFunc() {
  32. pool_->ThreadFunc();
  33. // Now that we have killed ourselves, we should reduce the thread count
  34. std::unique_lock<std::mutex> lock(pool_->mu_);
  35. pool_->nthreads_--;
  36. // Move ourselves to dead list
  37. pool_->dead_threads_.push_back(this);
  38. if ((pool_->shutdown_) && (pool_->nthreads_ == 0)) {
  39. pool_->shutdown_cv_.notify_one();
  40. }
  41. }
  42. void DynamicThreadPool::ThreadFunc() {
  43. for (;;) {
  44. // Wait until work is available or we are shutting down.
  45. std::unique_lock<std::mutex> lock(mu_);
  46. if (!shutdown_ && callbacks_.empty()) {
  47. // If there are too many threads waiting, then quit this thread
  48. if (threads_waiting_ >= reserve_threads_) {
  49. break;
  50. }
  51. threads_waiting_++;
  52. cv_.wait(lock);
  53. threads_waiting_--;
  54. }
  55. // Drain callbacks before considering shutdown to ensure all work
  56. // gets completed.
  57. if (!callbacks_.empty()) {
  58. auto cb = callbacks_.front();
  59. callbacks_.pop();
  60. lock.unlock();
  61. cb();
  62. } else if (shutdown_) {
  63. break;
  64. }
  65. }
  66. }
  67. DynamicThreadPool::DynamicThreadPool(int reserve_threads)
  68. : shutdown_(false),
  69. reserve_threads_(reserve_threads),
  70. nthreads_(0),
  71. threads_waiting_(0) {
  72. for (int i = 0; i < reserve_threads_; i++) {
  73. std::lock_guard<std::mutex> lock(mu_);
  74. nthreads_++;
  75. new DynamicThread(this);
  76. }
  77. }
  78. void DynamicThreadPool::ReapThreads(std::list<DynamicThread*>* tlist) {
  79. for (auto t = tlist->begin(); t != tlist->end(); t = tlist->erase(t)) {
  80. delete *t;
  81. }
  82. }
  83. DynamicThreadPool::~DynamicThreadPool() {
  84. std::unique_lock<std::mutex> lock(mu_);
  85. shutdown_ = true;
  86. cv_.notify_all();
  87. while (nthreads_ != 0) {
  88. shutdown_cv_.wait(lock);
  89. }
  90. ReapThreads(&dead_threads_);
  91. }
  92. void DynamicThreadPool::Add(const std::function<void()>& callback) {
  93. std::lock_guard<std::mutex> lock(mu_);
  94. // Add works to the callbacks list
  95. callbacks_.push(callback);
  96. // Increase pool size or notify as needed
  97. if (threads_waiting_ == 0) {
  98. // Kick off a new thread
  99. nthreads_++;
  100. new DynamicThread(this);
  101. } else {
  102. cv_.notify_one();
  103. }
  104. // Also use this chance to harvest dead threads
  105. if (!dead_threads_.empty()) {
  106. ReapThreads(&dead_threads_);
  107. }
  108. }
  109. } // namespace grpc