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  explicit 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_handle 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 
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_;
112  size_t max_threads_;
115 };
116 
117 // Global initializer for executor
119 
120 } // namespace grpc_core
121 
122 #endif /* GRPC_CORE_LIB_IOMGR_EXECUTOR_H */
grpc_core::ThreadState::mu
gpr_mu mu
Definition: executor.h:31
grpc_core::Executor::max_threads_
size_t max_threads_
Definition: executor.h:112
grpc_core::Executor
Definition: executor.h:55
grpc_core::Executor::SetThreading
void SetThreading(bool threading)
Definition: executor.cc:148
gpr_spinlock
Definition: src/core/lib/gpr/spinlock.h:29
grpc_core::Executor::IsThreadedDefault
static bool IsThreadedDefault()
Definition: executor.cc:442
grpc_core
Definition: call_metric_recorder.h:31
gpr_cv
pthread_cond_t gpr_cv
Definition: impl/codegen/sync_posix.h:48
error
grpc_error_handle error
Definition: retry_filter.cc:499
closure.h
grpc_core::Executor::Executor
Executor(const char *executor_name)
Definition: executor.cc:91
grpc_core::ThreadState
Definition: executor.h:30
grpc_core::Executor::ShutdownAll
static void ShutdownAll()
Definition: executor.cc:404
grpc_core::Executor::SetThreadingAll
static void SetThreadingAll(bool enable)
Definition: executor.cc:446
grpc_core::Executor::Run
static void Run(grpc_closure *closure, grpc_error_handle error, ExecutorType executor_type=ExecutorType::DEFAULT, ExecutorJobType job_type=ExecutorJobType::SHORT)
Definition: executor.cc:398
grpc_core::Executor::num_threads_
gpr_atm num_threads_
Definition: executor.h:113
grpc_core::ExecutorJobType::NUM_JOB_TYPES
@ NUM_JOB_TYPES
grpc_core::Executor::RunClosures
static size_t RunClosures(const char *executor_name, grpc_closure_list list)
Definition: executor.cc:99
grpc_core::ThreadState::name
const char * name
Definition: executor.h:33
grpc_core::ThreadState::shutdown
bool shutdown
Definition: executor.h:37
grpc_core::ExecutorJobType::SHORT
@ SHORT
grpc_core::Executor::IsThreaded
bool IsThreaded() const
Definition: executor.cc:144
grpc_core::ThreadState::queued_long_job
bool queued_long_job
Definition: executor.h:38
grpc_core::ThreadState::elems
grpc_closure_list elems
Definition: executor.h:35
grpc_core::Executor::SetThreadingDefault
static void SetThreadingDefault(bool enable)
Definition: executor.cc:454
arg
Definition: cmdline.cc:40
grpc_core::ExecutorJobType::LONG
@ LONG
grpc_core::Executor::thd_state_
ThreadState * thd_state_
Definition: executor.h:111
grpc_core::ExecutorType::DEFAULT
@ DEFAULT
grpc_closure_list
Definition: closure.h:41
grpc_core::Executor::InitAll
static void InitAll()
Definition: executor.cc:377
gpr_atm
intptr_t gpr_atm
Definition: impl/codegen/atm_gcc_atomic.h:32
grpc_core::Executor::name_
const char * name_
Definition: executor.h:110
gpr_mu
pthread_mutex_t gpr_mu
Definition: impl/codegen/sync_posix.h:47
grpc_core::Executor::Enqueue
void Enqueue(grpc_closure *closure, grpc_error_handle error, bool is_short)
Definition: executor.cc:260
grpc_core::ExecutorType
ExecutorType
Definition: executor.h:42
thd.h
grpc_core::Executor::Init
void Init()
Definition: executor.cc:97
closure
Definition: proxy.cc:59
grpc_core::Thread
Definition: thd.h:43
grpc_core::Executor::ThreadMain
static void ThreadMain(void *arg)
Definition: executor.cc:222
spinlock.h
grpc_core::ExecutorType::RESOLVER
@ RESOLVER
grpc_core::ThreadState::id
size_t id
Definition: executor.h:32
grpc_core::Executor::Shutdown
void Shutdown()
Definition: executor.cc:220
grpc_core::ThreadState::cv
gpr_cv cv
Definition: executor.h:34
grpc_error
Definition: error_internal.h:42
grpc_core::ExecutorType::NUM_EXECUTORS
@ NUM_EXECUTORS
grpc_core::ThreadState::thd
Thread thd
Definition: executor.h:39
grpc_core::ThreadState::depth
size_t depth
Definition: executor.h:36
grpc_core::Executor::adding_thread_lock_
gpr_spinlock adding_thread_lock_
Definition: executor.h:114
grpc_closure
Definition: closure.h:56
grpc_core::ExecutorJobType
ExecutorJobType
Definition: executor.h:49
grpc_core::grpc_executor_global_init
void grpc_executor_global_init()
Definition: executor.cc:459
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:16