GRPC C++  1.26.0
executor.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2015 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_CORE_LIB_IOMGR_EXECUTOR_H
20 #define GRPC_CORE_LIB_IOMGR_EXECUTOR_H
21 
23 
25 #include "src/core/lib/gprpp/thd.h"
27 
28 namespace grpc_core {
29 
30 struct ThreadState {
32  size_t id; // For debugging purposes
33  const char* name; // Thread state name
36  size_t depth; // Number of closures in the closure list
37  bool shutdown;
40 };
41 
42 enum class ExecutorType {
43  DEFAULT = 0,
44  RESOLVER,
45 
46  NUM_EXECUTORS // Add new values above this
47 };
48 
49 enum class ExecutorJobType {
50  SHORT = 0,
51  LONG,
52  NUM_JOB_TYPES // Add new values above this
53 };
54 
55 class Executor {
56  public:
57  Executor(const char* executor_name);
58 
59  void Init();
60 
62  bool IsThreaded() const;
63 
64  /* Enable/disable threading - must be called after Init and Shutdown(). Never
65  * call SetThreading(false) in the middle of an application */
66  void SetThreading(bool threading);
67 
69  void Shutdown();
70 
73  void Enqueue(grpc_closure* closure, grpc_error* error, bool is_short);
74 
75  // TODO(sreek): Currently we have two executors (available globally): The
76  // default executor and the resolver executor.
77  //
78  // Some of the functions below operate on the DEFAULT executor only while some
79  // operate of ALL the executors. This is a bit confusing and should be cleaned
80  // up in future (where we make all the following functions take ExecutorType
81  // and/or JobType)
82 
83  // Initialize ALL the executors
84  static void InitAll();
85 
86  static void Run(grpc_closure* closure, grpc_error* error,
87  ExecutorType executor_type = ExecutorType::DEFAULT,
89 
90  // Shutdown ALL the executors
91  static void ShutdownAll();
92 
93  // Set the threading mode for ALL the executors
94  static void SetThreadingAll(bool enable);
95 
96  // Set the threading mode for ALL the executors
97  static void SetThreadingDefault(bool enable);
98 
99  // Return if a given executor is running in threaded mode (i.e if
100  // SetThreading(true) was called previously on that executor)
101  static bool IsThreaded(ExecutorType executor_type);
102 
103  // Return if the DEFAULT executor is threaded
104  static bool IsThreadedDefault();
105 
106  private:
107  static size_t RunClosures(const char* executor_name, grpc_closure_list list);
108  static void ThreadMain(void* arg);
109 
110  const char* name_;
111  ThreadState* thd_state_;
112  size_t max_threads_;
113  gpr_atm num_threads_;
114  gpr_spinlock adding_thread_lock_;
115 };
116 
117 // Global initializer for executor
119 
120 } // namespace grpc_core
121 
122 #endif /* GRPC_CORE_LIB_IOMGR_EXECUTOR_H */
Definition: executor.h:55
Executor(const char *executor_name)
static void SetThreadingAll(bool enable)
bool IsThreaded() const
Is the executor multi-threaded?
void Enqueue(grpc_closure *closure, grpc_error *error, bool is_short)
Enqueue the closure onto the executor.
static bool IsThreaded(ExecutorType executor_type)
static void Run(grpc_closure *closure, grpc_error *error, ExecutorType executor_type=ExecutorType::DEFAULT, ExecutorJobType job_type=ExecutorJobType::SHORT)
void SetThreading(bool threading)
static void InitAll()
static bool IsThreadedDefault()
void Shutdown()
Shutdown the executor, running all pending work as part of the call.
static void SetThreadingDefault(bool enable)
static void ShutdownAll()
Definition: thd.h:46
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:30
pthread_cond_t gpr_cv
Definition: sync_posix.h:46
Internal thread interface.
Definition: backoff.h:26
ExecutorJobType
Definition: executor.h:49
void grpc_executor_global_init()
ExecutorType
Definition: executor.h:42
Definition: sync_windows.h:26
Definition: spinlock.h:28
Definition: closure.h:41
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
Definition: executor.h:30
grpc_closure_list elems
Definition: executor.h:35
gpr_mu mu
Definition: executor.h:31
bool shutdown
Definition: executor.h:37
const char * name
Definition: executor.h:33
gpr_cv cv
Definition: executor.h:34
bool queued_long_job
Definition: executor.h:38
size_t depth
Definition: executor.h:36
grpc_core::Thread thd
Definition: executor.h:39
size_t id
Definition: executor.h:32
Definition: error_internal.h:39