userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
thread_local.hpp
Go to the documentation of this file.
1
#pragma once
2
5
6
#include <type_traits>
7
8
#include <userver/compiler/impl/lifetime.hpp>
9
#include <userver/compiler/impl/tls.hpp>
10
11
USERVER_NAMESPACE_BEGIN
12
13
namespace
compiler
{
14
15
namespace
impl {
16
17
bool
AreCoroutineSwitchesAllowed() noexcept;
18
19
void
IncrementLocalCoroutineSwitchBans() noexcept;
20
21
void
DecrementLocalCoroutineSwitchBans() noexcept;
22
23
}
// namespace impl
24
31
class CoroutineSwitchBanScope {
32
public
:
33
CoroutineSwitchBanScope()
noexcept
;
34
35
CoroutineSwitchBanScope(CoroutineSwitchBanScope&&) =
delete
;
36
CoroutineSwitchBanScope& operator=(CoroutineSwitchBanScope&&) =
delete
;
37
~CoroutineSwitchBanScope();
38
};
39
43
template
<
typename
VariableType>
44
class
ThreadLocalScope final :
private
CoroutineSwitchBanScope {
45
public
:
46
ThreadLocalScope(ThreadLocalScope&&) =
delete
;
47
ThreadLocalScope& operator=(ThreadLocalScope&&) =
delete
;
48
~ThreadLocalScope() =
default
;
49
51
VariableType&
operator*
() &
noexcept
USERVER_IMPL_LIFETIME_BOUND;
52
54
VariableType*
operator->
() &
noexcept
USERVER_IMPL_LIFETIME_BOUND;
55
57
explicit
ThreadLocalScope(VariableType& variable)
noexcept
;
58
59
// Store ThreadLocalScope to a variable before using.
60
VariableType&
operator*
() &&
noexcept
=
delete
;
61
62
// Store ThreadLocalScope to a variable before using.
63
VariableType*
operator->
() &&
noexcept
=
delete
;
65
66
private
:
67
static_assert
(!std::is_reference_v<VariableType>);
68
static_assert
(!std::is_const_v<VariableType>);
69
70
VariableType& variable_;
71
};
72
127
template
<
typename
VariableType,
typename
Factory>
128
class
ThreadLocal final {
129
static_assert
(std::is_empty_v<Factory>);
130
static_assert
(std::is_default_constructible_v<Factory>);
131
static_assert
(std::is_same_v<VariableType, std::invoke_result_t<const Factory&>>);
132
133
public
:
134
consteval
ThreadLocal() =
default
;
135
136
consteval
/*implicit*/
ThreadLocal(Factory) {}
137
138
ThreadLocalScope<VariableType>
Use() {
139
return
ThreadLocalScope<VariableType>
{impl::GetThreadLocal([]() -> VariableType& {
140
thread_local
VariableType variable{
static_cast<
const
Factory&
>
(Factory{})()};
141
return
variable;
142
})};
143
}
144
};
145
146
template
<
typename
Factory>
147
ThreadLocal
(Factory factory) ->
ThreadLocal<std::invoke_result_t<const Factory&>
, Factory>;
148
149
inline
CoroutineSwitchBanScope::CoroutineSwitchBanScope() noexcept {
150
#ifndef NDEBUG
151
impl::IncrementLocalCoroutineSwitchBans();
152
#endif
153
}
154
155
inline
CoroutineSwitchBanScope::~CoroutineSwitchBanScope() {
156
#ifndef NDEBUG
157
impl::DecrementLocalCoroutineSwitchBans();
158
#endif
159
}
160
161
template
<
typename
VariableType>
162
ThreadLocalScope<VariableType>::ThreadLocalScope(VariableType& variable) noexcept : variable_(variable) {}
163
164
template
<
typename
VariableType>
165
VariableType&
ThreadLocalScope<VariableType>::operator*
() &
noexcept
USERVER_IMPL_LIFETIME_BOUND {
166
return
variable_;
167
}
168
169
template
<
typename
VariableType>
170
VariableType*
ThreadLocalScope<VariableType>::operator->
() &
noexcept
USERVER_IMPL_LIFETIME_BOUND {
171
return
&**
this
;
172
}
173
174
}
// namespace compiler
175
176
USERVER_NAMESPACE_END
userver
compiler
thread_local.hpp
Generated on
for userver by
Doxygen
1.17.0