once.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include <google/protobuf/stubs/once.h>
  37. #ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY
  38. #ifdef _WIN32
  39. #include <windows.h>
  40. #else
  41. #include <sched.h>
  42. #endif
  43. #include <google/protobuf/stubs/atomicops.h>
  44. namespace google {
  45. namespace protobuf {
  46. namespace {
  47. void SchedYield() {
  48. #ifdef _WIN32
  49. Sleep(0);
  50. #else // POSIX
  51. sched_yield();
  52. #endif
  53. }
  54. } // namespace
  55. void GoogleOnceInitImpl(ProtobufOnceType* once, Closure* closure) {
  56. internal::AtomicWord state = internal::Acquire_Load(once);
  57. // Fast path. The provided closure was already executed.
  58. if (state == ONCE_STATE_DONE) {
  59. return;
  60. }
  61. // The closure execution did not complete yet. The once object can be in one
  62. // of the two following states:
  63. // - UNINITIALIZED: We are the first thread calling this function.
  64. // - EXECUTING_CLOSURE: Another thread is already executing the closure.
  65. //
  66. // First, try to change the state from UNINITIALIZED to EXECUTING_CLOSURE
  67. // atomically.
  68. state = internal::Acquire_CompareAndSwap(
  69. once, ONCE_STATE_UNINITIALIZED, ONCE_STATE_EXECUTING_CLOSURE);
  70. if (state == ONCE_STATE_UNINITIALIZED) {
  71. // We are the first thread to call this function, so we have to call the
  72. // closure.
  73. closure->Run();
  74. internal::Release_Store(once, ONCE_STATE_DONE);
  75. } else {
  76. // Another thread has already started executing the closure. We need to
  77. // wait until it completes the initialization.
  78. while (state == ONCE_STATE_EXECUTING_CLOSURE) {
  79. // Note that futex() could be used here on Linux as an improvement.
  80. SchedYield();
  81. state = internal::Acquire_Load(once);
  82. }
  83. }
  84. }
  85. } // namespace protobuf
  86. } // namespace google
  87. #endif // GOOGLE_PROTOBUF_NO_THREAD_SAFETY