/** * Looking Glass * Copyright © 2017-2026 The Looking Glass Authors * https://looking-glass.io * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., 59 * Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #pragma once #include "Seq.h" #include #include #include namespace Atomic { namespace Detail { inline volatile LONG * Ptr(uint32_t& value) { static_assert(sizeof(value) == sizeof(LONG), "atomic value must match the Windows interlocked width"); return reinterpret_cast(&value); } } template T Load(const std::atomic& value, std::memory_order order = std::memory_order_seq_cst) { return value.load(order); } template void Store(std::atomic& value, U data, std::memory_order order = std::memory_order_seq_cst) { value.store(static_cast(data), order); } template T Swap(std::atomic& value, U data, std::memory_order order = std::memory_order_seq_cst) { return value.exchange(static_cast(data), order); } template T FetchAdd(std::atomic& value, U data, std::memory_order order = std::memory_order_seq_cst) { return value.fetch_add(static_cast(data), order); } template T FetchSub(std::atomic& value, U data, std::memory_order order = std::memory_order_seq_cst) { return value.fetch_sub(static_cast(data), order); } template bool CAS(std::atomic& value, T& expected, U data, std::memory_order order = std::memory_order_seq_cst) { return value.compare_exchange_strong( expected, static_cast(data), order); } template bool CAS(std::atomic& value, T& expected, U data, std::memory_order success, std::memory_order failure) { return value.compare_exchange_strong( expected, static_cast(data), success, failure); } template bool CASWeak(std::atomic& value, T& expected, U data, std::memory_order order = std::memory_order_seq_cst) { return value.compare_exchange_weak( expected, static_cast(data), order); } template bool CASWeak(std::atomic& value, T& expected, U data, std::memory_order success, std::memory_order failure) { return value.compare_exchange_weak( expected, static_cast(data), success, failure); } template T Inc(std::atomic& value, std::memory_order order = std::memory_order_seq_cst) { return FetchAdd(value, static_cast(1), order) + 1; } template T Next(std::atomic& value, std::memory_order order = std::memory_order_relaxed) { T current = Load(value, std::memory_order_relaxed); for (;;) { const T next = Seq::Next(current); if (CASWeak(value, current, next, order)) return next; } } inline uint32_t Load(uint32_t& value) { return static_cast(InterlockedCompareExchange( Detail::Ptr(value), 0, 0)); } inline void Store(uint32_t& value, uint32_t data) { InterlockedExchange(Detail::Ptr(value), static_cast(data)); } inline uint32_t Swap(uint32_t& value, uint32_t data) { return static_cast( InterlockedExchange(Detail::Ptr(value), static_cast(data))); } inline uint32_t FetchAdd(uint32_t& value, uint32_t data) { return static_cast( InterlockedExchangeAdd(Detail::Ptr(value), static_cast(data))); } inline uint32_t FetchSub(uint32_t& value, uint32_t data) { return static_cast(InterlockedExchangeAdd( Detail::Ptr(value), static_cast(0U - data))); } inline bool CAS(uint32_t& value, uint32_t expected, uint32_t data) { const uint32_t actual = static_cast(InterlockedCompareExchange( Detail::Ptr(value), static_cast(data), static_cast(expected))); return actual == expected; } inline bool CASWeak(uint32_t& value, uint32_t expected, uint32_t data) { return CAS(value, expected, data); } inline uint32_t Inc(uint32_t& value) { return static_cast(InterlockedIncrement(Detail::Ptr(value))); } inline uint32_t Next(uint32_t& value, uint32_t step = 1) { uint32_t result = FetchAdd(value, step) + step; if (!result) result = FetchAdd(value, step) + step; return result; } inline void Fence() { MemoryBarrier(); } }