proxygen
Settings.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018-present Facebook, Inc.
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 #pragma once
17 
18 #include <functional>
19 #include <string>
20 
21 #include <folly/Range.h>
24 
25 namespace folly {
26 namespace settings {
27 
28 class Snapshot;
29 namespace detail {
30 
35 template <class T, std::atomic<uint64_t>* TrivialPtr>
37  public:
48  return core_.getWithHint(*TrivialPtr);
49  }
50  const T* operator->() const {
51  return &core_.getSlow().value;
52  }
53 
62  void set(const T& t, StringPiece reason = "api") {
63  core_.set(t, reason);
64  }
65 
72  const T& defaultValue() const {
73  return core_.defaultValue();
74  }
75 
76  explicit SettingWrapper(SettingCore<T>& core) : core_(core) {}
77 
78  private:
81 };
82 
83 /* C++20 has std::type_indentity */
84 template <class T>
85 struct TypeIdentity {
86  using type = T;
87 };
88 template <class T>
90 
106 #define FOLLY_SETTINGS_DEFINE_LOCAL_FUNC__( \
107  _project, _name, _Type, _overloadType) \
108  extern std::atomic<folly::settings::detail::SettingCore<_Type>*> \
109  FOLLY_SETTINGS_CACHE__##_project##_##_name; \
110  extern std::atomic<uint64_t> FOLLY_SETTINGS_TRIVIAL__##_project##_##_name; \
111  folly::settings::detail::SettingCore<_Type>& \
112  FOLLY_SETTINGS_FUNC__##_project##_##_name(); \
113  FOLLY_ALWAYS_INLINE auto FOLLY_SETTINGS_LOCAL_FUNC__##_project##_##_name( \
114  _overloadType) { \
115  if (!FOLLY_SETTINGS_CACHE__##_project##_##_name.load()) { \
116  FOLLY_SETTINGS_CACHE__##_project##_##_name.store( \
117  &FOLLY_SETTINGS_FUNC__##_project##_##_name()); \
118  } \
119  return folly::settings::detail:: \
120  SettingWrapper<_Type, &FOLLY_SETTINGS_TRIVIAL__##_project##_##_name>( \
121  *FOLLY_SETTINGS_CACHE__##_project##_##_name.load()); \
122  } \
123  /* This is here just to force a semicolon */ \
124  folly::settings::detail::SettingCore<_Type>& \
125  FOLLY_SETTINGS_FUNC__##_project##_##_name()
126 
127 } // namespace detail
128 
149 #define FOLLY_SETTING_DEFINE(_project, _name, _Type, _def, _desc) \
150  /* Fastpath optimization, see notes in FOLLY_SETTINGS_DEFINE_LOCAL_FUNC__. \
151  Aggregate all off these together in a single section for better TLB \
152  and cache locality. */ \
153  __attribute__((__section__(".folly.settings.cache"))) \
154  std::atomic<folly::settings::detail::SettingCore<_Type>*> \
155  FOLLY_SETTINGS_CACHE__##_project##_##_name; \
156  /* Location for the small value cache (if _Type is small and trivial). \
157  Intentionally located right after the pointer cache above to take \
158  advantage of the prefetching */ \
159  __attribute__((__section__(".folly.settings.cache"))) std::atomic<uint64_t> \
160  FOLLY_SETTINGS_TRIVIAL__##_project##_##_name; \
161  /* Meyers singleton to avoid SIOF */ \
162  FOLLY_NOINLINE folly::settings::detail::SettingCore<_Type>& \
163  FOLLY_SETTINGS_FUNC__##_project##_##_name() { \
164  static folly::Indestructible<folly::settings::detail::SettingCore<_Type>> \
165  setting( \
166  folly::settings::SettingMetadata{ \
167  #_project, #_name, #_Type, typeid(_Type), #_def, _desc}, \
168  folly::settings::detail::TypeIdentityT<_Type>{_def}, \
169  FOLLY_SETTINGS_TRIVIAL__##_project##_##_name); \
170  return *setting; \
171  } \
172  /* Ensure the setting is registered even if not used in program */ \
173  auto& FOLLY_SETTINGS_INIT__##_project##_##_name = \
174  FOLLY_SETTINGS_FUNC__##_project##_##_name(); \
175  FOLLY_SETTINGS_DEFINE_LOCAL_FUNC__(_project, _name, _Type, char)
176 
180 #define FOLLY_SETTING_DECLARE(_project, _name, _Type) \
181  FOLLY_SETTINGS_DEFINE_LOCAL_FUNC__(_project, _name, _Type, int)
182 
192 #define FOLLY_SETTING(_project, _name) \
193  FOLLY_SETTINGS_LOCAL_FUNC__##_project##_##_name(0)
194 
199 Optional<SettingMetadata> getSettingsMeta(StringPiece settingName);
200 
201 namespace detail {
202 
207 template <class T>
209  public:
215  const T& operator*() const;
216  const T* operator->() const {
217  return &operator*();
218  }
219 
224  void set(const T& t, StringPiece reason = "api") {
225  core_.set(t, reason, &snapshot_);
226  }
227 
228  private:
232 
234  : snapshot_(snapshot), core_(core) {}
235 };
236 
237 } // namespace detail
238 
267 class Snapshot final : public detail::SnapshotBase {
268  public:
272  template <class T, std::atomic<uint64_t>* P>
274  detail::SettingWrapper<T, P>&& setting) {
275  return detail::SnapshotSettingWrapper<T>(*this, setting.core_);
276  }
277 
283  Snapshot() = default;
284 
289  void publish() override;
290 
299  bool setFromString(
300  StringPiece settingName,
301  StringPiece newValue,
302  StringPiece reason) override;
303 
308  Optional<SettingsInfo> getAsString(StringPiece settingName) const override;
309 
316  bool resetToDefault(StringPiece settingName) override;
317 
322  void forEachSetting(const std::function<
323  void(const SettingMetadata&, StringPiece, StringPiece)>&
324  func) const override;
325 
326  private:
327  template <typename T>
329 };
330 
331 namespace detail {
332 template <class T>
334  return snapshot_.get(core_).value;
335 }
336 } // namespace detail
337 
338 } // namespace settings
339 } // namespace folly
folly::std T
static http_parser_settings settings
Definition: test.c:1529
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
typename TypeIdentity< T >::type TypeIdentityT
Definition: Settings.h:89
detail::SnapshotSettingWrapper< T > operator()(detail::SettingWrapper< T, P > &&setting)
Definition: Settings.h:273
SnapshotSettingWrapper(Snapshot &snapshot, SettingCore< T > &core)
Definition: Settings.h:233
static const char *const value
Definition: Conv.cpp:50
SettingWrapper(SettingCore< T > &core)
Definition: Settings.h:76
Optional< SettingMetadata > getSettingsMeta(StringPiece settingName)
Definition: Settings.cpp:53
Range< const char * > StringPiece
std::conditional_t< IsSmallPOD< T >::value, T, const T & > operator*() const
Definition: Settings.h:47