Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
critical_alloc_section.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright 2020 ScyllaDB
20 */
21
22#pragma once
23
24namespace seastar {
25namespace memory {
26
27#ifdef SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
28
30namespace internal {
31
32// This variable is used in hot paths so we want to avoid the compiler
33// generating TLS init guards for it. In C++20 we have constinit to tell the
34// compiler that it can be initialized compile time (although gcc still doesn't
35// completely drops the init guards - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97848).
36// In < c++20 we use `__thread` which results in no TLS init guards generated.
37#ifdef __cpp_constinit
38extern thread_local constinit volatile int critical_alloc_section;
39#else
40extern __thread volatile int critical_alloc_section;
41#endif
42
43} // namespace internal
45
57class scoped_critical_alloc_section {
58public:
59 scoped_critical_alloc_section() {
60 // we assume the critical_alloc_section is thread local
61 // and there's seastar threads are non-preemptive.
62 // Otherwise, this would require an atomic variable
63 internal::critical_alloc_section = internal::critical_alloc_section + 1;
64 }
65 ~scoped_critical_alloc_section() {
66 internal::critical_alloc_section = internal::critical_alloc_section - 1;
67 }
68};
69
74inline bool is_critical_alloc_section() {
75 return bool(internal::critical_alloc_section);
76}
77
78#else // SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
79
80struct [[maybe_unused]] scoped_critical_alloc_section {};
81
82inline bool is_critical_alloc_section() {
83 return false;
84}
85
86#endif // SEASTAR_ENABLE_ALLOC_FAILURE_INJECTION
87
88} // namespace seastar
89} // namespace memory
Definition: critical_alloc_section.hh:80
Seastar API namespace.
Definition: abort_on_ebadf.hh:26