Path: blob/master/thirdparty/jolt_physics/Jolt/Core/Atomics.h
9906 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2021 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56JPH_SUPPRESS_WARNINGS_STD_BEGIN7#include <atomic>8JPH_SUPPRESS_WARNINGS_STD_END910JPH_NAMESPACE_BEGIN1112// Things we're using from STL13using std::atomic;14using std::memory_order;15using std::memory_order_relaxed;16using std::memory_order_acquire;17using std::memory_order_release;18using std::memory_order_acq_rel;19using std::memory_order_seq_cst;2021/// Atomically compute the min(ioAtomic, inValue) and store it in ioAtomic, returns true if value was updated22template <class T>23bool AtomicMin(atomic<T> &ioAtomic, const T inValue, const memory_order inMemoryOrder = memory_order_seq_cst)24{25T cur_value = ioAtomic.load(memory_order_relaxed);26while (cur_value > inValue)27if (ioAtomic.compare_exchange_weak(cur_value, inValue, inMemoryOrder))28return true;29return false;30}3132/// Atomically compute the max(ioAtomic, inValue) and store it in ioAtomic, returns true if value was updated33template <class T>34bool AtomicMax(atomic<T> &ioAtomic, const T inValue, const memory_order inMemoryOrder = memory_order_seq_cst)35{36T cur_value = ioAtomic.load(memory_order_relaxed);37while (cur_value < inValue)38if (ioAtomic.compare_exchange_weak(cur_value, inValue, inMemoryOrder))39return true;40return false;41}4243JPH_NAMESPACE_END444546