spin_lock.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**************************************************************************/
  2. /* spin_lock.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef SPIN_LOCK_H
  31. #define SPIN_LOCK_H
  32. #include "core/os/thread.h"
  33. #include "core/typedefs.h"
  34. #ifdef THREADS_ENABLED
  35. // Note the implementations below avoid false sharing by ensuring their
  36. // sizes match the assumed cache line. We can't use align attributes
  37. // because these objects may end up unaligned in semi-tightly packed arrays.
  38. #ifdef _MSC_VER
  39. #include <intrin.h>
  40. #endif
  41. #if defined(__APPLE__)
  42. #include <os/lock.h>
  43. class SpinLock {
  44. union {
  45. mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
  46. char aligner[Thread::CACHE_LINE_BYTES];
  47. };
  48. public:
  49. _ALWAYS_INLINE_ void lock() const {
  50. os_unfair_lock_lock(&_lock);
  51. }
  52. _ALWAYS_INLINE_ void unlock() const {
  53. os_unfair_lock_unlock(&_lock);
  54. }
  55. };
  56. #else // __APPLE__
  57. #include <atomic>
  58. _ALWAYS_INLINE_ static void _cpu_pause() {
  59. #if defined(_MSC_VER)
  60. // ----- MSVC.
  61. #if defined(_M_ARM) || defined(_M_ARM64) // ARM.
  62. __yield();
  63. #elif defined(_M_IX86) || defined(_M_X64) // x86.
  64. _mm_pause();
  65. #endif
  66. #elif defined(__GNUC__) || defined(__clang__)
  67. // ----- GCC/Clang.
  68. #if defined(__i386__) || defined(__x86_64__) // x86.
  69. __builtin_ia32_pause();
  70. #elif defined(__arm__) || defined(__aarch64__) // ARM.
  71. asm volatile("yield");
  72. #elif defined(__powerpc__) || defined(__ppc__) || defined(__PPC__) // PowerPC.
  73. asm volatile("or 27,27,27");
  74. #elif defined(__riscv) // RISC-V.
  75. asm volatile(".insn i 0x0F, 0, x0, x0, 0x010");
  76. #endif
  77. #endif
  78. }
  79. static_assert(std::atomic_bool::is_always_lock_free);
  80. class SpinLock {
  81. union {
  82. mutable std::atomic<bool> locked = ATOMIC_VAR_INIT(false);
  83. char aligner[Thread::CACHE_LINE_BYTES];
  84. };
  85. public:
  86. _ALWAYS_INLINE_ void lock() const {
  87. while (true) {
  88. bool expected = false;
  89. if (locked.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
  90. break;
  91. }
  92. do {
  93. _cpu_pause();
  94. } while (locked.load(std::memory_order_relaxed));
  95. }
  96. }
  97. _ALWAYS_INLINE_ void unlock() const {
  98. locked.store(false, std::memory_order_release);
  99. }
  100. };
  101. #endif // __APPLE__
  102. #else // THREADS_ENABLED
  103. class SpinLock {
  104. public:
  105. void lock() const {}
  106. void unlock() const {}
  107. };
  108. #endif // THREADS_ENABLED
  109. #endif // SPIN_LOCK_H