spin_lock.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #pragma once
  31. #include "core/os/thread.h"
  32. #include "core/typedefs.h"
  33. #ifdef THREADS_ENABLED
  34. // Note the implementations below avoid false sharing by ensuring their
  35. // sizes match the assumed cache line. We can't use align attributes
  36. // because these objects may end up unaligned in semi-tightly packed arrays.
  37. #ifdef _MSC_VER
  38. #include <intrin.h>
  39. #endif
  40. #if defined(__APPLE__)
  41. #include <os/lock.h>
  42. class SpinLock {
  43. union {
  44. mutable os_unfair_lock _lock = OS_UNFAIR_LOCK_INIT;
  45. char aligner[Thread::CACHE_LINE_BYTES];
  46. };
  47. public:
  48. _ALWAYS_INLINE_ void lock() const {
  49. os_unfair_lock_lock(&_lock);
  50. }
  51. _ALWAYS_INLINE_ void unlock() const {
  52. os_unfair_lock_unlock(&_lock);
  53. }
  54. };
  55. #else // __APPLE__
  56. #include <atomic>
  57. _ALWAYS_INLINE_ static void _cpu_pause() {
  58. #if defined(_MSC_VER)
  59. // ----- MSVC.
  60. #if defined(_M_ARM) || defined(_M_ARM64) // ARM.
  61. __yield();
  62. #elif defined(_M_IX86) || defined(_M_X64) // x86.
  63. _mm_pause();
  64. #endif
  65. #elif defined(__GNUC__) || defined(__clang__)
  66. // ----- GCC/Clang.
  67. #if defined(__i386__) || defined(__x86_64__) // x86.
  68. __builtin_ia32_pause();
  69. #elif defined(__arm__) || defined(__aarch64__) // ARM.
  70. asm volatile("yield");
  71. #elif defined(__powerpc__) // PowerPC.
  72. asm volatile("or 27,27,27");
  73. #elif defined(__riscv) // RISC-V.
  74. asm volatile(".insn i 0x0F, 0, x0, x0, 0x010");
  75. #endif
  76. #endif
  77. }
  78. static_assert(std::atomic_bool::is_always_lock_free);
  79. class SpinLock {
  80. union {
  81. mutable std::atomic<bool> locked = ATOMIC_VAR_INIT(false);
  82. char aligner[Thread::CACHE_LINE_BYTES];
  83. };
  84. public:
  85. _ALWAYS_INLINE_ void lock() const {
  86. while (true) {
  87. bool expected = false;
  88. if (locked.compare_exchange_weak(expected, true, std::memory_order_acquire, std::memory_order_relaxed)) {
  89. break;
  90. }
  91. do {
  92. _cpu_pause();
  93. } while (locked.load(std::memory_order_relaxed));
  94. }
  95. }
  96. _ALWAYS_INLINE_ void unlock() const {
  97. locked.store(false, std::memory_order_release);
  98. }
  99. };
  100. #endif // __APPLE__
  101. #else // THREADS_ENABLED
  102. class SpinLock {
  103. public:
  104. void lock() const {}
  105. void unlock() const {}
  106. };
  107. #endif // THREADS_ENABLED