| 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 |
|
| 32 |
// The OS-specific header included below must provide two calls: |
| 33 |
// base::internal::SpinLockDelay() and base::internal::SpinLockWake(). |
| 34 |
// See spinlock_internal.h for the spec of SpinLockWake(). |
| 35 |
|
| 36 |
// void SpinLockDelay(std::atomic<int> *w, int32 value, int loop) |
| 37 |
// SpinLockDelay() generates an apprproate spin delay on iteration "loop" of a |
| 38 |
// spin loop on location *w, whose previously observed value was "value". |
| 39 |
// SpinLockDelay() may do nothing, may yield the CPU, may sleep a clock tick, |
| 40 |
// or may wait for a delay that can be truncated by a call to SpinlockWake(w). |
| 41 |
// In all cases, it must return in bounded time even if SpinlockWake() is not |
| 42 |
// called. |
| 43 |
|
| 44 |
#include "base/spinlock_internal.h" |
| 45 |
|
| 46 |
// forward declaration for use by spinlock_*-inl.h |
| 47 |
namespace base { namespace internal { static int SuggestedDelayNS(int loop); }} |
| 48 |
|
| 49 |
#if defined(_WIN32) |
| 50 |
#include "base/spinlock_win32-inl.h" |
| 51 |
#elif defined(__linux__) |
| 52 |
#include "base/spinlock_linux-inl.h" |
| 53 |
#else |
| 54 |
#include "base/spinlock_posix-inl.h" |
| 55 |
#endif |
| 56 |
|
| 57 |
namespace base { |
| 58 |
namespace internal { |
| 59 |
|
| 60 |
// Return a suggested delay in nanoseconds for iteration number "loop" |
| 61 |
static int SuggestedDelayNS(int loop) { |
| 62 |
// Weak pseudo-random number generator to get some spread between threads |
| 63 |
// when many are spinning. |
| 64 |
static volatile uint64_t rand; |
| 65 |
uint64 r = rand; |
| 66 |
r = 0x5deece66dLL * r + 0xb; // numbers from nrand48() |
| 67 |
rand = r; |
| 68 |
|
| 69 |
r <<= 16; // 48-bit random number now in top 48-bits. |
| 70 |
if (loop < 0 || loop > 32) { // limit loop to 0..32 |
| 71 |
loop = 32; |
| 72 |
} |
| 73 |
// loop>>3 cannot exceed 4 because loop cannot exceed 32. |
| 74 |
// Select top 20..24 bits of lower 48 bits, |
| 75 |
// giving approximately 0ms to 16ms. |
| 76 |
// Mean is exponential in loop for first 32 iterations, then 8ms. |
| 77 |
// The futex path multiplies this by 16, since we expect explicit wakeups |
| 78 |
// almost always on that path. |
| 79 |
return r >> (44 - (loop >> 3)); |
| 80 |
} |
| 81 |
|
| 82 |
} // namespace internal |
| 83 |
} // namespace base |