once.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <google/protobuf/stubs/atomicops.h>
  79. #include <google/protobuf/stubs/callback.h>
  80. #include <google/protobuf/stubs/common.h>
  81. namespace google {
  82. namespace protobuf {
  83. #ifdef GOOGLE_PROTOBUF_NO_THREAD_SAFETY
  84. typedef bool ProtobufOnceType;
  85. #define GOOGLE_PROTOBUF_ONCE_INIT false
  86. inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) {
  87. if (!*once) {
  88. *once = true;
  89. init_func();
  90. }
  91. }
  92. template <typename Arg>
  93. inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)(Arg),
  94. Arg arg) {
  95. if (!*once) {
  96. *once = true;
  97. init_func(arg);
  98. }
  99. }
  100. #else
  101. enum {
  102. ONCE_STATE_UNINITIALIZED = 0,
  103. ONCE_STATE_EXECUTING_CLOSURE = 1,
  104. ONCE_STATE_DONE = 2
  105. };
  106. typedef internal::AtomicWord ProtobufOnceType;
  107. #define GOOGLE_PROTOBUF_ONCE_INIT ::google::protobuf::ONCE_STATE_UNINITIALIZED
  108. LIBPROTOBUF_EXPORT
  109. void GoogleOnceInitImpl(ProtobufOnceType* once, Closure* closure);
  110. inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) {
  111. if (internal::Acquire_Load(once) != ONCE_STATE_DONE) {
  112. internal::FunctionClosure0 func(init_func, false);
  113. GoogleOnceInitImpl(once, &func);
  114. }
  115. }
  116. template <typename Arg>
  117. inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)(Arg*),
  118. Arg* arg) {
  119. if (internal::Acquire_Load(once) != ONCE_STATE_DONE) {
  120. internal::FunctionClosure1<Arg*> func(init_func, false, arg);
  121. GoogleOnceInitImpl(once, &func);
  122. }
  123. }
  124. #endif // GOOGLE_PROTOBUF_NO_THREAD_SAFETY
  125. class GoogleOnceDynamic {
  126. public:
  127. GoogleOnceDynamic() : state_(GOOGLE_PROTOBUF_ONCE_INIT) { }
  128. // If this->Init() has not been called before by any thread,
  129. // execute (*func_with_arg)(arg) then return.
  130. // Otherwise, wait until that prior invocation has finished
  131. // executing its function, then return.
  132. template<typename T>
  133. void Init(void (*func_with_arg)(T*), T* arg) {
  134. GoogleOnceInit<T>(&this->state_,
  135. func_with_arg,
  136. arg);
  137. }
  138. private:
  139. ProtobufOnceType state_;
  140. };
  141. #define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \
  142. ::google::protobuf::ProtobufOnceType NAME = GOOGLE_PROTOBUF_ONCE_INIT
  143. } // namespace protobuf
  144. } // namespace google
  145. #endif // GOOGLE_PROTOBUF_STUBS_ONCE_H__