/Users/deen/code/yugabyte-db/src/yb/rpc/thread_pool.h
Line | Count | Source |
1 | | // |
2 | | // Copyright (c) YugaByte, Inc. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
5 | | // in compliance with the License. You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
10 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
11 | | // or implied. See the License for the specific language governing permissions and limitations |
12 | | // under the License. |
13 | | // |
14 | | // |
15 | | |
16 | | #ifndef YB_RPC_THREAD_POOL_H |
17 | | #define YB_RPC_THREAD_POOL_H |
18 | | |
19 | | #include <memory> |
20 | | #include <string> |
21 | | |
22 | | #include "yb/gutil/port.h" |
23 | | |
24 | | #include "yb/util/tostring.h" |
25 | | |
26 | | namespace yb { |
27 | | |
28 | | class Status; |
29 | | class Thread; |
30 | | |
31 | | namespace rpc { |
32 | | |
33 | | class ThreadPoolTask { |
34 | | public: |
35 | | // Invoked in thread pool |
36 | | virtual void Run() = 0; |
37 | | |
38 | | // When thread pool done with task, i.e. it completed or failed, it invokes Done |
39 | | virtual void Done(const Status& status) = 0; |
40 | | |
41 | | protected: |
42 | 196M | ~ThreadPoolTask() {} |
43 | | }; |
44 | | |
45 | | template <class F, class Base = ThreadPoolTask> |
46 | | class FunctorThreadPoolTask : public Base { |
47 | | public: |
48 | | explicit FunctorThreadPoolTask(const F& f) : f_(f) {} |
49 | | explicit FunctorThreadPoolTask(F&& f) : f_(std::move(f)) {} |
50 | | |
51 | | virtual ~FunctorThreadPoolTask() = default; |
52 | | |
53 | | private: |
54 | | void Run() override { |
55 | | f_(); |
56 | | } |
57 | | |
58 | | void Done(const Status& status) override { |
59 | | delete this; |
60 | | } |
61 | | |
62 | | F f_; |
63 | | }; |
64 | | |
65 | | template <class F> |
66 | | FunctorThreadPoolTask<F>* MakeFunctorThreadPoolTask(const F& f) { |
67 | | return new FunctorThreadPoolTask<F>(f); |
68 | | } |
69 | | |
70 | | template <class F> |
71 | | FunctorThreadPoolTask<F>* MakeFunctorThreadPoolTask(F&& f) { |
72 | | return new FunctorThreadPoolTask<F>(std::move(f)); |
73 | | } |
74 | | |
75 | | struct ThreadPoolOptions { |
76 | | std::string name; |
77 | | size_t queue_limit; |
78 | | size_t max_workers; |
79 | | |
80 | 61.9k | std::string ToString() const { |
81 | 61.9k | return YB_STRUCT_TO_STRING(name, queue_limit, max_workers); |
82 | 61.9k | } |
83 | | }; |
84 | | |
85 | | class ThreadPool { |
86 | | public: |
87 | | explicit ThreadPool(ThreadPoolOptions options); |
88 | | |
89 | | template <class... Args> |
90 | | explicit ThreadPool(Args&&... args) |
91 | 61.9k | : ThreadPool(ThreadPoolOptions{std::forward<Args>(args)...}) { |
92 | | |
93 | 61.9k | } yb::rpc::ThreadPool::ThreadPool<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, unsigned long const&, unsigned long const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&&, unsigned long const&, unsigned long const&) Line | Count | Source | 91 | 16.7k | : ThreadPool(ThreadPoolOptions{std::forward<Args>(args)...}) { | 92 | | | 93 | 16.7k | } |
yb::rpc::ThreadPool::ThreadPool<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long const&, unsigned long const&>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, unsigned long const&, unsigned long const&) Line | Count | Source | 91 | 35.2k | : ThreadPool(ThreadPoolOptions{std::forward<Args>(args)...}) { | 92 | | | 93 | 35.2k | } |
yb::rpc::ThreadPool::ThreadPool<char const (&) [19], unsigned long long&, unsigned long long&>(char const (&) [19], unsigned long long&, unsigned long long&) Line | Count | Source | 91 | 9.79k | : ThreadPool(ThreadPoolOptions{std::forward<Args>(args)...}) { | 92 | | | 93 | 9.79k | } |
yb::rpc::ThreadPool::ThreadPool<char const (&) [9], unsigned long const&, unsigned long const&>(char const (&) [9], unsigned long const&, unsigned long const&) Line | Count | Source | 91 | 82 | : ThreadPool(ThreadPoolOptions{std::forward<Args>(args)...}) { | 92 | | | 93 | 82 | } |
|
94 | | |
95 | | ~ThreadPool(); |
96 | | |
97 | | ThreadPool(ThreadPool&& rhs) noexcept; |
98 | | ThreadPool& operator=(ThreadPool&& rhs) noexcept; |
99 | | |
100 | | const ThreadPoolOptions& options() const; |
101 | | |
102 | | bool Enqueue(ThreadPoolTask* task); |
103 | | |
104 | | template <class F> |
105 | | void EnqueueFunctor(const F& f) { |
106 | | Enqueue(MakeFunctorThreadPoolTask(f)); |
107 | | } |
108 | | |
109 | | template <class F> |
110 | | void EnqueueFunctor(F&& f) { |
111 | | Enqueue(MakeFunctorThreadPoolTask(std::move(f))); |
112 | | } |
113 | | |
114 | | void Shutdown(); |
115 | | |
116 | | static bool IsCurrentThreadRpcWorker(); |
117 | | |
118 | | bool Owns(Thread* thread); |
119 | | bool OwnsThisThread(); |
120 | | |
121 | | private: |
122 | | class Impl; |
123 | | |
124 | | std::unique_ptr<Impl> impl_; |
125 | | }; |
126 | | |
127 | | } // namespace rpc |
128 | | } // namespace yb |
129 | | |
130 | | #endif // YB_RPC_THREAD_POOL_H |