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