userver: userver/compiler/thread_local.hpp Source File
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
11USERVER_NAMESPACE_BEGIN
12
13namespace compiler {
14
15namespace impl {
16
17bool AreCoroutineSwitchesAllowed() noexcept;
18
19void IncrementLocalCoroutineSwitchBans() noexcept;
20
21void DecrementLocalCoroutineSwitchBans() noexcept;
22
23} // namespace impl
24
31class CoroutineSwitchBanScope {
32public:
33 CoroutineSwitchBanScope() noexcept;
34
35 CoroutineSwitchBanScope(CoroutineSwitchBanScope&&) = delete;
36 CoroutineSwitchBanScope& operator=(CoroutineSwitchBanScope&&) = delete;
37 ~CoroutineSwitchBanScope();
38};
39
43template <typename VariableType>
44class ThreadLocalScope final : private CoroutineSwitchBanScope {
45public:
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
66private:
67 static_assert(!std::is_reference_v<VariableType>);
68 static_assert(!std::is_const_v<VariableType>);
69
70 VariableType& variable_;
71};
72
127template <typename VariableType, typename Factory>
128class 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
133public:
134 consteval ThreadLocal() = default;
135
136 consteval /*implicit*/ ThreadLocal(Factory) {}
137
139 return ThreadLocalScope<VariableType>{impl::GetThreadLocal([]() -> VariableType& {
140 thread_local VariableType variable{static_cast<const Factory&>(Factory{})()};
141 return variable;
142 })};
143 }
144};
145
146template <typename Factory>
148
149inline CoroutineSwitchBanScope::CoroutineSwitchBanScope() noexcept {
150#ifndef NDEBUG
151 impl::IncrementLocalCoroutineSwitchBans();
152#endif
153}
154
155inline CoroutineSwitchBanScope::~CoroutineSwitchBanScope() {
156#ifndef NDEBUG
157 impl::DecrementLocalCoroutineSwitchBans();
158#endif
159}
160
161template <typename VariableType>
162ThreadLocalScope<VariableType>::ThreadLocalScope(VariableType& variable) noexcept : variable_(variable) {}
163
164template <typename VariableType>
165VariableType& ThreadLocalScope<VariableType>::operator*() & noexcept USERVER_IMPL_LIFETIME_BOUND {
166 return variable_;
167}
168
169template <typename VariableType>
170VariableType* ThreadLocalScope<VariableType>::operator->() & noexcept USERVER_IMPL_LIFETIME_BOUND {
171 return &**this;
172}
173
174} // namespace compiler
175
176USERVER_NAMESPACE_END