blob: 9237c6e5caca2ee0235ef445ad44e6b2e88a4cdd [file] [log] [blame]
vjpaib28456b2015-07-23 14:17:10 -07001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
vjpaib28456b2015-07-23 14:17:10 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
vjpaib28456b2015-07-23 14:17:10 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
vjpaib28456b2015-07-23 14:17:10 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
vjpaib28456b2015-07-23 14:17:10 -070016 *
17 */
18
Vijay Paie8a7e302015-08-24 10:52:33 -070019#ifndef GRPC_INTERNAL_CPP_DYNAMIC_THREAD_POOL_H
20#define GRPC_INTERNAL_CPP_DYNAMIC_THREAD_POOL_H
vjpaib28456b2015-07-23 14:17:10 -070021
Vijay Pai320ed132016-11-01 17:16:55 -070022#include <condition_variable>
vjpaib28456b2015-07-23 14:17:10 -070023#include <list>
Nicolas "Pixel" Nobleb3d69c32015-07-29 06:12:51 +020024#include <memory>
Vijay Pai320ed132016-11-01 17:16:55 -070025#include <mutex>
vjpaib28456b2015-07-23 14:17:10 -070026#include <queue>
Vijay Pai320ed132016-11-01 17:16:55 -070027#include <thread>
vjpaib28456b2015-07-23 14:17:10 -070028
yang-g9d1f0c42015-08-24 16:08:36 -070029#include <grpc++/support/config.h>
30
Vijay Paie8a7e302015-08-24 10:52:33 -070031#include "src/cpp/server/thread_pool_interface.h"
32
vjpaib28456b2015-07-23 14:17:10 -070033namespace grpc {
34
Vijay Paic0b2acb2016-11-01 16:31:56 -070035class DynamicThreadPool final : public ThreadPoolInterface {
vjpaib28456b2015-07-23 14:17:10 -070036 public:
37 explicit DynamicThreadPool(int reserve_threads);
38 ~DynamicThreadPool();
39
Vijay Paic0b2acb2016-11-01 16:31:56 -070040 void Add(const std::function<void()>& callback) override;
vjpaib28456b2015-07-23 14:17:10 -070041
42 private:
43 class DynamicThread {
Craig Tillerd6c98df2015-08-18 09:33:44 -070044 public:
45 DynamicThread(DynamicThreadPool* pool);
vjpaib28456b2015-07-23 14:17:10 -070046 ~DynamicThread();
Craig Tillerd6c98df2015-08-18 09:33:44 -070047
48 private:
49 DynamicThreadPool* pool_;
Vijay Pai320ed132016-11-01 17:16:55 -070050 std::unique_ptr<std::thread> thd_;
vjpaib28456b2015-07-23 14:17:10 -070051 void ThreadFunc();
52 };
Vijay Pai320ed132016-11-01 17:16:55 -070053 std::mutex mu_;
54 std::condition_variable cv_;
55 std::condition_variable shutdown_cv_;
vjpaib28456b2015-07-23 14:17:10 -070056 bool shutdown_;
57 std::queue<std::function<void()>> callbacks_;
58 int reserve_threads_;
59 int nthreads_;
60 int threads_waiting_;
vjpaib28456b2015-07-23 14:17:10 -070061 std::list<DynamicThread*> dead_threads_;
62
63 void ThreadFunc();
64 static void ReapThreads(std::list<DynamicThread*>* tlist);
65};
66
67} // namespace grpc
68
Vijay Paie8a7e302015-08-24 10:52:33 -070069#endif // GRPC_INTERNAL_CPP_DYNAMIC_THREAD_POOL_H