| 1 |
/* |
| 2 |
* Copyright 2015 gRPC authors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
* |
| 16 |
*/ |
| 17 |
|
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "absl/base/thread_annotations.h" |
| 21 |
|
| 22 |
#include <grpc/grpc.h> |
| 23 |
#include <grpc/impl/codegen/gpr_types.h> |
| 24 |
#include <grpc/impl/codegen/grpc_types.h> |
| 25 |
#include <grpc/support/cpu.h> |
| 26 |
#include <grpc/support/log.h> |
| 27 |
#include <grpc/support/sync.h> |
| 28 |
#include <grpc/support/time.h> |
| 29 |
#include <grpcpp/completion_queue.h> |
| 30 |
#include <grpcpp/impl/codegen/completion_queue_tag.h> |
| 31 |
#include <grpcpp/impl/grpc_library.h> |
| 32 |
|
| 33 |
#include "src/core/lib/gpr/useful.h" |
| 34 |
#include "src/core/lib/gprpp/sync.h" |
| 35 |
#include "src/core/lib/gprpp/thd.h" |
| 36 |
|
| 37 |
namespace grpc { |
| 38 |
namespace { |
| 39 |
|
| 40 |
internal::GrpcLibraryInitializer g_gli_initializer; |
| 41 |
|
| 42 |
gpr_once g_once_init_callback_alternative = GPR_ONCE_INIT; |
| 43 |
grpc_core::Mutex* g_callback_alternative_mu; |
| 44 |
|
| 45 |
// Implement a ref-counted callback CQ for global use in the alternative |
| 46 |
// implementation so that its threads are only created once. Do this using |
| 47 |
// explicit ref-counts and raw pointers rather than a shared-ptr since that |
| 48 |
// has a non-trivial destructor and thus can't be used for global variables. |
| 49 |
struct CallbackAlternativeCQ { |
| 50 |
int refs ABSL_GUARDED_BY(g_callback_alternative_mu) = 0; |
| 51 |
CompletionQueue* cq ABSL_GUARDED_BY(g_callback_alternative_mu); |
| 52 |
std::vector<grpc_core::Thread>* nexting_threads |
| 53 |
ABSL_GUARDED_BY(g_callback_alternative_mu); |
| 54 |
|
| 55 |
CompletionQueue* Ref() { |
| 56 |
grpc_core::MutexLock lock(&*g_callback_alternative_mu); |
| 57 |
refs++; |
| 58 |
if (refs == 1) { |
| 59 |
cq = new CompletionQueue; |
| 60 |
int num_nexting_threads = |
| 61 |
grpc_core::Clamp(gpr_cpu_num_cores() / 2, 2u, 16u); |
| 62 |
nexting_threads = new std::vector<grpc_core::Thread>; |
| 63 |
for (int i = 0; i < num_nexting_threads; i++) { |
| 64 |
nexting_threads->emplace_back( |
| 65 |
"nexting_thread", |
| 66 |
[](void* arg) { |
| 67 |
grpc_completion_queue* cq = |
| 68 |
static_cast<CompletionQueue*>(arg)->cq(); |
| 69 |
while (true) { |
| 70 |
// Use the raw Core next function rather than the C++ Next since |
| 71 |
// Next incorporates FinalizeResult and we actually want that |
| 72 |
// called from the callback functor itself. |
| 73 |
// TODO(vjpai): Migrate below to next without a timeout or idle |
| 74 |
// phase. That's currently starving out some other polling, |
| 75 |
// though. |
| 76 |
auto ev = grpc_completion_queue_next( |
| 77 |
cq, |
| 78 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 79 |
gpr_time_from_millis(1000, GPR_TIMESPAN)), |
| 80 |
nullptr); |
| 81 |
if (ev.type == GRPC_QUEUE_SHUTDOWN) { |
| 82 |
return; |
| 83 |
} |
| 84 |
if (ev.type == GRPC_QUEUE_TIMEOUT) { |
| 85 |
gpr_sleep_until( |
| 86 |
gpr_time_add(gpr_now(GPR_CLOCK_REALTIME), |
| 87 |
gpr_time_from_millis(100, GPR_TIMESPAN))); |
| 88 |
continue; |
| 89 |
} |
| 90 |
GPR_DEBUG_ASSERT(ev.type == GRPC_OP_COMPLETE); |
| 91 |
// We can always execute the callback inline rather than |
| 92 |
// pushing it to another Executor thread because this |
| 93 |
// thread is definitely running on a background thread, does not |
| 94 |
// hold any application locks before executing the callback, |
| 95 |
// and cannot be entered recursively. |
| 96 |
auto* functor = |
| 97 |
static_cast<grpc_completion_queue_functor*>(ev.tag); |
| 98 |
functor->functor_run(functor, ev.success); |
| 99 |
} |
| 100 |
}, |
| 101 |
cq); |
| 102 |
} |
| 103 |
for (auto& th : *nexting_threads) { |
| 104 |
th.Start(); |
| 105 |
} |
| 106 |
} |
| 107 |
return cq; |
| 108 |
} |
| 109 |
|
| 110 |
void Unref() { |
| 111 |
grpc_core::MutexLock lock(g_callback_alternative_mu); |
| 112 |
refs--; |
| 113 |
if (refs == 0) { |
| 114 |
cq->Shutdown(); |
| 115 |
for (auto& th : *nexting_threads) { |
| 116 |
th.Join(); |
| 117 |
} |
| 118 |
delete nexting_threads; |
| 119 |
delete cq; |
| 120 |
} |
| 121 |
} |
| 122 |
}; |
| 123 |
|
| 124 |
CallbackAlternativeCQ g_callback_alternative_cq; |
| 125 |
|
| 126 |
} // namespace |
| 127 |
|
| 128 |
// 'CompletionQueue' constructor can safely call GrpcLibraryCodegen(false) here |
| 129 |
// i.e not have GrpcLibraryCodegen call grpc_init(). This is because, to create |
| 130 |
// a 'grpc_completion_queue' instance (which is being passed as the input to |
| 131 |
// this constructor), one must have already called grpc_init(). |
| 132 |
CompletionQueue::CompletionQueue(grpc_completion_queue* take) |
| 133 |
: GrpcLibraryCodegen(false), cq_(take) { |
| 134 |
InitialAvalanching(); |
| 135 |
} |
| 136 |
|
| 137 |
void CompletionQueue::Shutdown() { |
| 138 |
g_gli_initializer.summon(); |
| 139 |
#ifndef NDEBUG |
| 140 |
if (!ServerListEmpty()) { |
| 141 |
gpr_log(GPR_ERROR, |
| 142 |
"CompletionQueue shutdown being shutdown before its server."); |
| 143 |
} |
| 144 |
#endif |
| 145 |
CompleteAvalanching(); |
| 146 |
} |
| 147 |
|
| 148 |
CompletionQueue::NextStatus CompletionQueue::AsyncNextInternal( |
| 149 |
void** tag, bool* ok, gpr_timespec deadline) { |
| 150 |
for (;;) { |
| 151 |
auto ev = grpc_completion_queue_next(cq_, deadline, nullptr); |
| 152 |
switch (ev.type) { |
| 153 |
case GRPC_QUEUE_TIMEOUT: |
| 154 |
return TIMEOUT; |
| 155 |
case GRPC_QUEUE_SHUTDOWN: |
| 156 |
return SHUTDOWN; |
| 157 |
case GRPC_OP_COMPLETE: |
| 158 |
auto core_cq_tag = |
| 159 |
static_cast<grpc::internal::CompletionQueueTag*>(ev.tag); |
| 160 |
*ok = ev.success != 0; |
| 161 |
*tag = core_cq_tag; |
| 162 |
if (core_cq_tag->FinalizeResult(tag, ok)) { |
| 163 |
return GOT_EVENT; |
| 164 |
} |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
CompletionQueue::CompletionQueueTLSCache::CompletionQueueTLSCache( |
| 171 |
CompletionQueue* cq) |
| 172 |
: cq_(cq), flushed_(false) { |
| 173 |
grpc_completion_queue_thread_local_cache_init(cq_->cq_); |
| 174 |
} |
| 175 |
|
| 176 |
CompletionQueue::CompletionQueueTLSCache::~CompletionQueueTLSCache() { |
| 177 |
GPR_ASSERT(flushed_); |
| 178 |
} |
| 179 |
|
| 180 |
bool CompletionQueue::CompletionQueueTLSCache::Flush(void** tag, bool* ok) { |
| 181 |
int res = 0; |
| 182 |
void* res_tag; |
| 183 |
flushed_ = true; |
| 184 |
if (grpc_completion_queue_thread_local_cache_flush(cq_->cq_, &res_tag, |
| 185 |
&res)) { |
| 186 |
auto core_cq_tag = |
| 187 |
static_cast<grpc::internal::CompletionQueueTag*>(res_tag); |
| 188 |
*ok = res == 1; |
| 189 |
if (core_cq_tag->FinalizeResult(tag, ok)) { |
| 190 |
return true; |
| 191 |
} |
| 192 |
} |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
CompletionQueue* CompletionQueue::CallbackAlternativeCQ() { |
| 197 |
gpr_once_init(&g_once_init_callback_alternative, |
| 198 |
[] { g_callback_alternative_mu = new grpc_core::Mutex(); }); |
| 199 |
return g_callback_alternative_cq.Ref(); |
| 200 |
} |
| 201 |
|
| 202 |
void CompletionQueue::ReleaseCallbackAlternativeCQ(CompletionQueue* cq) |
| 203 |
ABSL_NO_THREAD_SAFETY_ANALYSIS { |
| 204 |
(void)cq; |
| 205 |
// This accesses g_callback_alternative_cq without acquiring the mutex |
| 206 |
// but it's considered safe because it just reads the pointer address. |
| 207 |
GPR_DEBUG_ASSERT(cq == g_callback_alternative_cq.cq); |
| 208 |
g_callback_alternative_cq.Unref(); |
| 209 |
} |
| 210 |
|
| 211 |
} // namespace grpc |