once.h 6.0 KB

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