Debian Codegraph @

thirdparty/jolt_physics/Jolt/Core/Atomics.h

raw symbols SCIP
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
7 JPH_SUPPRESS_WARNINGS_STD_BEGIN
8 #include <atomic>
9 JPH_SUPPRESS_WARNINGS_STD_END
10
11 JPH_NAMESPACE_BEGIN
12
13 // Things we're using from STL
14 using std::atomic;
15 using std::memory_order;
16 using std::memory_order_relaxed;
17 using std::memory_order_acquire;
18 using std::memory_order_release;
19 using std::memory_order_acq_rel;
20 using std::memory_order_seq_cst;
21
22 /// Atomically compute the min(ioAtomic, inValue) and store it in ioAtomic, returns true if value was updated
23 template <class T>
24 bool 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
33 /// Atomically compute the max(ioAtomic, inValue) and store it in ioAtomic, returns true if value was updated
34 template <class T>
35 bool 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
44 JPH_NAMESPACE_END