aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/global/variable-inl.h
diff options
context:
space:
mode:
authorarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-08-15 18:32:55 +0300
committerarkady-e1ppa <arkady-e1ppa@yandex-team.com>2024-08-15 18:44:19 +0300
commit28ff4da78aa89f0226af33522332b7f06521412e (patch)
tree09c5bbbba83b4a04d51a77c654d0eaeab9bb404f /library/cpp/yt/global/variable-inl.h
parent0ecc758c681a1a8d54fbf95f0b58bba175518fac (diff)
downloadydb-28ff4da78aa89f0226af33522332b7f06521412e.tar.gz
YT-21233: Split error into error and stripped_error to rid the latter of deps on global variables
What happened: 1. error contents has been split into stripped_error and error. stripped_error contains the error itself (with attributes for now) and macros; error contains stripped_error and some extensions, namely, functions to get fiberId, hostname and traceid/spanid and all functions used to (de-)serialize error. This means that you cannot print error if you only include stripped_error, therefore you are likely to still require the entire error.h at the moment. 2. Mechanic for gathering origin attributes has been moved to newly created library/cpp/yt/error thus having no dependency on fibers, net or tracing. stripped_error uses these attributes as extendable semi-erased (meaning, you still would have to add a field and recompile the entire thing, but you don't have to introduce an extra dependency) storage for a bunch of attributes 3. Parsing of said attributes is done in error file (and not stripped_error). P.S. So far the plan is to eventually move stripped_error (once dependency on core/ytree/attributes is eliminated) without any actual change to dependency graph of anything outside of core (e.g. you would still have to include misc/error.h to use it). Next step would be re-teaching the error how to print, which would move some more methods from core to the standalone module. After that one could finally depend on the error itself and not the entire core. Annotations: [nodiff:caesar] 66615172181355821241d2e5f8e4a0f15e0ea791
Diffstat (limited to 'library/cpp/yt/global/variable-inl.h')
-rw-r--r--library/cpp/yt/global/variable-inl.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/library/cpp/yt/global/variable-inl.h b/library/cpp/yt/global/variable-inl.h
new file mode 100644
index 0000000000..32d22fd7c8
--- /dev/null
+++ b/library/cpp/yt/global/variable-inl.h
@@ -0,0 +1,95 @@
+#ifndef GLOBAL_VARIABLE_INL_H_
+#error "Direct inclusion of this file is not allowed, include variable.h"
+// For the sake of sane code completion.
+#include "variable.h"
+#endif
+
+namespace NYT::NGlobal {
+
+////////////////////////////////////////////////////////////////////////////////
+
+template <CTriviallyErasable<GlobalVariableMaxByteSize> T>
+TVariable<T>::TVariable(
+ const TVariableTag& tag,
+ TAccessor accessor,
+ T initValue) noexcept
+ : Value_(initValue)
+{
+ NDetail::RegisterVariable(tag, accessor);
+}
+
+template <CTriviallyErasable<GlobalVariableMaxByteSize> T>
+T TVariable<T>::Get() const noexcept
+{
+ return Value_;
+}
+
+template <CTriviallyErasable<GlobalVariableMaxByteSize> T>
+void TVariable<T>::Set(T value) noexcept
+{
+ Value_ = value;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+#undef YT_DEFINE_TRACKED_GLOBAL
+#undef YT_DEFINE_TRACKED_THREAD_LOCAL
+
+////////////////////////////////////////////////////////////////////////////////
+
+#define YT_DEFINE_TRACKED_GLOBAL(Type, Name, Tag, InitExpr) \
+ namespace NGlobalTracking##Name##Tag { \
+ \
+ ::NYT::NGlobal::TErasedStorage GetErased##Name() noexcept; \
+ \
+ static ::NYT::NGlobal::TVariable<Type> Name{Tag, GetErased##Name, (InitExpr)}; \
+ \
+ ::NYT::NGlobal::TErasedStorage GetErased##Name() noexcept \
+ { \
+ return ::NYT::NGlobal::TErasedStorage{Name.Get()}; \
+ } \
+ \
+ } /*namespace NGlobalTracking##Name##Tag*/ \
+ using NGlobalTracking##Name##Tag::Name; \
+ static_assert(true)
+
+// NB(arkady-e1ppa): We must ensure that tracker is constructed thus
+// we have to call ref tracker inside tls accessor.
+// NB(arkady-e1ppa): Unlike normal static variable, we cannot just pull
+// varibale name out as we might want to forward-declare thread local variable
+// now that it is modelled as function. Pulling alias from ns unfortunately
+// doesn't work as function definition :(.
+#define YT_DEFINE_TRACKED_THREAD_LOCAL(Type, Name, Tag, ...) \
+ Y_NO_INLINE Type& Name(); \
+ namespace NGlobalTracking##Name##Tag { \
+ \
+ void EnsureTracked() noexcept; \
+ ::NYT::NGlobal::TErasedStorage GetErased##Name() noexcept; \
+ \
+ static ::NYT::NGlobal::TVariable<std::byte> TlsTrackerFor##Name{Tag, GetErased##Name}; \
+ \
+ void EnsureTracked() noexcept \
+ { \
+ auto val = TlsTrackerFor##Name.Get(); \
+ Y_UNUSED(val); \
+ } \
+ \
+ ::NYT::NGlobal::TErasedStorage GetErased##Name() noexcept \
+ { \
+ return ::NYT::NGlobal::TErasedStorage{Name()}; \
+ } \
+ \
+ } /*namespace NGlobalTracking##Name##Tag*/ \
+ Y_NO_INLINE Type& Name() \
+ { \
+ thread_local Type tlsData { __VA_ARGS__ }; \
+ asm volatile(""); \
+ NGlobalTracking##Name##Tag::EnsureTracked(); \
+ return tlsData; \
+ } \
+ \
+ static_assert(true)
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT::NGlobal