const_init.h 3.4 KB

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