GRPC Core  9.0.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,
88  ExecutorJobType job_type = ExecutorJobType::SHORT);
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 */
grpc_core::Executor::IsThreadedDefault
static bool IsThreadedDefault()
Definition: executor.cc:450
grpc_core::Executor::InitAll
static void InitAll()
Definition: executor.cc:385
grpc_core::ThreadState::shutdown
bool shutdown
Definition: executor.h:37
spinlock.h
gpr_spinlock
Definition: spinlock.h:28
grpc_core
Round Robin Policy.
Definition: backend_metric.cc:24
gpr_cv
pthread_cond_t gpr_cv
Definition: sync_posix.h:46
grpc_core::Thread
Definition: thd.h:46
closure.h
grpc_core::Executor::SetThreadingDefault
static void SetThreadingDefault(bool enable)
Definition: executor.cc:462
grpc_core::Executor::SetThreadingAll
static void SetThreadingAll(bool enable)
Definition: executor.cc:454
grpc_core::ThreadState::depth
size_t depth
Definition: executor.h:36
grpc_core::Executor
Definition: executor.h:55
grpc_core::ExecutorJobType::SHORT
@ SHORT
grpc_core::ThreadState::id
size_t id
Definition: executor.h:32
grpc_core::Executor::Enqueue
void Enqueue(grpc_closure *closure, grpc_error *error, bool is_short)
Enqueue the closure onto the executor.
Definition: executor.cc:254
grpc_core::Executor::Run
static void Run(grpc_closure *closure, grpc_error *error, ExecutorType executor_type=ExecutorType::DEFAULT, ExecutorJobType job_type=ExecutorJobType::SHORT)
Definition: executor.cc:406
grpc_core::Executor::IsThreaded
bool IsThreaded() const
Is the executor multi-threaded?
Definition: executor.cc:136
grpc_core::ExecutorType::DEFAULT
@ DEFAULT
grpc_closure_list
Definition: closure.h:41
grpc_core::Executor::ShutdownAll
static void ShutdownAll()
Definition: executor.cc:412
grpc_core::Executor::SetThreading
void SetThreading(bool threading)
Definition: executor.cc:140
gpr_atm
intptr_t gpr_atm
Definition: atm_gcc_atomic.h:30
grpc_core::ThreadState::cv
gpr_cv cv
Definition: executor.h:34
grpc_core::ThreadState::thd
grpc_core::Thread thd
Definition: executor.h:39
grpc_core::ExecutorType
ExecutorType
Definition: executor.h:42
grpc_core::Executor::Shutdown
void Shutdown()
Shutdown the executor, running all pending work as part of the call.
Definition: executor.cc:213
grpc_core::ThreadState::queued_long_job
bool queued_long_job
Definition: executor.h:38
grpc_core::Executor::Init
void Init()
Definition: executor.cc:98
thd.h
grpc_core::ThreadState
Definition: executor.h:30
gpr_mu
Definition: sync_windows.h:26
grpc_core::ThreadState::name
const char * name
Definition: executor.h:33
grpc_error
Definition: error_internal.h:39
grpc_closure
A closure over a grpc_iomgr_cb_func.
Definition: closure.h:56
grpc_core::ThreadState::mu
gpr_mu mu
Definition: executor.h:31
grpc_core::ExecutorJobType
ExecutorJobType
Definition: executor.h:49
grpc_core::grpc_executor_global_init
void grpc_executor_global_init()
Definition: executor.cc:467
grpc_core::Executor::Executor
Executor(const char *executor_name)
Definition: executor.cc:92
port_platform.h
grpc_core::ThreadState::elems
grpc_closure_list elems
Definition: executor.h:35