src/cpp/thread_manager/thread_manager.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2016 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 
19 #ifndef GRPC_INTERNAL_CPP_THREAD_MANAGER_H
20 #define GRPC_INTERNAL_CPP_THREAD_MANAGER_H
21 
22 #include <list>
23 
25 #include "src/core/lib/gprpp/thd.h"
28 
29 namespace grpc {
30 
32  public:
34  int min_pollers, int max_pollers);
35  virtual ~ThreadManager();
36 
37  // Initializes and Starts the Rpc Manager threads
38  void Initialize();
39 
40  // The return type of PollForWork() function
42 
43  // "Polls" for new work.
44  // If the return value is WORK_FOUND:
45  // - The implementaion of PollForWork() MAY set some opaque identifier to
46  // (identify the work item found) via the '*tag' parameter
47  // - The implementaion MUST set the value of 'ok' to 'true' or 'false'. A
48  // value of 'false' indicates some implemenation specific error (that is
49  // neither SHUTDOWN nor TIMEOUT)
50  // - ThreadManager does not interpret the values of 'tag' and 'ok'
51  // - ThreadManager WILL call DoWork() and pass '*tag' and 'ok' as input to
52  // DoWork()
53  //
54  // If the return value is SHUTDOWN:,
55  // - ThreadManager WILL NOT call DoWork() and terminates the thread
56  //
57  // If the return value is TIMEOUT:,
58  // - ThreadManager WILL NOT call DoWork()
59  // - ThreadManager MAY terminate the thread depending on the current number
60  // of active poller threads and mix_pollers/max_pollers settings
61  // - Also, the value of timeout is specific to the derived class
62  // implementation
63  virtual WorkStatus PollForWork(void** tag, bool* ok) = 0;
64 
65  // The implementation of DoWork() is supposed to perform the work found by
66  // PollForWork(). The tag and ok parameters are the same as returned by
67  // PollForWork(). The resources parameter indicates that the call actually
68  // has the resources available for performing the RPC's work. If it doesn't,
69  // the implementation should fail it appropriately.
70  //
71  // The implementation of DoWork() should also do any setup needed to ensure
72  // that the next call to PollForWork() (not necessarily by the current thread)
73  // actually finds some work
74  virtual void DoWork(void* tag, bool ok, bool resources) = 0;
75 
76  // Mark the ThreadManager as shutdown and begin draining the work. This is a
77  // non-blocking call and the caller should call Wait(), a blocking call which
78  // returns only once the shutdown is complete
79  virtual void Shutdown();
80 
81  // Has Shutdown() been called
82  bool IsShutdown();
83 
84  // A blocking call that returns only after the ThreadManager has shutdown and
85  // all the threads have drained all the outstanding work
86  virtual void Wait();
87 
88  // Max number of concurrent threads that were ever active in this thread
89  // manager so far. This is useful for debugging purposes (and in unit tests)
90  // to check if resource_quota is properly being enforced.
92 
93  private:
94  // Helper wrapper class around grpc_core::Thread. Takes a ThreadManager object
95  // and starts a new grpc_core::Thread to calls the Run() function.
96  //
97  // The Run() function calls ThreadManager::MainWorkLoop() function and once
98  // that completes, it marks the WorkerThread completed by calling
99  // ThreadManager::MarkAsCompleted()
100  //
101  // WHY IS THIS NEEDED?:
102  // When a thread terminates, some other thread *must* call Join() on that
103  // thread so that the resources are released. Having a WorkerThread wrapper
104  // will make this easier. Once Run() completes, each thread calls the
105  // following two functions:
106  // ThreadManager::CleanupCompletedThreads()
107  // ThreadManager::MarkAsCompleted()
108  //
109  // - MarkAsCompleted() puts the WorkerThread object in the ThreadManger's
110  // completed_threads_ list
111  // - CleanupCompletedThreads() calls "Join()" on the threads that are already
112  // in the completed_threads_ list (since a thread cannot call Join() on
113  // itself, it calls CleanupCompletedThreads() *before* calling
114  // MarkAsCompleted())
115  //
116  // TODO(sreek): Consider creating the threads 'detached' so that Join() need
117  // not be called (and the need for this WorkerThread class is eliminated)
118  class WorkerThread {
119  public:
120  explicit WorkerThread(ThreadManager* thd_mgr);
121  ~WorkerThread();
122 
123  bool created() const { return created_; }
124  void Start() { thd_.Start(); }
125 
126  private:
127  // Calls thd_mgr_->MainWorkLoop() and once that completes, calls
128  // thd_mgr_>MarkAsCompleted(this) to mark the thread as completed
129  void Run();
130 
133  bool created_;
134  };
135 
136  // The main function in ThreadManager
137  void MainWorkLoop();
138 
141 
142  // Protects shutdown_, num_pollers_, num_threads_ and
143  // max_active_threads_sofar_
145 
146  bool shutdown_;
148 
149  // The resource user object to use when requesting quota to create threads
150  //
151  // Note: The user of this ThreadManager object must create grpc_resource_quota
152  // object (that contains the actual max thread quota) and a grpc_resource_user
153  // object through which quota is requested whenever new threads need to be
154  // created
156 
157  // Number of threads doing polling
159 
160  // The minimum and maximum number of threads that should be doing polling
163 
164  // The total number of threads currently active (includes threads includes the
165  // threads that are currently polling i.e num_pollers_)
167 
168  // See GetMaxActiveThreadsSoFar()'s description.
169  // To be more specific, this variable tracks the max value num_threads_ was
170  // ever set so far
172 
174  std::list<WorkerThread*> completed_threads_;
175 };
176 
177 } // namespace grpc
178 
179 #endif // GRPC_INTERNAL_CPP_THREAD_MANAGER_H
grpc::ThreadManager::SHUTDOWN
@ SHUTDOWN
Definition: src/cpp/thread_manager/thread_manager.h:41
grpc_core::CondVar
Definition: src/core/lib/gprpp/sync.h:126
grpc
Definition: grpcpp/alarm.h:33
grpc::ThreadManager::shutdown_
bool shutdown_
Definition: src/cpp/thread_manager/thread_manager.h:146
grpc_resource_quota
struct grpc_resource_quota grpc_resource_quota
Definition: grpc_types.h:729
grpc::ThreadManager::IsShutdown
bool IsShutdown()
Definition: thread_manager.cc:88
grpc::ThreadManager::Shutdown
virtual void Shutdown()
Definition: thread_manager.cc:83
grpc::ThreadManager::ThreadManager
ThreadManager(const char *name, grpc_resource_quota *resource_quota, int min_pollers, int max_pollers)
Definition: thread_manager.cc:56
grpc::ThreadManager::PollForWork
virtual WorkStatus PollForWork(void **tag, bool *ok)=0
grpc::ThreadManager::WorkerThread::WorkerThread
WorkerThread(ThreadManager *thd_mgr)
Definition: thread_manager.cc:33
grpc::ThreadManager::thread_quota_
grpc_core::ThreadQuotaPtr thread_quota_
Definition: src/cpp/thread_manager/thread_manager.h:155
setup.name
name
Definition: setup.py:542
resource_quota
ResourceQuotaRefPtr resource_quota
Definition: filter_fuzzer.cc:145
grpc::ThreadManager::num_threads_
int num_threads_
Definition: src/cpp/thread_manager/thread_manager.h:166
grpc::ThreadManager::num_pollers_
int num_pollers_
Definition: src/cpp/thread_manager/thread_manager.h:158
grpc::ThreadManager::completed_threads_
std::list< WorkerThread * > completed_threads_
Definition: src/cpp/thread_manager/thread_manager.h:174
grpc::ThreadManager::WorkerThread
Definition: src/cpp/thread_manager/thread_manager.h:118
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
grpc::ThreadManager::WORK_FOUND
@ WORK_FOUND
Definition: src/cpp/thread_manager/thread_manager.h:41
grpc::ThreadManager::Initialize
void Initialize()
Definition: thread_manager.cc:127
grpc::ThreadManager::WorkerThread::thd_mgr_
ThreadManager *const thd_mgr_
Definition: src/cpp/thread_manager/thread_manager.h:131
grpc::ThreadManager::WorkerThread::thd_
grpc_core::Thread thd_
Definition: src/cpp/thread_manager/thread_manager.h:132
grpc::ThreadManager::mu_
grpc_core::Mutex mu_
Definition: src/cpp/thread_manager/thread_manager.h:144
grpc::ThreadManager::min_pollers_
int min_pollers_
Definition: src/cpp/thread_manager/thread_manager.h:161
grpc::ThreadManager::WorkerThread::created_
bool created_
Definition: src/cpp/thread_manager/thread_manager.h:133
grpc::ThreadManager::WorkerThread::~WorkerThread
~WorkerThread()
Definition: thread_manager.cc:51
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
grpc::ThreadManager::MainWorkLoop
void MainWorkLoop()
Definition: thread_manager.cc:150
grpc::ThreadManager::DoWork
virtual void DoWork(void *tag, bool ok, bool resources)=0
grpc::ThreadManager::Wait
virtual void Wait()
Definition: thread_manager.cc:76
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc::ThreadManager::~ThreadManager
virtual ~ThreadManager()
Definition: thread_manager.cc:67
grpc::ThreadManager::WorkerThread::Start
void Start()
Definition: src/cpp/thread_manager/thread_manager.h:124
grpc::ThreadManager::WorkerThread::Run
void Run()
Definition: thread_manager.cc:46
grpc::ThreadManager::WorkerThread::created
bool created() const
Definition: src/cpp/thread_manager/thread_manager.h:123
grpc::ThreadManager::MarkAsCompleted
void MarkAsCompleted(WorkerThread *thd)
Definition: thread_manager.cc:98
thd.h
grpc::ThreadManager::shutdown_cv_
grpc_core::CondVar shutdown_cv_
Definition: src/cpp/thread_manager/thread_manager.h:147
ok
bool ok
Definition: async_end2end_test.cc:197
grpc::ThreadManager::max_pollers_
int max_pollers_
Definition: src/cpp/thread_manager/thread_manager.h:162
grpc_core::Thread
Definition: thd.h:43
grpc::ThreadManager
Definition: src/cpp/thread_manager/thread_manager.h:31
grpc::ThreadManager::GetMaxActiveThreadsSoFar
int GetMaxActiveThreadsSoFar()
Definition: thread_manager.cc:93
thread_quota.h
profile_analyzer.thd
thd
Definition: profile_analyzer.py:168
grpc::ThreadManager::max_active_threads_sofar_
int max_active_threads_sofar_
Definition: src/cpp/thread_manager/thread_manager.h:171
grpc::ThreadManager::CleanupCompletedThreads
void CleanupCompletedThreads()
Definition: thread_manager.cc:116
grpc::ThreadManager::WorkStatus
WorkStatus
Definition: src/cpp/thread_manager/thread_manager.h:41
grpc::ThreadManager::TIMEOUT
@ TIMEOUT
Definition: src/cpp/thread_manager/thread_manager.h:41
grpc::ThreadManager::list_mu_
grpc_core::Mutex list_mu_
Definition: src/cpp/thread_manager/thread_manager.h:173
sync.h
api.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:37