blob: 6a43d9465cdef66c3f71a7218ad431505bb0abf3 (
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
#include "codicil.h"
#include <yt/yt/core/concurrency/fls.h>
#include <library/cpp/yt/small_containers/compact_vector.h>
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
namespace NDetail {
using TCodicilBuilderStack = TCompactVector<TCodicilBuilder, 16>;
NConcurrency::TFlsSlot<TCodicilBuilderStack>& CodicilBuilderStackSlot()
{
static NConcurrency::TFlsSlot<TCodicilBuilderStack> result;
return result;
}
} // namespace NDetail
void PushCodicilBuilder(TCodicilBuilder&& builder)
{
NDetail::CodicilBuilderStackSlot()->push_back(std::move(builder));
}
void PopCodicilBuilder()
{
auto& stack = *NDetail::CodicilBuilderStackSlot();
YT_ASSERT(!stack.empty());
stack.pop_back();
}
TRange<TCodicilBuilder> GetCodicilBuilders()
{
// NB: Don't forcefully construct FLS slot to avoid allocations;
// these may lead to deadlocks if the program crashes during an allocation itself.
if (!NDetail::CodicilBuilderStackSlot().IsInitialized()) {
return {};
}
return TRange(*NDetail::CodicilBuilderStackSlot());
}
std::vector<std::string> BuildCodicils()
{
auto builders = GetCodicilBuilders();
std::vector<std::string> result;
result.reserve(builders.size());
TCodicilFormatter formatter;
for (const auto& builder : builders) {
formatter.Reset();
builder(&formatter);
result.emplace_back(formatter.GetBuffer());
}
return result;
}
TCodicilBuilder MakeNonOwningCodicilBuilder(TStringBuf codicil)
{
return [codicil] (TCodicilFormatter* formatter) {
formatter->AppendString(codicil);
};
}
TCodicilBuilder MakeOwningCodicilBuilder(std::string codicil)
{
return [codicil = std::move(codicil)] (TCodicilFormatter* formatter) {
formatter->AppendString(codicil);
};
}
////////////////////////////////////////////////////////////////////////////////
TCodicilGuard::TCodicilGuard(TCodicilBuilder&& builder)
: Active_(true)
{
PushCodicilBuilder(std::move(builder));
}
TCodicilGuard::~TCodicilGuard()
{
Release();
}
TCodicilGuard::TCodicilGuard(TCodicilGuard&& other)
: Active_(other.Active_)
{
other.Active_ = false;
}
TCodicilGuard& TCodicilGuard::operator=(TCodicilGuard&& other)
{
if (this != &other) {
Release();
Active_ = other.Active_;
other.Active_ = false;
}
return *this;
}
void TCodicilGuard::Release()
{
if (Active_) {
PopCodicilBuilder();
Active_ = false;
}
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|