vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 19 | #include "src/cpp/server/dynamic_thread_pool.h" |
| 20 | |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 21 | #include <mutex> |
| 22 | #include <thread> |
yang-g | 3e4bd95 | 2015-08-24 15:54:07 -0700 | [diff] [blame] | 23 | |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 24 | #include <grpc/support/log.h> |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 25 | |
| 26 | namespace grpc { |
Craig Tiller | 7c70b6c | 2017-01-23 07:48:42 -0800 | [diff] [blame] | 27 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 28 | DynamicThreadPool::DynamicThread::DynamicThread(DynamicThreadPool* pool) |
| 29 | : pool_(pool), |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 30 | thd_(new std::thread(&DynamicThreadPool::DynamicThread::ThreadFunc, |
Vijay Pai | 0109d16 | 2016-11-01 17:20:42 -0700 | [diff] [blame] | 31 | this)) {} |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 32 | DynamicThreadPool::DynamicThread::~DynamicThread() { |
| 33 | thd_->join(); |
| 34 | thd_.reset(); |
| 35 | } |
| 36 | |
| 37 | void DynamicThreadPool::DynamicThread::ThreadFunc() { |
| 38 | pool_->ThreadFunc(); |
| 39 | // Now that we have killed ourselves, we should reduce the thread count |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 40 | std::unique_lock<std::mutex> lock(pool_->mu_); |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 41 | pool_->nthreads_--; |
vjpai | 02b8054 | 2015-07-23 17:44:45 -0700 | [diff] [blame] | 42 | // Move ourselves to dead list |
| 43 | pool_->dead_threads_.push_back(this); |
| 44 | |
vjpai | b76f3ad | 2015-07-23 14:41:23 -0700 | [diff] [blame] | 45 | if ((pool_->shutdown_) && (pool_->nthreads_ == 0)) { |
| 46 | pool_->shutdown_cv_.notify_one(); |
| 47 | } |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 48 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 49 | |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 50 | void DynamicThreadPool::ThreadFunc() { |
| 51 | for (;;) { |
| 52 | // Wait until work is available or we are shutting down. |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 53 | std::unique_lock<std::mutex> lock(mu_); |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 54 | if (!shutdown_ && callbacks_.empty()) { |
| 55 | // If there are too many threads waiting, then quit this thread |
vjpai | 02b8054 | 2015-07-23 17:44:45 -0700 | [diff] [blame] | 56 | if (threads_waiting_ >= reserve_threads_) { |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 57 | break; |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 58 | } |
| 59 | threads_waiting_++; |
| 60 | cv_.wait(lock); |
| 61 | threads_waiting_--; |
| 62 | } |
| 63 | // Drain callbacks before considering shutdown to ensure all work |
| 64 | // gets completed. |
| 65 | if (!callbacks_.empty()) { |
| 66 | auto cb = callbacks_.front(); |
| 67 | callbacks_.pop(); |
| 68 | lock.unlock(); |
| 69 | cb(); |
| 70 | } else if (shutdown_) { |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 76 | DynamicThreadPool::DynamicThreadPool(int reserve_threads) |
| 77 | : shutdown_(false), |
| 78 | reserve_threads_(reserve_threads), |
| 79 | nthreads_(0), |
| 80 | threads_waiting_(0) { |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 81 | for (int i = 0; i < reserve_threads_; i++) { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 82 | std::lock_guard<std::mutex> lock(mu_); |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 83 | nthreads_++; |
vjpai | 02b8054 | 2015-07-23 17:44:45 -0700 | [diff] [blame] | 84 | new DynamicThread(this); |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | void DynamicThreadPool::ReapThreads(std::list<DynamicThread*>* tlist) { |
vjpai | 24b3b7e | 2015-07-23 18:51:03 -0700 | [diff] [blame] | 89 | for (auto t = tlist->begin(); t != tlist->end(); t = tlist->erase(t)) { |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 90 | delete *t; |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 91 | } |
| 92 | } |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 93 | |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 94 | DynamicThreadPool::~DynamicThreadPool() { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 95 | std::unique_lock<std::mutex> lock(mu_); |
vjpai | b76f3ad | 2015-07-23 14:41:23 -0700 | [diff] [blame] | 96 | shutdown_ = true; |
| 97 | cv_.notify_all(); |
| 98 | while (nthreads_ != 0) { |
| 99 | shutdown_cv_.wait(lock); |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 100 | } |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 101 | ReapThreads(&dead_threads_); |
| 102 | } |
| 103 | |
| 104 | void DynamicThreadPool::Add(const std::function<void()>& callback) { |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 105 | std::lock_guard<std::mutex> lock(mu_); |
vjpai | 02b8054 | 2015-07-23 17:44:45 -0700 | [diff] [blame] | 106 | // Add works to the callbacks list |
| 107 | callbacks_.push(callback); |
| 108 | // Increase pool size or notify as needed |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 109 | if (threads_waiting_ == 0) { |
| 110 | // Kick off a new thread |
| 111 | nthreads_++; |
vjpai | 02b8054 | 2015-07-23 17:44:45 -0700 | [diff] [blame] | 112 | new DynamicThread(this); |
| 113 | } else { |
| 114 | cv_.notify_one(); |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 115 | } |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 116 | // Also use this chance to harvest dead threads |
| 117 | if (!dead_threads_.empty()) { |
| 118 | ReapThreads(&dead_threads_); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | } // namespace grpc |