| 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 |
|
| 22 |
#include <grpc/support/port_platform.h> |
| 23 |
|
| 24 |
#include <limits> |
| 25 |
|
| 26 |
#include <grpc/impl/codegen/gpr_types.h> |
| 27 |
#include <grpc/impl/codegen/grpc_types.h> |
| 28 |
#include <grpc/support/atm.h> |
| 29 |
#include <grpc/support/cpu.h> |
| 30 |
#include <grpc/support/log.h> |
| 31 |
|
| 32 |
#include "src/core/lib/gpr/time_precise.h" |
| 33 |
#include "src/core/lib/gprpp/debug_location.h" |
| 34 |
#include "src/core/lib/gprpp/fork.h" |
| 35 |
#include "src/core/lib/gprpp/time.h" |
| 36 |
#include "src/core/lib/iomgr/closure.h" |
| 37 |
|
| 38 |
/** A combiner represents a list of work to be executed later. |
| 39 |
Forward declared here to avoid a circular dependency with combiner.h. */ |
| 40 |
typedef struct grpc_combiner grpc_combiner; |
| 41 |
|
| 42 |
/* This exec_ctx is ready to return: either pre-populated, or cached as soon as |
| 43 |
the finish_check returns true */ |
| 44 |
#define GRPC_EXEC_CTX_FLAG_IS_FINISHED 1 |
| 45 |
/* The exec_ctx's thread is (potentially) owned by a call or channel: care |
| 46 |
should be given to not delete said call/channel from this exec_ctx */ |
| 47 |
#define GRPC_EXEC_CTX_FLAG_THREAD_RESOURCE_LOOP 2 |
| 48 |
/* This exec ctx was initialized by an internal thread, and should not |
| 49 |
be counted by fork handlers */ |
| 50 |
#define GRPC_EXEC_CTX_FLAG_IS_INTERNAL_THREAD 4 |
| 51 |
|
| 52 |
/* This application callback exec ctx was initialized by an internal thread, and |
| 53 |
should not be counted by fork handlers */ |
| 54 |
#define GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD 1 |
| 55 |
|
| 56 |
namespace grpc_core { |
| 57 |
class Combiner; |
| 58 |
/** Execution context. |
| 59 |
* A bag of data that collects information along a callstack. |
| 60 |
* It is created on the stack at core entry points (public API or iomgr), and |
| 61 |
* stored internally as a thread-local variable. |
| 62 |
* |
| 63 |
* Generally, to create an exec_ctx instance, add the following line at the top |
| 64 |
* of the public API entry point or at the start of a thread's work function : |
| 65 |
* |
| 66 |
* ExecCtx exec_ctx; |
| 67 |
* |
| 68 |
* Access the created ExecCtx instance using : |
| 69 |
* ExecCtx::Get() |
| 70 |
* |
| 71 |
* Specific responsibilities (this may grow in the future): |
| 72 |
* - track a list of core work that needs to be delayed until the base of the |
| 73 |
* call stack (this provides a convenient mechanism to run callbacks |
| 74 |
* without worrying about locking issues) |
| 75 |
* - provide a decision maker (via IsReadyToFinish) that provides a |
| 76 |
* signal as to whether a borrowed thread should continue to do work or |
| 77 |
* should actively try to finish up and get this thread back to its owner |
| 78 |
* |
| 79 |
* CONVENTIONS: |
| 80 |
* - Instance of this must ALWAYS be constructed on the stack, never |
| 81 |
* heap allocated. |
| 82 |
* - Do not pass exec_ctx as a parameter to a function. Always access it using |
| 83 |
* ExecCtx::Get(). |
| 84 |
* - NOTE: In the future, the convention is likely to change to allow only one |
| 85 |
* ExecCtx on a thread's stack at the same time. The TODO below |
| 86 |
* discusses this plan in more detail. |
| 87 |
* |
| 88 |
* TODO(yashykt): Only allow one "active" ExecCtx on a thread at the same time. |
| 89 |
* Stage 1: If a new one is created on the stack, it should just |
| 90 |
* pass-through to the underlying ExecCtx deeper in the thread's |
| 91 |
* stack. |
| 92 |
* Stage 2: Assert if a 2nd one is ever created on the stack |
| 93 |
* since that implies a core re-entry outside of application |
| 94 |
* callbacks. |
| 95 |
*/ |
| 96 |
class ExecCtx { |
| 97 |
public: |
| 98 |
/** Default Constructor */ |
| 99 |
|
| 100 |
ExecCtx() : flags_(GRPC_EXEC_CTX_FLAG_IS_FINISHED) { |
| 101 |
Fork::IncExecCtxCount(); |
| 102 |
Set(this); |
| 103 |
} |
| 104 |
|
| 105 |
/** Parameterised Constructor */ |
| 106 |
explicit ExecCtx(uintptr_t fl) : flags_(fl) { |
| 107 |
if (!(GRPC_EXEC_CTX_FLAG_IS_INTERNAL_THREAD & flags_)) { |
| 108 |
Fork::IncExecCtxCount(); |
| 109 |
} |
| 110 |
Set(this); |
| 111 |
} |
| 112 |
|
| 113 |
/** Destructor */ |
| 114 |
virtual ~ExecCtx() { |
| 115 |
flags_ |= GRPC_EXEC_CTX_FLAG_IS_FINISHED; |
| 116 |
Flush(); |
| 117 |
Set(last_exec_ctx_); |
| 118 |
if (!(GRPC_EXEC_CTX_FLAG_IS_INTERNAL_THREAD & flags_)) { |
| 119 |
Fork::DecExecCtxCount(); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** Disallow copy and assignment operators */ |
| 124 |
ExecCtx(const ExecCtx&) = delete; |
| 125 |
ExecCtx& operator=(const ExecCtx&) = delete; |
| 126 |
|
| 127 |
unsigned starting_cpu() { |
| 128 |
if (starting_cpu_ == std::numeric_limits<unsigned>::max()) { |
| 129 |
starting_cpu_ = gpr_cpu_current_cpu(); |
| 130 |
} |
| 131 |
return starting_cpu_; |
| 132 |
} |
| 133 |
|
| 134 |
struct CombinerData { |
| 135 |
/* currently active combiner: updated only via combiner.c */ |
| 136 |
Combiner* active_combiner; |
| 137 |
/* last active combiner in the active combiner list */ |
| 138 |
Combiner* last_combiner; |
| 139 |
}; |
| 140 |
|
| 141 |
/** Only to be used by grpc-combiner code */ |
| 142 |
CombinerData* combiner_data() { return &combiner_data_; } |
| 143 |
|
| 144 |
/** Return pointer to grpc_closure_list */ |
| 145 |
grpc_closure_list* closure_list() { return &closure_list_; } |
| 146 |
|
| 147 |
/** Return flags */ |
| 148 |
uintptr_t flags() { return flags_; } |
| 149 |
|
| 150 |
/** Checks if there is work to be done */ |
| 151 |
bool HasWork() { |
| 152 |
return combiner_data_.active_combiner != nullptr || |
| 153 |
!grpc_closure_list_empty(closure_list_); |
| 154 |
} |
| 155 |
|
| 156 |
/** Flush any work that has been enqueued onto this grpc_exec_ctx. |
| 157 |
* Caller must guarantee that no interfering locks are held. |
| 158 |
* Returns true if work was performed, false otherwise. |
| 159 |
*/ |
| 160 |
bool Flush(); |
| 161 |
|
| 162 |
/** Returns true if we'd like to leave this execution context as soon as |
| 163 |
* possible: useful for deciding whether to do something more or not |
| 164 |
* depending on outside context. |
| 165 |
*/ |
| 166 |
bool IsReadyToFinish() { |
| 167 |
if ((flags_ & GRPC_EXEC_CTX_FLAG_IS_FINISHED) == 0) { |
| 168 |
if (CheckReadyToFinish()) { |
| 169 |
flags_ |= GRPC_EXEC_CTX_FLAG_IS_FINISHED; |
| 170 |
return true; |
| 171 |
} |
| 172 |
return false; |
| 173 |
} else { |
| 174 |
return true; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
Timestamp Now() { return Timestamp::Now(); } |
| 179 |
void InvalidateNow() { time_cache_.InvalidateCache(); } |
| 180 |
void SetNowIomgrShutdown() { |
| 181 |
// We get to do a test only set now on this path just because iomgr |
| 182 |
// is getting removed and no point adding more interfaces for it. |
| 183 |
time_cache_.TestOnlySetNow(Timestamp::InfFuture()); |
| 184 |
} |
| 185 |
void TestOnlySetNow(Timestamp now) { time_cache_.TestOnlySetNow(now); } |
| 186 |
|
| 187 |
/** Gets pointer to current exec_ctx. */ |
| 188 |
static ExecCtx* Get() { return exec_ctx_; } |
| 189 |
|
| 190 |
static void Run(const DebugLocation& location, grpc_closure* closure, |
| 191 |
grpc_error_handle error); |
| 192 |
|
| 193 |
static void RunList(const DebugLocation& location, grpc_closure_list* list); |
| 194 |
|
| 195 |
protected: |
| 196 |
/** Check if ready to finish. */ |
| 197 |
virtual bool CheckReadyToFinish() { return false; } |
| 198 |
|
| 199 |
/** Disallow delete on ExecCtx. */ |
| 200 |
static void operator delete(void* /* p */) { abort(); } |
| 201 |
|
| 202 |
private: |
| 203 |
/** Set exec_ctx_ to exec_ctx. */ |
| 204 |
static void Set(ExecCtx* exec_ctx) { exec_ctx_ = exec_ctx; } |
| 205 |
|
| 206 |
grpc_closure_list closure_list_ = GRPC_CLOSURE_LIST_INIT; |
| 207 |
CombinerData combiner_data_ = {nullptr, nullptr}; |
| 208 |
uintptr_t flags_; |
| 209 |
|
| 210 |
unsigned starting_cpu_ = std::numeric_limits<unsigned>::max(); |
| 211 |
|
| 212 |
ScopedTimeCache time_cache_; |
| 213 |
static thread_local ExecCtx* exec_ctx_; |
| 214 |
ExecCtx* last_exec_ctx_ = Get(); |
| 215 |
}; |
| 216 |
|
| 217 |
/** Application-callback execution context. |
| 218 |
* A bag of data that collects information along a callstack. |
| 219 |
* It is created on the stack at core entry points, and stored internally |
| 220 |
* as a thread-local variable. |
| 221 |
* |
| 222 |
* There are three key differences between this structure and ExecCtx: |
| 223 |
* 1. ApplicationCallbackExecCtx builds a list of application-level |
| 224 |
* callbacks, but ExecCtx builds a list of internal callbacks to invoke. |
| 225 |
* 2. ApplicationCallbackExecCtx invokes its callbacks only at destruction; |
| 226 |
* there is no explicit Flush method. |
| 227 |
* 3. If more than one ApplicationCallbackExecCtx is created on the thread's |
| 228 |
* stack, only the one closest to the base of the stack is actually |
| 229 |
* active and this is the only one that enqueues application callbacks. |
| 230 |
* (Unlike ExecCtx, it is not feasible to prevent multiple of these on the |
| 231 |
* stack since the executing application callback may itself enter core. |
| 232 |
* However, the new one created will just pass callbacks through to the |
| 233 |
* base one and those will not be executed until the return to the |
| 234 |
* destructor of the base one, preventing unlimited stack growth.) |
| 235 |
* |
| 236 |
* This structure exists because application callbacks may themselves cause a |
| 237 |
* core re-entry (e.g., through a public API call) and if that call in turn |
| 238 |
* causes another application-callback, there could be arbitrarily growing |
| 239 |
* stacks of core re-entries. Instead, any application callbacks instead should |
| 240 |
* not be invoked until other core work is done and other application callbacks |
| 241 |
* have completed. To accomplish this, any application callback should be |
| 242 |
* enqueued using ApplicationCallbackExecCtx::Enqueue . |
| 243 |
* |
| 244 |
* CONVENTIONS: |
| 245 |
* - Instances of this must ALWAYS be constructed on the stack, never |
| 246 |
* heap allocated. |
| 247 |
* - Instances of this are generally constructed before ExecCtx when needed. |
| 248 |
* The only exception is for ExecCtx's that are explicitly flushed and |
| 249 |
* that survive beyond the scope of the function that can cause application |
| 250 |
* callbacks to be invoked (e.g., in the timer thread). |
| 251 |
* |
| 252 |
* Generally, core entry points that may trigger application-level callbacks |
| 253 |
* will have the following declarations: |
| 254 |
* |
| 255 |
* ApplicationCallbackExecCtx callback_exec_ctx; |
| 256 |
* ExecCtx exec_ctx; |
| 257 |
* |
| 258 |
* This ordering is important to make sure that the ApplicationCallbackExecCtx |
| 259 |
* is destroyed after the ExecCtx (to prevent the re-entry problem described |
| 260 |
* above, as well as making sure that ExecCtx core callbacks are invoked first) |
| 261 |
* |
| 262 |
*/ |
| 263 |
|
| 264 |
class ApplicationCallbackExecCtx { |
| 265 |
public: |
| 266 |
/** Default Constructor */ |
| 267 |
ApplicationCallbackExecCtx() { Set(this, flags_); } |
| 268 |
|
| 269 |
/** Parameterised Constructor */ |
| 270 |
explicit ApplicationCallbackExecCtx(uintptr_t fl) : flags_(fl) { |
| 271 |
Set(this, flags_); |
| 272 |
} |
| 273 |
|
| 274 |
~ApplicationCallbackExecCtx() { |
| 275 |
if (Get() == this) { |
| 276 |
while (head_ != nullptr) { |
| 277 |
auto* f = head_; |
| 278 |
head_ = f->internal_next; |
| 279 |
if (f->internal_next == nullptr) { |
| 280 |
tail_ = nullptr; |
| 281 |
} |
| 282 |
(*f->functor_run)(f, f->internal_success); |
| 283 |
} |
| 284 |
callback_exec_ctx_ = nullptr; |
| 285 |
if (!(GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD & flags_)) { |
| 286 |
Fork::DecExecCtxCount(); |
| 287 |
} |
| 288 |
} else { |
| 289 |
GPR_DEBUG_ASSERT(head_ == nullptr); |
| 290 |
GPR_DEBUG_ASSERT(tail_ == nullptr); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
uintptr_t Flags() { return flags_; } |
| 295 |
|
| 296 |
static ApplicationCallbackExecCtx* Get() { return callback_exec_ctx_; } |
| 297 |
|
| 298 |
static void Set(ApplicationCallbackExecCtx* exec_ctx, uintptr_t flags) { |
| 299 |
if (Get() == nullptr) { |
| 300 |
if (!(GRPC_APP_CALLBACK_EXEC_CTX_FLAG_IS_INTERNAL_THREAD & flags)) { |
| 301 |
Fork::IncExecCtxCount(); |
| 302 |
} |
| 303 |
callback_exec_ctx_ = exec_ctx; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
static void Enqueue(grpc_completion_queue_functor* functor, int is_success) { |
| 308 |
functor->internal_success = is_success; |
| 309 |
functor->internal_next = nullptr; |
| 310 |
|
| 311 |
ApplicationCallbackExecCtx* ctx = Get(); |
| 312 |
|
| 313 |
if (ctx->head_ == nullptr) { |
| 314 |
ctx->head_ = functor; |
| 315 |
} |
| 316 |
if (ctx->tail_ != nullptr) { |
| 317 |
ctx->tail_->internal_next = functor; |
| 318 |
} |
| 319 |
ctx->tail_ = functor; |
| 320 |
} |
| 321 |
|
| 322 |
static bool Available() { return Get() != nullptr; } |
| 323 |
|
| 324 |
private: |
| 325 |
uintptr_t flags_{0u}; |
| 326 |
grpc_completion_queue_functor* head_{nullptr}; |
| 327 |
grpc_completion_queue_functor* tail_{nullptr}; |
| 328 |
static thread_local ApplicationCallbackExecCtx* callback_exec_ctx_; |
| 329 |
}; |
| 330 |
|
| 331 |
} // namespace grpc_core |
| 332 |
|
| 333 |
#endif /* GRPC_CORE_LIB_IOMGR_EXEC_CTX_H */ |