spinlock_internal.cc 4.2 KB

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