exec_ctx.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_EXEC_CTX_H
20 #define GRPC_CORE_LIB_IOMGR_EXEC_CTX_H
21 
23 
24 #include <limits>
25 
28 #include <grpc/support/atm.h>
29 #include <grpc/support/cpu.h>
30 #include <grpc/support/log.h>
31 
33 #include "src/core/lib/gpr/tls.h"
38 
42 
43 /* This exec_ctx is ready to return: either pre-populated, or cached as soon as
44  the finish_check returns true */
45 #define GRPC_EXEC_CTX_FLAG_IS_FINISHED 1
46 /* The exec_ctx's thread is (potentially) owned by a call or channel: care
47  should be given to not delete said call/channel from this exec_ctx */
48 #define GRPC_EXEC_CTX_FLAG_THREAD_RESOURCE_LOOP 2
49 /* This exec ctx was initialized by an internal thread, and should not
50  be counted by fork handlers */
51 #define GRPC_EXEC_CTX_FLAG_IS_INTERNAL_THREAD 4
52 
53 /* This application callback exec ctx was initialized by an internal thread, and
54  should not be counted by fork handlers */
55 #define GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD 1
56 
57 namespace grpc_core {
58 class Combiner;
97 class ExecCtx {
98  public:
103  Set(this);
104  }
105 
107  explicit ExecCtx(uintptr_t fl) : flags_(fl) {
110  }
111  Set(this);
112  }
113 
115  virtual ~ExecCtx() {
117  Flush();
121  }
122  }
123 
125  ExecCtx(const ExecCtx&) = delete;
126  ExecCtx& operator=(const ExecCtx&) = delete;
127 
128  unsigned starting_cpu() {
131  }
132  return starting_cpu_;
133  }
134 
135  struct CombinerData {
136  /* currently active combiner: updated only via combiner.c */
138  /* last active combiner in the active combiner list */
140  };
141 
144 
147 
149  uintptr_t flags() { return flags_; }
150 
152  bool HasWork() {
153  return combiner_data_.active_combiner != nullptr ||
155  }
156 
161  bool Flush();
162 
168  if ((flags_ & GRPC_EXEC_CTX_FLAG_IS_FINISHED) == 0) {
169  if (CheckReadyToFinish()) {
171  return true;
172  }
173  return false;
174  } else {
175  return true;
176  }
177  }
178 
183  Timestamp Now();
184 
188  void InvalidateNow() { now_is_valid_ = false; }
189 
193  now_is_valid_ = true;
194  }
195 
199  void TestOnlySetNow(Timestamp new_val) {
200  now_ = new_val;
201  now_is_valid_ = true;
202  }
203 
205  static ExecCtx* Get() { return exec_ctx_; }
206 
207  static void Run(const DebugLocation& location, grpc_closure* closure,
209 
210  static void RunList(const DebugLocation& location, grpc_closure_list* list);
211 
212  protected:
214  virtual bool CheckReadyToFinish() { return false; }
215 
217  static void operator delete(void* /* p */) { abort(); }
218 
219  private:
221  static void Set(ExecCtx* exec_ctx) { exec_ctx_ = exec_ctx; }
222 
224  CombinerData combiner_data_ = {nullptr, nullptr};
226 
228 
229  bool now_is_valid_ = false;
231 
232  static GPR_THREAD_LOCAL(ExecCtx*) exec_ctx_;
234 };
235 
284  public:
287 
290  Set(this, flags_);
291  }
292 
294  if (Get() == this) {
295  while (head_ != nullptr) {
296  auto* f = head_;
297  head_ = f->internal_next;
298  if (f->internal_next == nullptr) {
299  tail_ = nullptr;
300  }
301  (*f->functor_run)(f, f->internal_success);
302  }
303  callback_exec_ctx_ = nullptr;
306  }
307  } else {
308  GPR_DEBUG_ASSERT(head_ == nullptr);
309  GPR_DEBUG_ASSERT(tail_ == nullptr);
310  }
311  }
312 
313  uintptr_t Flags() { return flags_; }
314 
315  static ApplicationCallbackExecCtx* Get() { return callback_exec_ctx_; }
316 
318  if (Get() == nullptr) {
321  }
322  callback_exec_ctx_ = exec_ctx;
323  }
324  }
325 
326  static void Enqueue(grpc_completion_queue_functor* functor, int is_success) {
327  functor->internal_success = is_success;
328  functor->internal_next = nullptr;
329 
331 
332  if (ctx->head_ == nullptr) {
333  ctx->head_ = functor;
334  }
335  if (ctx->tail_ != nullptr) {
336  ctx->tail_->internal_next = functor;
337  }
338  ctx->tail_ = functor;
339  }
340 
342  static void GlobalInit(void) {}
343 
345  static void GlobalShutdown(void) {}
346 
347  static bool Available() { return Get() != nullptr; }
348 
349  private:
353  static GPR_THREAD_LOCAL(ApplicationCallbackExecCtx*) callback_exec_ctx_;
354 };
355 
356 } // namespace grpc_core
357 
358 #endif /* GRPC_CORE_LIB_IOMGR_EXEC_CTX_H */
grpc_core::ApplicationCallbackExecCtx::GPR_THREAD_LOCAL
static GPR_THREAD_LOCAL(ApplicationCallbackExecCtx *) callback_exec_ctx_
fork.h
GRPC_EXEC_CTX_FLAG_IS_FINISHED
#define GRPC_EXEC_CTX_FLAG_IS_FINISHED
Definition: exec_ctx.h:45
gpr_cpu_current_cpu
GPRAPI unsigned gpr_cpu_current_cpu(void)
grpc_core::ApplicationCallbackExecCtx::Enqueue
static void Enqueue(grpc_completion_queue_functor *functor, int is_success)
Definition: exec_ctx.h:326
grpc_core::ExecCtx::closure_list_
grpc_closure_list closure_list_
Definition: exec_ctx.h:223
log.h
ctx
Definition: benchmark-async.c:30
grpc_core::ExecCtx::combiner_data
CombinerData * combiner_data()
Definition: exec_ctx.h:143
grpc_core::Fork::DecExecCtxCount
static void DecExecCtxCount()
Definition: src/core/lib/gprpp/fork.h:57
grpc_core::ApplicationCallbackExecCtx::tail_
grpc_completion_queue_functor * tail_
Definition: exec_ctx.h:352
grpc_core::DebugLocation
Definition: debug_location.h:31
grpc_core::ExecCtx::operator=
ExecCtx & operator=(const ExecCtx &)=delete
grpc_core::ExecCtx::starting_cpu_
unsigned starting_cpu_
Definition: exec_ctx.h:227
grpc_core::ExecCtx::combiner_data_
CombinerData combiner_data_
Definition: exec_ctx.h:224
grpc_core::ExecCtx::RunList
static void RunList(const DebugLocation &location, grpc_closure_list *list)
Definition: exec_ctx.cc:129
grpc_core::ExecCtx::~ExecCtx
virtual ~ExecCtx()
Definition: exec_ctx.h:115
GPR_DEBUG_ASSERT
#define GPR_DEBUG_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:103
grpc_core::ApplicationCallbackExecCtx::Get
static ApplicationCallbackExecCtx * Get()
Definition: exec_ctx.h:315
grpc_completion_queue_functor::internal_success
int internal_success
Definition: grpc_types.h:785
grpc_core
Definition: call_metric_recorder.h:31
GRPC_CLOSURE_LIST_INIT
#define GRPC_CLOSURE_LIST_INIT
Definition: closure.h:167
grpc_core::ExecCtx::GPR_THREAD_LOCAL
static GPR_THREAD_LOCAL(ExecCtx *) exec_ctx_
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
grpc_combiner
struct grpc_combiner grpc_combiner
Definition: exec_ctx.h:41
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::ExecCtx::CombinerData::last_combiner
Combiner * last_combiner
Definition: exec_ctx.h:139
grpc_core::ExecCtx::CheckReadyToFinish
virtual bool CheckReadyToFinish()
Definition: exec_ctx.h:214
grpc_core::ApplicationCallbackExecCtx::ApplicationCallbackExecCtx
ApplicationCallbackExecCtx(uintptr_t fl)
Definition: exec_ctx.h:289
closure.h
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
grpc_core::ApplicationCallbackExecCtx::Set
static void Set(ApplicationCallbackExecCtx *exec_ctx, uintptr_t flags)
Definition: exec_ctx.h:317
grpc_core::ApplicationCallbackExecCtx
Definition: exec_ctx.h:283
grpc_core::ExecCtx::last_exec_ctx_
ExecCtx * last_exec_ctx_
Definition: exec_ctx.h:233
grpc_core::ExecCtx::HasWork
bool HasWork()
Definition: exec_ctx.h:152
grpc_types.h
grpc_core::ExecCtx::closure_list
grpc_closure_list * closure_list()
Definition: exec_ctx.h:146
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
grpc_core::ApplicationCallbackExecCtx::Flags
uintptr_t Flags()
Definition: exec_ctx.h:313
grpc_core::ExecCtx::IsReadyToFinish
bool IsReadyToFinish()
Definition: exec_ctx.h:167
grpc_core::ExecCtx::CombinerData
Definition: exec_ctx.h:135
cpu.h
grpc_completion_queue_functor::internal_next
struct grpc_completion_queue_functor * internal_next
Definition: grpc_types.h:786
time.h
grpc_core::ApplicationCallbackExecCtx::GlobalInit
static void GlobalInit(void)
Definition: exec_ctx.h:342
grpc_core::ExecCtx::flags
uintptr_t flags()
Definition: exec_ctx.h:149
grpc_core::ExecCtx::ExecCtx
ExecCtx(uintptr_t fl)
Definition: exec_ctx.h:107
grpc_core::ApplicationCallbackExecCtx::flags_
uintptr_t flags_
Definition: exec_ctx.h:350
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
grpc_core::ExecCtx
Definition: exec_ctx.h:97
grpc_core::ExecCtx::Set
static void Set(ExecCtx *exec_ctx)
Definition: exec_ctx.h:221
gpr_types.h
grpc_core::ExecCtx::now_is_valid_
bool now_is_valid_
Definition: exec_ctx.h:229
grpc_closure_list
Definition: closure.h:41
grpc_core::ApplicationCallbackExecCtx::Available
static bool Available()
Definition: exec_ctx.h:347
grpc_core::ExecCtx::now_
Timestamp now_
Definition: exec_ctx.h:230
grpc_core::ExecCtx::TestOnlySetNow
void TestOnlySetNow(Timestamp new_val)
Definition: exec_ctx.h:199
grpc_core::ExecCtx::ExecCtx
ExecCtx()
Definition: exec_ctx.h:101
GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD
#define GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD
Definition: exec_ctx.h:55
debug_location.h
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
grpc_core::ExecCtx::SetNowIomgrShutdown
void SetNowIomgrShutdown()
Definition: exec_ctx.h:191
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
grpc_core::ApplicationCallbackExecCtx::GlobalShutdown
static void GlobalShutdown(void)
Definition: exec_ctx.h:345
grpc_core::ExecCtx::starting_cpu
unsigned starting_cpu()
Definition: exec_ctx.h:128
grpc_completion_queue_functor
Definition: grpc_types.h:773
grpc_core::Fork::IncExecCtxCount
static void IncExecCtxCount()
Definition: src/core/lib/gprpp/fork.h:50
closure
Definition: proxy.cc:59
tls.h
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
grpc_core::Combiner
Definition: combiner.h:34
grpc_core::ExecCtx::CombinerData::active_combiner
Combiner * active_combiner
Definition: exec_ctx.h:137
grpc_core::ApplicationCallbackExecCtx::ApplicationCallbackExecCtx
ApplicationCallbackExecCtx()
Definition: exec_ctx.h:286
grpc_core::Timestamp::InfFuture
static constexpr Timestamp InfFuture()
Definition: src/core/lib/gprpp/time.h:79
atm.h
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_core::ApplicationCallbackExecCtx::head_
grpc_completion_queue_functor * head_
Definition: exec_ctx.h:351
grpc_core::ExecCtx::flags_
uintptr_t flags_
Definition: exec_ctx.h:225
grpc_error
Definition: error_internal.h:42
GRPC_EXEC_CTX_FLAG_IS_INTERNAL_THREAD
#define GRPC_EXEC_CTX_FLAG_IS_INTERNAL_THREAD
Definition: exec_ctx.h:51
grpc_closure_list_empty
bool grpc_closure_list_empty(grpc_closure_list closure_list)
Definition: closure.h:243
grpc_core::ApplicationCallbackExecCtx::~ApplicationCallbackExecCtx
~ApplicationCallbackExecCtx()
Definition: exec_ctx.h:293
grpc_closure
Definition: closure.h:56
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
grpc_core::ExecCtx::InvalidateNow
void InvalidateNow()
Definition: exec_ctx.h:188
time_precise.h
port_platform.h


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