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
31// Author: kenton@google.com (Kenton Varda)
32//
33// emulates google3/base/once.h
34//
35// This header is intended to be included only by internal .cc files and
36// generated .pb.cc files.  Users should not use this directly.
37
38#include <google/protobuf/stubs/once.h>
39
40#ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY
41
42#ifdef _WIN32
43#include <windows.h>
44#else
45#include <sched.h>
46#endif
47
48#include <google/protobuf/stubs/atomicops.h>
49
50namespace google {
51namespace protobuf {
52
53namespace {
54
55void SchedYield() {
56#ifdef _WIN32
57  Sleep(0);
58#else  // POSIX
59  sched_yield();
60#endif
61}
62
63}  // namespace
64
65void GoogleOnceInitImpl(ProtobufOnceType* once, Closure* closure) {
66  internal::AtomicWord state = internal::Acquire_Load(once);
67  // Fast path. The provided closure was already executed.
68  if (state == ONCE_STATE_DONE) {
69    return;
70  }
71  // The closure execution did not complete yet. The once object can be in one
72  // of the two following states:
73  //   - UNINITIALIZED: We are the first thread calling this function.
74  //   - EXECUTING_CLOSURE: Another thread is already executing the closure.
75  //
76  // First, try to change the state from UNINITIALIZED to EXECUTING_CLOSURE
77  // atomically.
78  state = internal::Acquire_CompareAndSwap(
79      once, ONCE_STATE_UNINITIALIZED, ONCE_STATE_EXECUTING_CLOSURE);
80  if (state == ONCE_STATE_UNINITIALIZED) {
81    // We are the first thread to call this function, so we have to call the
82    // closure.
83    closure->Run();
84    internal::Release_Store(once, ONCE_STATE_DONE);
85  } else {
86    // Another thread has already started executing the closure. We need to
87    // wait until it completes the initialization.
88    while (state == ONCE_STATE_EXECUTING_CLOSURE) {
89      // Note that futex() could be used here on Linux as an improvement.
90      SchedYield();
91      state = internal::Acquire_Load(once);
92    }
93  }
94}
95
96}  // namespace protobuf
97}  // namespace google
98
99#endif  // GOOGLE_PROTOBUF_NO_THREAD_SAFETY
100