exec_ctx.cc
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 
20 
22 
23 #include <grpc/support/log.h>
24 #include <grpc/support/sync.h>
25 
29 
31 #ifndef NDEBUG
32  closure->scheduled = false;
34  gpr_log(GPR_DEBUG, "running closure %p: created [%s:%d]: %s [%s:%d]",
35  closure, closure->file_created, closure->line_created,
36  closure->run ? "run" : "scheduled", closure->file_initiated,
37  closure->line_initiated);
38  }
39 #endif
40 #ifdef GRPC_ERROR_IS_ABSEIL_STATUS
43  closure->error_data.error = 0;
44  closure->cb(closure->cb_arg, std::move(error));
45 #else
47  reinterpret_cast<grpc_error_handle>(closure->error_data.error);
48  closure->error_data.error = 0;
49  closure->cb(closure->cb_arg, error);
51 #endif
52 #ifndef NDEBUG
54  gpr_log(GPR_DEBUG, "closure %p finished", closure);
55  }
56 #endif
57 }
58 
61 }
62 
63 namespace grpc_core {
64 
65 GPR_THREAD_LOCAL(ExecCtx*) ExecCtx::exec_ctx_;
66 GPR_THREAD_LOCAL(ApplicationCallbackExecCtx*)
67 ApplicationCallbackExecCtx::callback_exec_ctx_;
68 
70  bool did_something = false;
71  GPR_TIMER_SCOPE("grpc_exec_ctx_flush", 0);
72  for (;;) {
76  while (c != nullptr) {
77  grpc_closure* next = c->next_data.next;
78  did_something = true;
79  exec_ctx_run(c);
80  c = next;
81  }
82  } else if (!grpc_combiner_continue_exec_ctx()) {
83  break;
84  }
85  }
87  return did_something;
88 }
89 
91  if (!now_is_valid_) {
93  now_is_valid_ = true;
94  }
95  return now_;
96 }
97 
100  (void)location;
101  if (closure == nullptr) {
103  return;
104  }
105 #ifndef NDEBUG
106  if (closure->scheduled) {
108  "Closure already scheduled. (closure: %p, created: [%s:%d], "
109  "previously scheduled at: [%s: %d], newly scheduled at [%s: %d]",
110  closure, closure->file_created, closure->line_created,
111  closure->file_initiated, closure->line_initiated, location.file(),
112  location.line());
113  abort();
114  }
115  closure->scheduled = true;
116  closure->file_initiated = location.file();
117  closure->line_initiated = location.line();
118  closure->run = false;
119  GPR_ASSERT(closure->cb != nullptr);
120 #endif
121 #ifdef GRPC_ERROR_IS_ABSEIL_STATUS
122  closure->error_data.error = internal::StatusAllocHeapPtr(error);
123 #else
124  closure->error_data.error = reinterpret_cast<intptr_t>(error);
125 #endif
127 }
128 
129 void ExecCtx::RunList(const DebugLocation& location, grpc_closure_list* list) {
130  (void)location;
131  grpc_closure* c = list->head;
132  while (c != nullptr) {
133  grpc_closure* next = c->next_data.next;
134 #ifndef NDEBUG
135  if (c->scheduled) {
137  "Closure already scheduled. (closure: %p, created: [%s:%d], "
138  "previously scheduled at: [%s: %d], newly scheduled at [%s:%d]",
139  c, c->file_created, c->line_created, c->file_initiated,
140  c->line_initiated, location.file(), location.line());
141  abort();
142  }
143  c->scheduled = true;
144  c->file_initiated = location.file();
145  c->line_initiated = location.line();
146  c->run = false;
147  GPR_ASSERT(c->cb != nullptr);
148 #endif
149  exec_ctx_sched(c);
150  c = next;
151  }
152  list->head = list->tail = nullptr;
153 }
154 
155 } // namespace grpc_core
grpc_closure_list::tail
grpc_closure * tail
Definition: closure.h:43
grpc_core::ExecCtx::closure_list_
grpc_closure_list closure_list_
Definition: exec_ctx.h:223
log.h
grpc_trace_closure
grpc_core::DebugOnlyTraceFlag grpc_trace_closure
grpc_core::DebugLocation
Definition: debug_location.h:31
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
timers.h
GPR_TIMER_SCOPE
#define GPR_TIMER_SCOPE(tag, important)
Definition: src/core/lib/profiling/timers.h:43
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::internal::StatusAllocHeapPtr
uintptr_t StatusAllocHeapPtr(absl::Status s)
Definition: status_helper.cc:421
grpc_core::Timestamp
Definition: src/core/lib/gprpp/time.h:62
error
grpc_error_handle error
Definition: retry_filter.cc:499
grpc_core::DebugLocation::file
const char * file() const
Definition: debug_location.h:34
grpc_closure_list_append
bool grpc_closure_list_append(grpc_closure_list *closure_list, grpc_closure *closure)
Definition: closure.h:176
exec_ctx_sched
static void exec_ctx_sched(grpc_closure *closure)
Definition: exec_ctx.cc:59
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
grpc_core::DebugLocation::line
int line() const
Definition: debug_location.h:35
grpc_closure_list::head
grpc_closure * head
Definition: closure.h:42
grpc_core::ExecCtx::Flush
bool Flush()
Definition: exec_ctx.cc:69
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
grpc_combiner_continue_exec_ctx
bool grpc_combiner_continue_exec_ctx()
Definition: combiner.cc:181
GPR_CLOCK_MONOTONIC
@ GPR_CLOCK_MONOTONIC
Definition: gpr_types.h:36
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
error.h
GPR_THREAD_LOCAL
#define GPR_THREAD_LOCAL(type)
Definition: tls.h:151
GPR_ERROR
#define GPR_ERROR
Definition: include/grpc/impl/codegen/log.h:57
gpr_now
GPRAPI gpr_timespec gpr_now(gpr_clock_type clock)
grpc_core::ExecCtx::now_is_valid_
bool now_is_valid_
Definition: exec_ctx.h:229
grpc_closure_list
Definition: closure.h:41
grpc_core::ExecCtx::now_
Timestamp now_
Definition: exec_ctx.h:230
grpc_core::TraceFlag::enabled
bool enabled()
Definition: debug/trace.h:82
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
grpc_core::Timestamp::FromTimespecRoundDown
static Timestamp FromTimespecRoundDown(gpr_timespec t)
Definition: src/core/lib/gprpp/time.cc:141
combiner.h
exec_ctx.h
closure
Definition: proxy.cc:59
GRPC_ERROR_UNREF
#define GRPC_ERROR_UNREF(err)
Definition: error.h:262
grpc_core::ExecCtx::Run
static void Run(const DebugLocation &location, grpc_closure *closure, grpc_error_handle error)
Definition: exec_ctx.cc:98
grpc_core::internal::StatusMoveFromHeapPtr
absl::Status StatusMoveFromHeapPtr(uintptr_t ptr)
Move the status from a heap ptr. (GetFrom & FreeHeap)
Definition: status_helper.cc:440
grpc_core::ExecCtx::CombinerData::active_combiner
Combiner * active_combiner
Definition: exec_ctx.h:137
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_error
Definition: error_internal.h:42
grpc_closure_list_empty
bool grpc_closure_list_empty(grpc_closure_list closure_list)
Definition: closure.h:243
sync.h
grpc_closure
Definition: closure.h:56
exec_ctx_run
static void exec_ctx_run(grpc_closure *closure)
Definition: exec_ctx.cc:30
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
port_platform.h


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