/Users/deen/code/yugabyte-db/src/yb/gutil/spinlock_internal.cc
Line | Count | Source |
1 | | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
2 | | /* Copyright (c) 2010, Google Inc. |
3 | | * All rights reserved. |
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 | | * The following only applies to changes made to this file as part of YugaByte development. |
32 | | * |
33 | | * Portions Copyright (c) YugaByte, Inc. |
34 | | * |
35 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
36 | | * in compliance with the License. You may obtain a copy of the License at |
37 | | * |
38 | | * http://www.apache.org/licenses/LICENSE-2.0 |
39 | | * |
40 | | * Unless required by applicable law or agreed to in writing, software distributed under the License |
41 | | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
42 | | * or implied. See the License for the specific language governing permissions and limitations |
43 | | * under the License. |
44 | | * |
45 | | */ |
46 | | |
47 | | // The OS-specific header included below must provide two calls: |
48 | | // yb::base::internal::SpinLockDelay() and yb::base::internal::SpinLockWake(). |
49 | | // See spinlock_internal.h for the spec of SpinLockWake(). |
50 | | |
51 | | // void SpinLockDelay(volatile Atomic32 *w, int32 value, int loop) |
52 | | // SpinLockDelay() generates an apprproate spin delay on iteration "loop" of a |
53 | | // spin loop on location *w, whose previously observed value was "value". |
54 | | // SpinLockDelay() may do nothing, may yield the CPU, may sleep a clock tick, |
55 | | // or may wait for a delay that can be truncated by a call to SpinLockWake(w). |
56 | | // In all cases, it must return in bounded time even if SpinLockWake() is not |
57 | | // called. |
58 | | |
59 | | #include "yb/gutil/spinlock_internal.h" |
60 | | |
61 | | // forward declaration for use by spinlock_*-inl.h |
62 | | namespace yb { |
63 | | namespace base { |
64 | | namespace internal { |
65 | | static int SuggestedDelayNS(int loop); |
66 | | } // namespace internal |
67 | | } // namespace base |
68 | | } // namespace yb |
69 | | |
70 | | #if defined(_WIN32) |
71 | | #include "yb/gutil/spinlock_win32-inl.h" |
72 | | #elif defined(__linux__) |
73 | | #include "yb/gutil/spinlock_linux-inl.h" |
74 | | #else |
75 | | #include "yb/gutil/spinlock_posix-inl.h" |
76 | | #endif |
77 | | |
78 | | namespace yb { |
79 | | namespace base { |
80 | | namespace internal { |
81 | | |
82 | | // See spinlock_internal.h for spec. |
83 | | int32 SpinLockWait(volatile Atomic32 *w, int n, |
84 | 2.44k | const SpinLockWaitTransition trans[]) { |
85 | 2.44k | int32 v; |
86 | 2.44k | bool done = false; |
87 | 10.8k | for (int loop = 0; !done; loop++) { |
88 | 8.36k | v = ::base::subtle::Acquire_Load(w); |
89 | 8.36k | int i; |
90 | 27.9k | for (i = 0; i != n && v != trans[i].from; i++) { |
91 | 19.5k | } |
92 | 8.36k | if (i == n) { |
93 | 4.38k | SpinLockDelay(w, v, loop); // no matching transition |
94 | 3.97k | } else if (trans[i].to == v || // null transition |
95 | 3.43k | ::base::subtle::Acquire_CompareAndSwap(w, v, trans[i].to) == v) { |
96 | 3.43k | done = trans[i].done; |
97 | 3.43k | } |
98 | 8.36k | } |
99 | 2.44k | return v; |
100 | 2.44k | } |
101 | | |
102 | | // Return a suggested delay in nanoseconds for iteration number "loop" |
103 | 2.84M | static int SuggestedDelayNS(int loop) { |
104 | | // Weak pseudo-random number generator to get some spread between threads |
105 | | // when many are spinning. |
106 | 2.84M | #ifdef BASE_HAS_ATOMIC64 |
107 | 2.84M | static ::base::subtle::Atomic64 rand; |
108 | 2.84M | uint64 r = ::base::subtle::NoBarrier_Load(&rand); |
109 | 2.84M | r = 0x5deece66dLL * r + 0xb; // numbers from nrand48() |
110 | 2.84M | ::base::subtle::NoBarrier_Store(&rand, r); |
111 | | |
112 | 2.84M | r <<= 16; // 48-bit random number now in top 48-bits. |
113 | 2.84M | if (loop < 0 || loop > 32) { // limit loop to 0..32 |
114 | 253 | loop = 32; |
115 | 253 | } |
116 | | // loop>>3 cannot exceed 4 because loop cannot exceed 32. |
117 | | // Select top 20..24 bits of lower 48 bits, |
118 | | // giving approximately 0ms to 16ms. |
119 | | // Mean is exponential in loop for first 32 iterations, then 8ms. |
120 | | // The futex path multiplies this by 16, since we expect explicit wakeups |
121 | | // almost always on that path. |
122 | 2.84M | return static_cast<int>(r >> (44 - (loop >> 3))); |
123 | | #else |
124 | | static Atomic32 rand; |
125 | | uint32 r = ::base::subtle::NoBarrier_Load(&rand); |
126 | | r = 0x343fd * r + 0x269ec3; // numbers from MSVC++ |
127 | | ::base::subtle::NoBarrier_Store(&rand, r); |
128 | | |
129 | | r <<= 1; // 31-bit random number now in top 31-bits. |
130 | | if (loop < 0 || loop > 32) { // limit loop to 0..32 |
131 | | loop = 32; |
132 | | } |
133 | | // loop>>3 cannot exceed 4 because loop cannot exceed 32. |
134 | | // Select top 20..24 bits of lower 31 bits, |
135 | | // giving approximately 0ms to 16ms. |
136 | | // Mean is exponential in loop for first 32 iterations, then 8ms. |
137 | | // The futex path multiplies this by 16, since we expect explicit wakeups |
138 | | // almost always on that path. |
139 | | return r >> (12 - (loop >> 3)); |
140 | | #endif |
141 | 2.84M | } |
142 | | |
143 | | } // namespace internal |
144 | | } // namespace base |
145 | | } // namespace yb |