spinlock.cuh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /******************************************************************************
  2. * Copyright (c) 2011, Duane Merrill. All rights reserved.
  3. * Copyright (c) 2011-2014, NVIDIA CORPORATION. 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 met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the NVIDIA CORPORATION nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. ******************************************************************************/
  28. /**
  29. * \file
  30. * Simple x86/x64 atomic spinlock, portable across MS Windows (cl.exe) & Linux (g++)
  31. */
  32. #pragma once
  33. #if defined(_WIN32) || defined(_WIN64)
  34. #include <intrin.h>
  35. #include <windows.h>
  36. #undef small // Windows is terrible for polluting macro namespace
  37. /**
  38. * Compiler read/write barrier
  39. */
  40. #pragma intrinsic(_ReadWriteBarrier)
  41. #endif
  42. #include "../util_namespace.cuh"
  43. /// Optional outer namespace(s)
  44. CUB_NS_PREFIX
  45. /// CUB namespace
  46. namespace cub {
  47. #if defined(_MSC_VER)
  48. // Microsoft VC++
  49. typedef long Spinlock;
  50. #else
  51. // GNU g++
  52. typedef int Spinlock;
  53. /**
  54. * Compiler read/write barrier
  55. */
  56. __forceinline__ void _ReadWriteBarrier()
  57. {
  58. __sync_synchronize();
  59. }
  60. /**
  61. * Atomic exchange
  62. */
  63. __forceinline__ long _InterlockedExchange(volatile int * const Target, const int Value)
  64. {
  65. // NOTE: __sync_lock_test_and_set would be an acquire barrier, so we force a full barrier
  66. _ReadWriteBarrier();
  67. return __sync_lock_test_and_set(Target, Value);
  68. }
  69. /**
  70. * Pause instruction to prevent excess processor bus usage
  71. */
  72. __forceinline__ void YieldProcessor()
  73. {
  74. #ifndef __arm__
  75. asm volatile("pause\n": : :"memory");
  76. #endif // __arm__
  77. }
  78. #endif // defined(_MSC_VER)
  79. /**
  80. * Return when the specified spinlock has been acquired
  81. */
  82. __forceinline__ void Lock(volatile Spinlock *lock)
  83. {
  84. while (1)
  85. {
  86. if (!_InterlockedExchange(lock, 1)) return;
  87. while (*lock) YieldProcessor();
  88. }
  89. }
  90. /**
  91. * Release the specified spinlock
  92. */
  93. __forceinline__ void Unlock(volatile Spinlock *lock)
  94. {
  95. _ReadWriteBarrier();
  96. *lock = 0;
  97. }
  98. } // CUB namespace
  99. CUB_NS_POSTFIX // Optional outer namespace(s)