core_configuration.cc
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
18 
19 #include <algorithm>
20 #include <atomic>
21 #include <utility>
22 #include <vector>
23 
24 #include <grpc/support/log.h>
25 
26 namespace grpc_core {
27 
28 std::atomic<CoreConfiguration*> CoreConfiguration::config_{nullptr};
29 std::atomic<CoreConfiguration::RegisteredBuilder*> CoreConfiguration::builders_{
30  nullptr};
32 
34 
36  return new CoreConfiguration(this);
37 }
38 
47 
49  GPR_ASSERT(config_.load(std::memory_order_relaxed) == nullptr &&
50  "CoreConfiguration was already instantiated before builder "
51  "registration was completed");
53  n->builder = std::move(builder);
54  n->next = builders_.load(std::memory_order_relaxed);
55  while (!builders_.compare_exchange_weak(n->next, n, std::memory_order_acq_rel,
56  std::memory_order_relaxed)) {
57  }
58  GPR_ASSERT(config_.load(std::memory_order_relaxed) == nullptr &&
59  "CoreConfiguration was already instantiated before builder "
60  "registration was completed");
61 }
62 
64  // Construct builder, pass it up to code that knows about build configuration
66  // The linked list of builders stores things in reverse registration order.
67  // To get things registered as systems relying on this expect however, we
68  // actually need to run things in forward registration order, so we iterate
69  // once over the linked list to build a vector of builders, and then iterate
70  // over said vector in reverse to actually run the builders.
71  std::vector<RegisteredBuilder*> registered_builders;
72  for (RegisteredBuilder* b = builders_.load(std::memory_order_acquire);
73  b != nullptr; b = b->next) {
74  registered_builders.push_back(b);
75  }
76  for (auto it = registered_builders.rbegin(); it != registered_builders.rend();
77  ++it) {
78  (*it)->builder(&builder);
79  }
80  // Finally, call the built in configuration builder.
81  if (default_builder_ != nullptr) (*default_builder_)(&builder);
82  // Use builder to construct a confguration
83  CoreConfiguration* p = builder.Build();
84  // Try to set configuration global - it's possible another thread raced us
85  // here, in which case we drop the work we did and use the one that got set
86  // first
87  CoreConfiguration* expected = nullptr;
88  if (!config_.compare_exchange_strong(expected, p, std::memory_order_acq_rel,
89  std::memory_order_acquire)) {
90  delete p;
91  return *expected;
92  }
93  return *p;
94 }
95 
97  delete config_.exchange(nullptr, std::memory_order_acquire);
99  builders_.exchange(nullptr, std::memory_order_acquire);
100  while (builder != nullptr) {
101  RegisteredBuilder* next = builder->next;
102  delete builder;
103  builder = next;
104  }
105 }
106 
107 } // namespace grpc_core
grpc_core::CoreConfiguration::service_config_parser_
ServiceConfigParser service_config_parser_
Definition: core_configuration.h:189
grpc_core::CoreConfiguration::BuildNewAndMaybeSet
static const CoreConfiguration & BuildNewAndMaybeSet()
Definition: core_configuration.cc:63
regen-readme.it
it
Definition: regen-readme.py:15
log.h
core_configuration.h
grpc_core::CoreConfiguration::Builder::CoreConfiguration
friend class CoreConfiguration
Definition: core_configuration.h:66
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::CoreConfiguration::Builder
Definition: core_configuration.h:41
grpc_core::CoreConfiguration
Definition: core_configuration.h:34
profile_analyzer.builder
builder
Definition: profile_analyzer.py:159
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::CoreConfiguration::handshaker_registry_
HandshakerRegistry handshaker_registry_
Definition: core_configuration.h:187
grpc_core::CoreConfiguration::config_
static std::atomic< CoreConfiguration * > config_
Definition: core_configuration.h:179
grpc_core::CoreConfiguration::default_builder_
static void(* default_builder_)(CoreConfiguration::Builder *)
Definition: core_configuration.h:183
grpc_core::CoreConfiguration::Builder::Build
CoreConfiguration * Build()
Definition: core_configuration.cc:35
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
grpc_core::CoreConfiguration::RegisterBuilder
static void RegisterBuilder(std::function< void(Builder *)> builder)
Definition: core_configuration.cc:48
grpc_core::CoreConfiguration::channel_creds_registry_
ChannelCredsRegistry channel_creds_registry_
Definition: core_configuration.h:188
grpc_core::CoreConfiguration::channel_args_preconditioning_
ChannelArgsPreconditioning channel_args_preconditioning_
Definition: core_configuration.h:185
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
grpc_core::CoreConfiguration::Reset
static void Reset()
Definition: core_configuration.cc:96
grpc_core::CoreConfiguration::builders_
static std::atomic< RegisteredBuilder * > builders_
Definition: core_configuration.h:181
Builder
struct Builder Builder
Definition: bloaty/third_party/protobuf/ruby/ext/google/protobuf_c/protobuf.h:68
grpc_core::CoreConfiguration::CoreConfiguration
CoreConfiguration(const CoreConfiguration &)=delete
grpc_core::CoreConfiguration::channel_init_
ChannelInit channel_init_
Definition: core_configuration.h:186
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
grpc_core::CoreConfiguration::RegisteredBuilder
Definition: core_configuration.h:169
grpc_core::CoreConfiguration::Builder::Builder
Builder()
grpc_core::CoreConfiguration::resolver_registry_
ResolverRegistry resolver_registry_
Definition: core_configuration.h:190
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:58:57