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 | |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 19 | #ifndef GRPC_INTERNAL_CPP_DYNAMIC_THREAD_POOL_H |
| 20 | #define GRPC_INTERNAL_CPP_DYNAMIC_THREAD_POOL_H |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 21 | |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 22 | #include <condition_variable> |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 23 | #include <list> |
Nicolas "Pixel" Noble | b3d69c3 | 2015-07-29 06:12:51 +0200 | [diff] [blame] | 24 | #include <memory> |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 25 | #include <mutex> |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 26 | #include <queue> |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 27 | #include <thread> |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 28 | |
yang-g | 9d1f0c4 | 2015-08-24 16:08:36 -0700 | [diff] [blame] | 29 | #include <grpc++/support/config.h> |
| 30 | |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 31 | #include "src/cpp/server/thread_pool_interface.h" |
| 32 | |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 33 | namespace grpc { |
| 34 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 35 | class DynamicThreadPool final : public ThreadPoolInterface { |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 36 | public: |
| 37 | explicit DynamicThreadPool(int reserve_threads); |
| 38 | ~DynamicThreadPool(); |
| 39 | |
Vijay Pai | c0b2acb | 2016-11-01 16:31:56 -0700 | [diff] [blame] | 40 | void Add(const std::function<void()>& callback) override; |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 41 | |
| 42 | private: |
| 43 | class DynamicThread { |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 44 | public: |
| 45 | DynamicThread(DynamicThreadPool* pool); |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 46 | ~DynamicThread(); |
Craig Tiller | d6c98df | 2015-08-18 09:33:44 -0700 | [diff] [blame] | 47 | |
| 48 | private: |
| 49 | DynamicThreadPool* pool_; |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 50 | std::unique_ptr<std::thread> thd_; |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 51 | void ThreadFunc(); |
| 52 | }; |
Vijay Pai | 320ed13 | 2016-11-01 17:16:55 -0700 | [diff] [blame] | 53 | std::mutex mu_; |
| 54 | std::condition_variable cv_; |
| 55 | std::condition_variable shutdown_cv_; |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 56 | bool shutdown_; |
| 57 | std::queue<std::function<void()>> callbacks_; |
| 58 | int reserve_threads_; |
| 59 | int nthreads_; |
| 60 | int threads_waiting_; |
vjpai | b28456b | 2015-07-23 14:17:10 -0700 | [diff] [blame] | 61 | std::list<DynamicThread*> dead_threads_; |
| 62 | |
| 63 | void ThreadFunc(); |
| 64 | static void ReapThreads(std::list<DynamicThread*>* tlist); |
| 65 | }; |
| 66 | |
| 67 | } // namespace grpc |
| 68 | |
Vijay Pai | e8a7e30 | 2015-08-24 10:52:33 -0700 | [diff] [blame] | 69 | #endif // GRPC_INTERNAL_CPP_DYNAMIC_THREAD_POOL_H |