xref: /aosp_15_r20/external/abseil-cpp/absl/base/const_init.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker //
15*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
16*9356374aSAndroid Build Coastguard Worker // kConstInit
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // A constructor tag used to mark an object as safe for use as a global
20*9356374aSAndroid Build Coastguard Worker // variable, avoiding the usual lifetime issues that can affect globals.
21*9356374aSAndroid Build Coastguard Worker 
22*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_CONST_INIT_H_
23*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_CONST_INIT_H_
24*9356374aSAndroid Build Coastguard Worker 
25*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
26*9356374aSAndroid Build Coastguard Worker 
27*9356374aSAndroid Build Coastguard Worker // In general, objects with static storage duration (such as global variables)
28*9356374aSAndroid Build Coastguard Worker // can trigger tricky object lifetime situations.  Attempting to access them
29*9356374aSAndroid Build Coastguard Worker // from the constructors or destructors of other global objects can result in
30*9356374aSAndroid Build Coastguard Worker // undefined behavior, unless their constructors and destructors are designed
31*9356374aSAndroid Build Coastguard Worker // with this issue in mind.
32*9356374aSAndroid Build Coastguard Worker //
33*9356374aSAndroid Build Coastguard Worker // The normal way to deal with this issue in C++11 is to use constant
34*9356374aSAndroid Build Coastguard Worker // initialization and trivial destructors.
35*9356374aSAndroid Build Coastguard Worker //
36*9356374aSAndroid Build Coastguard Worker // Constant initialization is guaranteed to occur before any other code
37*9356374aSAndroid Build Coastguard Worker // executes.  Constructors that are declared 'constexpr' are eligible for
38*9356374aSAndroid Build Coastguard Worker // constant initialization.  You can annotate a variable declaration with the
39*9356374aSAndroid Build Coastguard Worker // ABSL_CONST_INIT macro to express this intent.  For compilers that support
40*9356374aSAndroid Build Coastguard Worker // it, this annotation will cause a compilation error for declarations that
41*9356374aSAndroid Build Coastguard Worker // aren't subject to constant initialization (perhaps because a runtime value
42*9356374aSAndroid Build Coastguard Worker // was passed as a constructor argument).
43*9356374aSAndroid Build Coastguard Worker //
44*9356374aSAndroid Build Coastguard Worker // On program shutdown, lifetime issues can be avoided on global objects by
45*9356374aSAndroid Build Coastguard Worker // ensuring that they contain  trivial destructors.  A class has a trivial
46*9356374aSAndroid Build Coastguard Worker // destructor unless it has a user-defined destructor, a virtual method or base
47*9356374aSAndroid Build Coastguard Worker // class, or a data member or base class with a non-trivial destructor of its
48*9356374aSAndroid Build Coastguard Worker // own.  Objects with static storage duration and a trivial destructor are not
49*9356374aSAndroid Build Coastguard Worker // cleaned up on program shutdown, and are thus safe to access from other code
50*9356374aSAndroid Build Coastguard Worker // running during shutdown.
51*9356374aSAndroid Build Coastguard Worker //
52*9356374aSAndroid Build Coastguard Worker // For a few core Abseil classes, we make a best effort to allow for safe global
53*9356374aSAndroid Build Coastguard Worker // instances, even though these classes have non-trivial destructors.  These
54*9356374aSAndroid Build Coastguard Worker // objects can be created with the absl::kConstInit tag.  For example:
55*9356374aSAndroid Build Coastguard Worker //   ABSL_CONST_INIT absl::Mutex global_mutex(absl::kConstInit);
56*9356374aSAndroid Build Coastguard Worker //
57*9356374aSAndroid Build Coastguard Worker // The line above declares a global variable of type absl::Mutex which can be
58*9356374aSAndroid Build Coastguard Worker // accessed at any point during startup or shutdown.  global_mutex's destructor
59*9356374aSAndroid Build Coastguard Worker // will still run, but will not invalidate the object.  Note that C++ specifies
60*9356374aSAndroid Build Coastguard Worker // that accessing an object after its destructor has run results in undefined
61*9356374aSAndroid Build Coastguard Worker // behavior, but this pattern works on the toolchains we support.
62*9356374aSAndroid Build Coastguard Worker //
63*9356374aSAndroid Build Coastguard Worker // The absl::kConstInit tag should only be used to define objects with static
64*9356374aSAndroid Build Coastguard Worker // or thread_local storage duration.
65*9356374aSAndroid Build Coastguard Worker 
66*9356374aSAndroid Build Coastguard Worker namespace absl {
67*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
68*9356374aSAndroid Build Coastguard Worker 
69*9356374aSAndroid Build Coastguard Worker enum ConstInitType {
70*9356374aSAndroid Build Coastguard Worker   kConstInit,
71*9356374aSAndroid Build Coastguard Worker };
72*9356374aSAndroid Build Coastguard Worker 
73*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
74*9356374aSAndroid Build Coastguard Worker }  // namespace absl
75*9356374aSAndroid Build Coastguard Worker 
76*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_BASE_CONST_INIT_H_
77