00001 // Copyright 2017 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 // 00015 // ----------------------------------------------------------------------------- 00016 // kConstInit 00017 // ----------------------------------------------------------------------------- 00018 // 00019 // A constructor tag used to mark an object as safe for use as a global 00020 // variable, avoiding the usual lifetime issues that can affect globals. 00021 00022 #ifndef ABSL_BASE_CONST_INIT_H_ 00023 #define ABSL_BASE_CONST_INIT_H_ 00024 00025 // In general, objects with static storage duration (such as global variables) 00026 // can trigger tricky object lifetime situations. Attempting to access them 00027 // from the constructors or destructors of other global objects can result in 00028 // undefined behavior, unless their constructors and destructors are designed 00029 // with this issue in mind. 00030 // 00031 // The normal way to deal with this issue in C++11 is to use constant 00032 // initialization and trivial destructors. 00033 // 00034 // Constant initialization is guaranteed to occur before any other code 00035 // executes. Constructors that are declared 'constexpr' are eligible for 00036 // constant initialization. You can annotate a variable declaration with the 00037 // ABSL_CONST_INIT macro to express this intent. For compilers that support 00038 // it, this annotation will cause a compilation error for declarations that 00039 // aren't subject to constant initialization (perhaps because a runtime value 00040 // was passed as a constructor argument). 00041 // 00042 // On program shutdown, lifetime issues can be avoided on global objects by 00043 // ensuring that they contain trivial destructors. A class has a trivial 00044 // destructor unless it has a user-defined destructor, a virtual method or base 00045 // class, or a data member or base class with a non-trivial destructor of its 00046 // own. Objects with static storage duration and a trivial destructor are not 00047 // cleaned up on program shutdown, and are thus safe to access from other code 00048 // running during shutdown. 00049 // 00050 // For a few core Abseil classes, we make a best effort to allow for safe global 00051 // instances, even though these classes have non-trivial destructors. These 00052 // objects can be created with the absl::kConstInit tag. For example: 00053 // ABSL_CONST_INIT absl::Mutex global_mutex(absl::kConstInit); 00054 // 00055 // The line above declares a global variable of type absl::Mutex which can be 00056 // accessed at any point during startup or shutdown. global_mutex's destructor 00057 // will still run, but will not invalidate the object. Note that C++ specifies 00058 // that accessing an object after its destructor has run results in undefined 00059 // behavior, but this pattern works on the toolchains we support. 00060 // 00061 // The absl::kConstInit tag should only be used to define objects with static 00062 // or thread_local storage duration. 00063 00064 namespace absl { 00065 00066 enum ConstInitType { 00067 kConstInit, 00068 }; 00069 00070 } // namespace absl 00071 00072 #endif // ABSL_BASE_CONST_INIT_H_