dynamic_thread_pool.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <grpc/support/log.h>
  20. #include <grpcpp/impl/codegen/sync.h>
  21. #include "src/core/lib/gprpp/thd.h"
  22. namespace grpc {
  23. DynamicThreadPool::DynamicThread::DynamicThread(DynamicThreadPool* pool)
  24. : pool_(pool),
  25. thd_("grpcpp_dynamic_pool",
  26. [](void* th) {
  27. static_cast<DynamicThreadPool::DynamicThread*>(th)->ThreadFunc();
  28. },
  29. this) {
  30. thd_.Start();
  31. }
  32. DynamicThreadPool::DynamicThread::~DynamicThread() { thd_.Join(); }
  33. void DynamicThreadPool::DynamicThread::ThreadFunc() {
  34. pool_->ThreadFunc();
  35. // Now that we have killed ourselves, we should reduce the thread count
  36. grpc_core::MutexLock lock(&pool_->mu_);
  37. pool_->nthreads_--;
  38. // Move ourselves to dead list
  39. pool_->dead_threads_.push_back(this);
  40. if ((pool_->shutdown_) && (pool_->nthreads_ == 0)) {
  41. pool_->shutdown_cv_.Signal();
  42. }
  43. }
  44. void DynamicThreadPool::ThreadFunc() {
  45. for (;;) {
  46. // Wait until work is available or we are shutting down.
  47. grpc_core::ReleasableMutexLock lock(&mu_);
  48. if (!shutdown_ && callbacks_.empty()) {
  49. // If there are too many threads waiting, then quit this thread
  50. if (threads_waiting_ >= reserve_threads_) {
  51. break;
  52. }
  53. threads_waiting_++;
  54. cv_.Wait(&mu_);
  55. threads_waiting_--;
  56. }
  57. // Drain callbacks before considering shutdown to ensure all work
  58. // gets completed.
  59. if (!callbacks_.empty()) {
  60. auto cb = callbacks_.front();
  61. callbacks_.pop();
  62. lock.Unlock();
  63. cb();
  64. } else if (shutdown_) {
  65. break;
  66. }
  67. }
  68. }
  69. DynamicThreadPool::DynamicThreadPool(int reserve_threads)
  70. : shutdown_(false),
  71. reserve_threads_(reserve_threads),
  72. nthreads_(0),
  73. threads_waiting_(0) {
  74. for (int i = 0; i < reserve_threads_; i++) {
  75. grpc_core::MutexLock lock(&mu_);
  76. nthreads_++;
  77. new DynamicThread(this);
  78. }
  79. }
  80. void DynamicThreadPool::ReapThreads(std::list<DynamicThread*>* tlist) {
  81. for (auto t = tlist->begin(); t != tlist->end(); t = tlist->erase(t)) {
  82. delete *t;
  83. }
  84. }
  85. DynamicThreadPool::~DynamicThreadPool() {
  86. grpc_core::MutexLock lock(&mu_);
  87. shutdown_ = true;
  88. cv_.Broadcast();
  89. while (nthreads_ != 0) {
  90. shutdown_cv_.Wait(&mu_);
  91. }
  92. ReapThreads(&dead_threads_);
  93. }
  94. void DynamicThreadPool::Add(const std::function<void()>& callback) {
  95. grpc_core::MutexLock lock(&mu_);
  96. // Add works to the callbacks list
  97. callbacks_.push(callback);
  98. // Increase pool size or notify as needed
  99. if (threads_waiting_ == 0) {
  100. // Kick off a new thread
  101. nthreads_++;
  102. new DynamicThread(this);
  103. } else {
  104. cv_.Signal();
  105. }
  106. // Also use this chance to harvest dead threads
  107. if (!dead_threads_.empty()) {
  108. ReapThreads(&dead_threads_);
  109. }
  110. }
  111. } // namespace grpc