aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/global/access.h
blob: e746f80c8bac8406fd64f9f02835562ba1bf42de (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once

#include <library/cpp/yt/memory/erased_storage.h>

#include <library/cpp/yt/misc/strong_typedef.h>

#include <atomic>

namespace NYT::NGlobal {

////////////////////////////////////////////////////////////////////////////////

namespace NDetail {

class TGlobalVariablesRegistry;

} // namespace NDetail

////////////////////////////////////////////////////////////////////////////////

inline constexpr size_t GlobalVariableMaxByteSize = 32;
using TErasedStorage = NYT::TErasedStorage<GlobalVariableMaxByteSize>;

// NB(arkady-e1ppa): Accessor must ensure thread-safety on its own.
using TAccessor = TErasedStorage(*)() noexcept;

////////////////////////////////////////////////////////////////////////////////

// Usage:
/*
 * // public.h file:
 * // NB: It's important to mark it inline for linker to deduplicate
 * // addresses accross different UT's.
 * inline constexpr NGlobal::TVariableTag MyGlobalVarTag = {};
 *
 *
 *
 * // some_stuff.cpp file
 *
 * TErasedStorage GetMyVar()
 * {
 * // definition here
 * }
 * NGlobal::Variable<int> MyGlobalVar{MyGlobalVarTag, &GetMyVar};
 *
 *
 *
 * // other_stuff.cpp file
 *
 *
 * int ReadMyVar()
 * {
 *   auto erased = NGlobal::GetErasedVariable(MyGlobalVarTag);
 *   return erased->AsConcrete<int>();
 * }
 */
class TVariableTag
{
public:
    TVariableTag() = default;

    TVariableTag(const TVariableTag& other) = delete;
    TVariableTag& operator=(const TVariableTag& other) = delete;

private:
    friend class ::NYT::NGlobal::NDetail::TGlobalVariablesRegistry;

    mutable std::atomic<bool> Initialized_ = false;
    mutable std::atomic<int> Key_ = -1;
};

////////////////////////////////////////////////////////////////////////////////

// Defined in impl.cpp.
// |std::nullopt| iff global variable with a given tag is not present.
std::optional<TErasedStorage> GetErasedVariable(const TVariableTag& tag);

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NGlobal