/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/stubs/once.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 123 lines · 35 code · 18 blank · 70 comment · 1 complexity · 966f7ffb5c4a7b166e9264e9b7e827d1 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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 three things:
  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
  44. // or class member variables of this type).
  45. // * A function GogoleOnceInit(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. //
  50. // This implements a way to perform lazy initialization. It's more efficient
  51. // than using mutexes as no lock is needed if initialization has already
  52. // happened.
  53. //
  54. // Example usage:
  55. // void Init();
  56. // GOOGLE_PROTOBUF_DECLARE_ONCE(once_init);
  57. //
  58. // // Calls Init() exactly once.
  59. // void InitOnce() {
  60. // GoogleOnceInit(&once_init, &Init);
  61. // }
  62. //
  63. // Note that if GoogleOnceInit() is called before main() has begun, it must
  64. // only be called by the thread that will eventually call main() -- that is,
  65. // the thread that performs dynamic initialization. In general this is a safe
  66. // assumption since people don't usually construct threads before main() starts,
  67. // but it is technically not guaranteed. Unfortunately, Win32 provides no way
  68. // whatsoever to statically-initialize its synchronization primitives, so our
  69. // only choice is to assume that dynamic initialization is single-threaded.
  70. #ifndef GOOGLE_PROTOBUF_STUBS_ONCE_H__
  71. #define GOOGLE_PROTOBUF_STUBS_ONCE_H__
  72. #include <google/protobuf/stubs/common.h>
  73. #ifndef _WIN32
  74. #include <pthread.h>
  75. #endif
  76. namespace google {
  77. namespace protobuf {
  78. #ifdef _WIN32
  79. struct ProtobufOnceInternal;
  80. struct LIBPROTOBUF_EXPORT ProtobufOnceType {
  81. ProtobufOnceType();
  82. ~ProtobufOnceType();
  83. void Init(void (*init_func)());
  84. volatile bool initialized_;
  85. ProtobufOnceInternal* internal_;
  86. };
  87. #define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \
  88. ::google::protobuf::ProtobufOnceType NAME
  89. inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) {
  90. // Note: Double-checked locking is safe on x86.
  91. if (!once->initialized_) {
  92. once->Init(init_func);
  93. }
  94. }
  95. #else
  96. typedef pthread_once_t ProtobufOnceType;
  97. #define GOOGLE_PROTOBUF_DECLARE_ONCE(NAME) \
  98. pthread_once_t NAME = PTHREAD_ONCE_INIT
  99. inline void GoogleOnceInit(ProtobufOnceType* once, void (*init_func)()) {
  100. pthread_once(once, init_func);
  101. }
  102. #endif
  103. } // namespace protobuf
  104. } // namespace google
  105. #endif // GOOGLE_PROTOBUF_STUBS_ONCE_H__