Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Atomics.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
8#include <atomic>
10
12
13// Things we're using from STL
14using std::atomic;
15using std::memory_order;
16using std::memory_order_relaxed;
17using std::memory_order_acquire;
18using std::memory_order_release;
19using std::memory_order_acq_rel;
20using std::memory_order_seq_cst;
21
23template <class T>
24bool AtomicMin(atomic<T> &ioAtomic, const T inValue, const memory_order inMemoryOrder = memory_order_seq_cst)
25{
26 T cur_value = ioAtomic.load(memory_order_relaxed);
27 while (cur_value > inValue)
28 if (ioAtomic.compare_exchange_weak(cur_value, inValue, inMemoryOrder))
29 return true;
30 return false;
31}
32
34template <class T>
35bool AtomicMax(atomic<T> &ioAtomic, const T inValue, const memory_order inMemoryOrder = memory_order_seq_cst)
36{
37 T cur_value = ioAtomic.load(memory_order_relaxed);
38 while (cur_value < inValue)
39 if (ioAtomic.compare_exchange_weak(cur_value, inValue, inMemoryOrder))
40 return true;
41 return false;
42}
43
bool AtomicMax(atomic< T > &ioAtomic, const T inValue, const memory_order inMemoryOrder=memory_order_seq_cst)
Atomically compute the max(ioAtomic, inValue) and store it in ioAtomic, returns true if value was upd...
Definition: Atomics.h:35
bool AtomicMin(atomic< T > &ioAtomic, const T inValue, const memory_order inMemoryOrder=memory_order_seq_cst)
Atomically compute the min(ioAtomic, inValue) and store it in ioAtomic, returns true if value was upd...
Definition: Atomics.h:24
#define JPH_SUPPRESS_WARNINGS_STD_BEGIN
Definition: Core.h:372
#define JPH_SUPPRESS_WARNINGS_STD_END
Definition: Core.h:384
#define JPH_NAMESPACE_END
Definition: Core.h:367
#define JPH_NAMESPACE_BEGIN
Definition: Core.h:361