|
|
|||
File indexing completed on 2025-12-16 09:40:42
0001 // Copyright 2017 The Abseil Authors. 0002 // 0003 // Licensed under the Apache License, Version 2.0 (the "License"); 0004 // you may not use this file except in compliance with the License. 0005 // You may obtain a copy of the License at 0006 // 0007 // https://www.apache.org/licenses/LICENSE-2.0 0008 // 0009 // Unless required by applicable law or agreed to in writing, software 0010 // distributed under the License is distributed on an "AS IS" BASIS, 0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0012 // See the License for the specific language governing permissions and 0013 // limitations under the License. 0014 // 0015 // ----------------------------------------------------------------------------- 0016 // kConstInit 0017 // ----------------------------------------------------------------------------- 0018 // 0019 // A constructor tag used to mark an object as safe for use as a global 0020 // variable, avoiding the usual lifetime issues that can affect globals. 0021 0022 #ifndef ABSL_BASE_CONST_INIT_H_ 0023 #define ABSL_BASE_CONST_INIT_H_ 0024 0025 #include "absl/base/config.h" 0026 0027 // In general, objects with static storage duration (such as global variables) 0028 // can trigger tricky object lifetime situations. Attempting to access them 0029 // from the constructors or destructors of other global objects can result in 0030 // undefined behavior, unless their constructors and destructors are designed 0031 // with this issue in mind. 0032 // 0033 // The normal way to deal with this issue in C++11 is to use constant 0034 // initialization and trivial destructors. 0035 // 0036 // Constant initialization is guaranteed to occur before any other code 0037 // executes. Constructors that are declared 'constexpr' are eligible for 0038 // constant initialization. You can annotate a variable declaration with the 0039 // ABSL_CONST_INIT macro to express this intent. For compilers that support 0040 // it, this annotation will cause a compilation error for declarations that 0041 // aren't subject to constant initialization (perhaps because a runtime value 0042 // was passed as a constructor argument). 0043 // 0044 // On program shutdown, lifetime issues can be avoided on global objects by 0045 // ensuring that they contain trivial destructors. A class has a trivial 0046 // destructor unless it has a user-defined destructor, a virtual method or base 0047 // class, or a data member or base class with a non-trivial destructor of its 0048 // own. Objects with static storage duration and a trivial destructor are not 0049 // cleaned up on program shutdown, and are thus safe to access from other code 0050 // running during shutdown. 0051 // 0052 // For a few core Abseil classes, we make a best effort to allow for safe global 0053 // instances, even though these classes have non-trivial destructors. These 0054 // objects can be created with the absl::kConstInit tag. For example: 0055 // ABSL_CONST_INIT absl::Mutex global_mutex(absl::kConstInit); 0056 // 0057 // The line above declares a global variable of type absl::Mutex which can be 0058 // accessed at any point during startup or shutdown. global_mutex's destructor 0059 // will still run, but will not invalidate the object. Note that C++ specifies 0060 // that accessing an object after its destructor has run results in undefined 0061 // behavior, but this pattern works on the toolchains we support. 0062 // 0063 // The absl::kConstInit tag should only be used to define objects with static 0064 // or thread_local storage duration. 0065 0066 namespace absl { 0067 ABSL_NAMESPACE_BEGIN 0068 0069 enum ConstInitType { 0070 kConstInit, 0071 }; 0072 0073 ABSL_NAMESPACE_END 0074 } // namespace absl 0075 0076 #endif // ABSL_BASE_CONST_INIT_H_
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|