once.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. //
  32. // emulates google3/base/once.h
  33. //
  34. // This header is intended to be included only by internal .cc files and
  35. // generated .pb.cc files. Users should not use this directly.
  36. //
  37. // This is basically a portable version of pthread_once().
  38. //
  39. // This header declares:
  40. // * A type called ProtobufOnceType.
  41. // * A macro GOOGLE_PROTOBUF_DECLARE_ONCE() which declares a variable of type
  42. // ProtobufOnceType. This is the only legal way to declare such a variable.
  43. // The macro may only be used at the global scope (you cannot create local or
  44. // class member variables of this type).
  45. // * A function GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()).
  46. // This function, when invoked multiple times given the same ProtobufOnceType
  47. // object, will invoke init_func on the first call only, and will make sure
  48. // none of the calls return before that first call to init_func has finished.
  49. // * The user can provide a parameter which GoogleOnceInit() forwards to the
  50. // user-provided function when it is called. Usage example:
  51. // int a = 10;
  52. // GoogleOnceInit(&my_once, &MyFunctionExpectingIntArgument, &a);
  53. // * This implementation guarantees that ProtobufOnceType is a POD (i.e. no
  54. // static initializer generated).
  55. //
  56. // This implements a way to perform lazy initialization. It's more efficient
  57. // than using mutexes as no lock is needed if initialization has already
  58. // happened.
  59. //
  60. // Example usage:
  61. // void Init();
  62. // GOOGLE_PROTOBUF_DECLARE_ONCE(once_init);
  63. //
  64. // // Calls Init() exactly once.
  65. // void InitOnce() {
  66. // GoogleOnceInit(&once_init, &Init);
  67. // }
  68. //
  69. // Note that if GoogleOnceInit() is called before main() has begun, it must
  70. // only be called by the thread that will eventually call main() -- that is,
  71. // the thread that performs dynamic initialization. In general this is a safe
  72. // assumption since people don't usually construct threads before main() starts,
  73. // but it is technically not guaranteed. Unfortunately, Win32 provides no way
  74. // whatsoever to statically-initialize its synchronization primitives, so our
  75. // only choice is to assume that dynamic initialization is single-threaded.
  76. #ifndef GOOGLE_PROTOBUF_STUBS_ONCE_H__
  77. #define GOOGLE_PROTOBUF_STUBS_ONCE_H__
  78. #include <mutex>
  79. #include <utility>
  80. namespace google {
  81. namespace protobuf {
  82. namespace internal {
  83. using once_flag = std::once_flag;
  84. template <typename... Args>
  85. void call_once(Args&&... args ) {
  86. std::call_once(std::forward<Args>(args)...);
  87. }
  88. } // namespace internal
  89. // TODO(gerbens) remove this once third_party is fully extracted
  90. using ProtobufOnceType = internal::once_flag;
  91. inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) {
  92. std::call_once(*once, init_func);
  93. }
  94. template <typename Arg>
  95. inline void GoogleOnceInitArg(ProtobufOnceType* once, void (*init_func)(Arg*),
  96. Arg* arg) {
  97. std::call_once(*once, init_func, arg);
  98. }
  99. class GoogleOnceDynamic {
  100. public:
  101. // If this->Init() has not been called before by any thread,
  102. // execute (*func_with_arg)(arg) then return.
  103. // Otherwise, wait until that prior invocation has finished
  104. // executing its function, then return.
  105. template<typename T>
  106. void Init(void (*func_with_arg)(T*), T* arg) {
  107. GoogleOnceInitArg<T>(&this->state_, func_with_arg, arg);
  108. }
  109. private:
  110. ProtobufOnceType state_;
  111. };
  112. #define GOOGLE_PROTOBUF_ONCE_TYPE ::google::protobuf::ProtobufOnceType
  113. #define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \
  114. ::google::protobuf::ProtobufOnceType NAME
  115. } // namespace protobuf
  116. } // namespace google
  117. #endif // GOOGLE_PROTOBUF_STUBS_ONCE_H__