| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2016 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 |
#include <grpc/support/port_platform.h> |
| 20 |
|
| 21 |
#include "src/core/lib/iomgr/combiner.h" |
| 22 |
|
| 23 |
#include <assert.h> |
| 24 |
#include <inttypes.h> |
| 25 |
#include <string.h> |
| 26 |
|
| 27 |
#include <grpc/support/alloc.h> |
| 28 |
#include <grpc/support/log.h> |
| 29 |
|
| 30 |
#include "src/core/lib/gprpp/mpscq.h" |
| 31 |
#include "src/core/lib/iomgr/executor.h" |
| 32 |
#include "src/core/lib/iomgr/iomgr_internal.h" |
| 33 |
|
| 34 |
grpc_core::DebugOnlyTraceFlag grpc_combiner_trace(false, "combiner"); |
| 35 |
|
| 36 |
#define GRPC_COMBINER_TRACE(fn) \ |
| 37 |
do { \ |
| 38 |
if (grpc_combiner_trace.enabled()) { \ |
| 39 |
fn; \ |
| 40 |
} \ |
| 41 |
} while (0) |
| 42 |
|
| 43 |
#define STATE_UNORPHANED 1 |
| 44 |
#define STATE_ELEM_COUNT_LOW_BIT 2 |
| 45 |
|
| 46 |
static void combiner_exec(grpc_core::Combiner* lock, grpc_closure* closure, |
| 47 |
grpc_error_handle error); |
| 48 |
static void combiner_finally_exec(grpc_core::Combiner* lock, |
| 49 |
grpc_closure* closure, |
| 50 |
grpc_error_handle error); |
| 51 |
|
| 52 |
static void offload(void* arg, grpc_error_handle error); |
| 53 |
|
| 54 |
grpc_core::Combiner* grpc_combiner_create(void) { |
| 55 |
grpc_core::Combiner* lock = new grpc_core::Combiner(); |
| 56 |
gpr_ref_init(&lock->refs, 1); |
| 57 |
gpr_atm_no_barrier_store(&lock->state, STATE_UNORPHANED); |
| 58 |
grpc_closure_list_init(&lock->final_list); |
| 59 |
GRPC_CLOSURE_INIT(&lock->offload, offload, lock, nullptr); |
| 60 |
GRPC_COMBINER_TRACE(gpr_log(GPR_INFO, "C:%p create", lock)); |
| 61 |
return lock; |
| 62 |
} |
| 63 |
|
| 64 |
static void really_destroy(grpc_core::Combiner* lock) { |
| 65 |
GRPC_COMBINER_TRACE(gpr_log(GPR_INFO, "C:%p really_destroy", lock)); |
| 66 |
GPR_ASSERT(gpr_atm_no_barrier_load(&lock->state) == 0); |
| 67 |
delete lock; |
| 68 |
} |
| 69 |
|
| 70 |
static void start_destroy(grpc_core::Combiner* lock) { |
| 71 |
gpr_atm old_state = gpr_atm_full_fetch_add(&lock->state, -STATE_UNORPHANED); |
| 72 |
GRPC_COMBINER_TRACE(gpr_log( |
| 73 |
GPR_INFO, "C:%p really_destroy old_state=%" PRIdPTR, lock, old_state)); |
| 74 |
if (old_state == 1) { |
| 75 |
really_destroy(lock); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
#ifndef NDEBUG |
| 80 |
#define GRPC_COMBINER_DEBUG_SPAM(op, delta) \ |
| 81 |
if (grpc_combiner_trace.enabled()) { \ |
| 82 |
gpr_log(file, line, GPR_LOG_SEVERITY_DEBUG, \ |
| 83 |
"C:%p %s %" PRIdPTR " --> %" PRIdPTR " %s", lock, (op), \ |
| 84 |
gpr_atm_no_barrier_load(&lock->refs.count), \ |
| 85 |
gpr_atm_no_barrier_load(&lock->refs.count) + (delta), reason); \ |
| 86 |
} |
| 87 |
#else |
| 88 |
#define GRPC_COMBINER_DEBUG_SPAM(op, delta) |
| 89 |
#endif |
| 90 |
|
| 91 |
void grpc_combiner_unref(grpc_core::Combiner* lock GRPC_COMBINER_DEBUG_ARGS) { |
| 92 |
GRPC_COMBINER_DEBUG_SPAM("UNREF", -1); |
| 93 |
if (gpr_unref(&lock->refs)) { |
| 94 |
start_destroy(lock); |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
grpc_core::Combiner* grpc_combiner_ref( |
| 99 |
grpc_core::Combiner* lock GRPC_COMBINER_DEBUG_ARGS) { |
| 100 |
GRPC_COMBINER_DEBUG_SPAM(" REF", 1); |
| 101 |
gpr_ref(&lock->refs); |
| 102 |
return lock; |
| 103 |
} |
| 104 |
|
| 105 |
static void push_last_on_exec_ctx(grpc_core::Combiner* lock) { |
| 106 |
lock->next_combiner_on_this_exec_ctx = nullptr; |
| 107 |
if (grpc_core::ExecCtx::Get()->combiner_data()->active_combiner == nullptr) { |
| 108 |
grpc_core::ExecCtx::Get()->combiner_data()->active_combiner = |
| 109 |
grpc_core::ExecCtx::Get()->combiner_data()->last_combiner = lock; |
| 110 |
} else { |
| 111 |
grpc_core::ExecCtx::Get() |
| 112 |
->combiner_data() |
| 113 |
->last_combiner->next_combiner_on_this_exec_ctx = lock; |
| 114 |
grpc_core::ExecCtx::Get()->combiner_data()->last_combiner = lock; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
static void push_first_on_exec_ctx(grpc_core::Combiner* lock) { |
| 119 |
lock->next_combiner_on_this_exec_ctx = |
| 120 |
grpc_core::ExecCtx::Get()->combiner_data()->active_combiner; |
| 121 |
grpc_core::ExecCtx::Get()->combiner_data()->active_combiner = lock; |
| 122 |
if (lock->next_combiner_on_this_exec_ctx == nullptr) { |
| 123 |
grpc_core::ExecCtx::Get()->combiner_data()->last_combiner = lock; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
static void combiner_exec(grpc_core::Combiner* lock, grpc_closure* cl, |
| 128 |
grpc_error_handle error) { |
| 129 |
gpr_atm last = gpr_atm_full_fetch_add(&lock->state, STATE_ELEM_COUNT_LOW_BIT); |
| 130 |
GRPC_COMBINER_TRACE(gpr_log(GPR_INFO, |
| 131 |
"C:%p grpc_combiner_execute c=%p last=%" PRIdPTR, |
| 132 |
lock, cl, last)); |
| 133 |
if (last == 1) { |
| 134 |
gpr_atm_no_barrier_store( |
| 135 |
&lock->initiating_exec_ctx_or_null, |
| 136 |
reinterpret_cast<gpr_atm>(grpc_core::ExecCtx::Get())); |
| 137 |
// first element on this list: add it to the list of combiner locks |
| 138 |
// executing within this exec_ctx |
| 139 |
push_last_on_exec_ctx(lock); |
| 140 |
} else { |
| 141 |
// there may be a race with setting here: if that happens, we may delay |
| 142 |
// offload for one or two actions, and that's fine |
| 143 |
gpr_atm initiator = |
| 144 |
gpr_atm_no_barrier_load(&lock->initiating_exec_ctx_or_null); |
| 145 |
if (initiator != 0 && |
| 146 |
initiator != reinterpret_cast<gpr_atm>(grpc_core::ExecCtx::Get())) { |
| 147 |
gpr_atm_no_barrier_store(&lock->initiating_exec_ctx_or_null, 0); |
| 148 |
} |
| 149 |
} |
| 150 |
GPR_ASSERT(last & STATE_UNORPHANED); // ensure lock has not been destroyed |
| 151 |
assert(cl->cb); |
| 152 |
cl->error_data.error = grpc_core::internal::StatusAllocHeapPtr(error); |
| 153 |
lock->queue.Push(cl->next_data.mpscq_node.get()); |
| 154 |
} |
| 155 |
|
| 156 |
static void move_next() { |
| 157 |
grpc_core::ExecCtx::Get()->combiner_data()->active_combiner = |
| 158 |
grpc_core::ExecCtx::Get() |
| 159 |
->combiner_data() |
| 160 |
->active_combiner->next_combiner_on_this_exec_ctx; |
| 161 |
if (grpc_core::ExecCtx::Get()->combiner_data()->active_combiner == nullptr) { |
| 162 |
grpc_core::ExecCtx::Get()->combiner_data()->last_combiner = nullptr; |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
static void offload(void* arg, grpc_error_handle /*error*/) { |
| 167 |
grpc_core::Combiner* lock = static_cast<grpc_core::Combiner*>(arg); |
| 168 |
push_last_on_exec_ctx(lock); |
| 169 |
} |
| 170 |
|
| 171 |
static void queue_offload(grpc_core::Combiner* lock) { |
| 172 |
move_next(); |
| 173 |
GRPC_COMBINER_TRACE(gpr_log(GPR_INFO, "C:%p queue_offload", lock)); |
| 174 |
grpc_core::Executor::Run(&lock->offload, absl::OkStatus()); |
| 175 |
} |
| 176 |
|
| 177 |
bool grpc_combiner_continue_exec_ctx() { |
| 178 |
grpc_core::Combiner* lock = |
| 179 |
grpc_core::ExecCtx::Get()->combiner_data()->active_combiner; |
| 180 |
if (lock == nullptr) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
|
| 184 |
bool contended = |
| 185 |
gpr_atm_no_barrier_load(&lock->initiating_exec_ctx_or_null) == 0; |
| 186 |
|
| 187 |
GRPC_COMBINER_TRACE(gpr_log(GPR_INFO, |
| 188 |
"C:%p grpc_combiner_continue_exec_ctx " |
| 189 |
"contended=%d " |
| 190 |
"exec_ctx_ready_to_finish=%d " |
| 191 |
"time_to_execute_final_list=%d", |
| 192 |
lock, contended, |
| 193 |
grpc_core::ExecCtx::Get()->IsReadyToFinish(), |
| 194 |
lock->time_to_execute_final_list)); |
| 195 |
|
| 196 |
// offload only if all the following conditions are true: |
| 197 |
// 1. the combiner is contended and has more than one closure to execute |
| 198 |
// 2. the current execution context needs to finish as soon as possible |
| 199 |
// 3. the current thread is not a worker for any background poller |
| 200 |
// 4. the DEFAULT executor is threaded |
| 201 |
if (contended && grpc_core::ExecCtx::Get()->IsReadyToFinish() && |
| 202 |
!grpc_iomgr_platform_is_any_background_poller_thread() && |
| 203 |
grpc_core::Executor::IsThreadedDefault()) { |
| 204 |
// this execution context wants to move on: schedule remaining work to be |
| 205 |
// picked up on the executor |
| 206 |
queue_offload(lock); |
| 207 |
return true; |
| 208 |
} |
| 209 |
|
| 210 |
if (!lock->time_to_execute_final_list || |
| 211 |
// peek to see if something new has shown up, and execute that with |
| 212 |
// priority |
| 213 |
(gpr_atm_acq_load(&lock->state) >> 1) > 1) { |
| 214 |
grpc_core::MultiProducerSingleConsumerQueue::Node* n = lock->queue.Pop(); |
| 215 |
GRPC_COMBINER_TRACE( |
| 216 |
gpr_log(GPR_INFO, "C:%p maybe_finish_one n=%p", lock, n)); |
| 217 |
if (n == nullptr) { |
| 218 |
// queue is in an inconsistent state: use this as a cue that we should |
| 219 |
// go off and do something else for a while (and come back later) |
| 220 |
queue_offload(lock); |
| 221 |
return true; |
| 222 |
} |
| 223 |
grpc_closure* cl = reinterpret_cast<grpc_closure*>(n); |
| 224 |
#ifndef NDEBUG |
| 225 |
cl->scheduled = false; |
| 226 |
#endif |
| 227 |
grpc_error_handle cl_err = |
| 228 |
grpc_core::internal::StatusMoveFromHeapPtr(cl->error_data.error); |
| 229 |
cl->error_data.error = 0; |
| 230 |
cl->cb(cl->cb_arg, std::move(cl_err)); |
| 231 |
} else { |
| 232 |
grpc_closure* c = lock->final_list.head; |
| 233 |
GPR_ASSERT(c != nullptr); |
| 234 |
grpc_closure_list_init(&lock->final_list); |
| 235 |
int loops = 0; |
| 236 |
while (c != nullptr) { |
| 237 |
GRPC_COMBINER_TRACE( |
| 238 |
gpr_log(GPR_INFO, "C:%p execute_final[%d] c=%p", lock, loops, c)); |
| 239 |
grpc_closure* next = c->next_data.next; |
| 240 |
#ifndef NDEBUG |
| 241 |
c->scheduled = false; |
| 242 |
#endif |
| 243 |
grpc_error_handle error = |
| 244 |
grpc_core::internal::StatusMoveFromHeapPtr(c->error_data.error); |
| 245 |
c->error_data.error = 0; |
| 246 |
c->cb(c->cb_arg, std::move(error)); |
| 247 |
c = next; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
move_next(); |
| 252 |
lock->time_to_execute_final_list = false; |
| 253 |
gpr_atm old_state = |
| 254 |
gpr_atm_full_fetch_add(&lock->state, -STATE_ELEM_COUNT_LOW_BIT); |
| 255 |
GRPC_COMBINER_TRACE( |
| 256 |
gpr_log(GPR_INFO, "C:%p finish old_state=%" PRIdPTR, lock, old_state)); |
| 257 |
// Define a macro to ease readability of the following switch statement. |
| 258 |
#define OLD_STATE_WAS(orphaned, elem_count) \ |
| 259 |
(((orphaned) ? 0 : STATE_UNORPHANED) | \ |
| 260 |
((elem_count)*STATE_ELEM_COUNT_LOW_BIT)) |
| 261 |
// Depending on what the previous state was, we need to perform different |
| 262 |
// actions. |
| 263 |
switch (old_state) { |
| 264 |
default: |
| 265 |
// we have multiple queued work items: just continue executing them |
| 266 |
break; |
| 267 |
case OLD_STATE_WAS(false, 2): |
| 268 |
case OLD_STATE_WAS(true, 2): |
| 269 |
// we're down to one queued item: if it's the final list we should do that |
| 270 |
if (!grpc_closure_list_empty(lock->final_list)) { |
| 271 |
lock->time_to_execute_final_list = true; |
| 272 |
} |
| 273 |
break; |
| 274 |
case OLD_STATE_WAS(false, 1): |
| 275 |
// had one count, one unorphaned --> unlocked unorphaned |
| 276 |
return true; |
| 277 |
case OLD_STATE_WAS(true, 1): |
| 278 |
// and one count, one orphaned --> unlocked and orphaned |
| 279 |
really_destroy(lock); |
| 280 |
return true; |
| 281 |
case OLD_STATE_WAS(false, 0): |
| 282 |
case OLD_STATE_WAS(true, 0): |
| 283 |
// these values are illegal - representing an already unlocked or |
| 284 |
// deleted lock |
| 285 |
GPR_UNREACHABLE_CODE(return true); |
| 286 |
} |
| 287 |
push_first_on_exec_ctx(lock); |
| 288 |
return true; |
| 289 |
} |
| 290 |
|
| 291 |
static void enqueue_finally(void* closure, grpc_error_handle error); |
| 292 |
|
| 293 |
static void combiner_finally_exec(grpc_core::Combiner* lock, |
| 294 |
grpc_closure* closure, |
| 295 |
grpc_error_handle error) { |
| 296 |
GPR_ASSERT(lock != nullptr); |
| 297 |
GRPC_COMBINER_TRACE(gpr_log( |
| 298 |
GPR_INFO, "C:%p grpc_combiner_execute_finally c=%p; ac=%p", lock, closure, |
| 299 |
grpc_core::ExecCtx::Get()->combiner_data()->active_combiner)); |
| 300 |
if (grpc_core::ExecCtx::Get()->combiner_data()->active_combiner != lock) { |
| 301 |
// Using error_data.scratch to store the combiner so that it can be accessed |
| 302 |
// in enqueue_finally. |
| 303 |
closure->error_data.scratch = reinterpret_cast<uintptr_t>(lock); |
| 304 |
lock->Run(GRPC_CLOSURE_CREATE(enqueue_finally, closure, nullptr), error); |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
if (grpc_closure_list_empty(lock->final_list)) { |
| 309 |
gpr_atm_full_fetch_add(&lock->state, STATE_ELEM_COUNT_LOW_BIT); |
| 310 |
} |
| 311 |
grpc_closure_list_append(&lock->final_list, closure, error); |
| 312 |
} |
| 313 |
|
| 314 |
static void enqueue_finally(void* closure, grpc_error_handle error) { |
| 315 |
grpc_closure* cl = static_cast<grpc_closure*>(closure); |
| 316 |
grpc_core::Combiner* lock = |
| 317 |
reinterpret_cast<grpc_core::Combiner*>(cl->error_data.scratch); |
| 318 |
cl->error_data.scratch = 0; |
| 319 |
combiner_finally_exec(lock, cl, error); |
| 320 |
} |
| 321 |
|
| 322 |
namespace grpc_core { |
| 323 |
void Combiner::Run(grpc_closure* closure, grpc_error_handle error) { |
| 324 |
combiner_exec(this, closure, error); |
| 325 |
} |
| 326 |
|
| 327 |
void Combiner::FinallyRun(grpc_closure* closure, grpc_error_handle error) { |
| 328 |
combiner_finally_exec(this, closure, error); |
| 329 |
} |
| 330 |
} // namespace grpc_core |