initialization_state.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  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. // http://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. #ifndef CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_H_
  15. #define CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_H_
  16. #include <stdint.h>
  17. #include "base/macros.h"
  18. namespace crashpad {
  19. //! \brief Tracks whether data are initialized.
  20. //!
  21. //! Objects of this type track whether the data they’re guarding are
  22. //! initialized. The three possible states are uninitialized (the initial
  23. //! state), initializing, and valid. As the guarded data are initialized, an
  24. //! InitializationState object will normally transition through these three
  25. //! states. A fourth state corresponds to the destruction of objects of this
  26. //! type, making it less likely that a use-after-free of an InitializationState
  27. //! object will appear in the valid state.
  28. //!
  29. //! If the only purpose for tracking the initialization state of guarded data is
  30. //! to DCHECK when the object is in an unexpected state, use
  31. //! InitializationStateDcheck instead.
  32. class InitializationState {
  33. public:
  34. //! \brief The object’s state.
  35. enum State : uint8_t {
  36. //! \brief The object has not yet been initialized.
  37. kStateUninitialized = 0,
  38. //! \brief The object is being initialized.
  39. //!
  40. //! This state protects against attempted reinitializaton of
  41. //! partially-initialized objects whose initial initialization attempt
  42. //! failed. This state is to be used while objects are initializing, but are
  43. //! not yet fully initialized.
  44. kStateInvalid,
  45. //! \brief The object has been initialized.
  46. kStateValid,
  47. //! \brief The object has been destroyed.
  48. kStateDestroyed,
  49. };
  50. InitializationState() : state_(kStateUninitialized) {}
  51. ~InitializationState() { state_ = kStateDestroyed; }
  52. //! \brief Returns `true` if the object’s state is #kStateUninitialized and it
  53. //! is safe to begin initializing it.
  54. bool is_uninitialized() const { return state_ == kStateUninitialized; }
  55. //! \brief Sets the object’s state to #kStateInvalid, marking initialization
  56. //! as being in process.
  57. void set_invalid() { state_ = kStateInvalid; }
  58. //! \brief Sets the object’s state to #kStateValid, marking it initialized.
  59. void set_valid() { state_ = kStateValid; }
  60. //! \brief Returns `true` if the the object’s state is #kStateValid and it has
  61. //! been fully initialized and may be used.
  62. bool is_valid() const { return state_ == kStateValid; }
  63. protected:
  64. //! \brief Returns the object’s state.
  65. //!
  66. //! Consumers of this class should use an is_state_*() method instead.
  67. State state() const { return state_; }
  68. //! \brief Sets the object’s state.
  69. //!
  70. //! Consumers of this class should use a set_state_*() method instead.
  71. void set_state(State state) { state_ = state; }
  72. private:
  73. // state_ is volatile to ensure that it’ll be set by the destructor when it
  74. // runs. Otherwise, optimizations might prevent it from ever being set to
  75. // kStateDestroyed, limiting this class’ ability to catch use-after-free
  76. // errors.
  77. volatile State state_;
  78. DISALLOW_COPY_AND_ASSIGN(InitializationState);
  79. };
  80. } // namespace crashpad
  81. #endif // CRASHPAD_UTIL_MISC_INITIALIZATION_INITIALIZATION_STATE_H_