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