vecmem 1.14.0
Loading...
Searching...
No Matches
dummy_device_atomic_ref.ipp
1/*
2 * VecMem project, part of the ACTS project (R&D line)
3 *
4 * (c) 2022-2024 CERN for the benefit of the ACTS project
5 *
6 * Mozilla Public License Version 2.0
7 */
8#pragma once
9
10// Local include(s).
11#include "vecmem/memory/memory_order.hpp"
12#include "vecmem/utils/types.hpp"
13
14// System include(s).
15#include <type_traits>
16
17namespace vecmem {
18
19template <typename T, device_address_space address>
20VECMEM_HOST_AND_DEVICE
23
24template <typename T, device_address_space address>
25VECMEM_HOST_AND_DEVICE
29
30template <typename T, device_address_space address>
32 value_type data) const -> value_type {
33
34 store(data);
35 return data;
36}
37
38template <typename T, device_address_space address>
40 value_type data, memory_order order) const {
41
42 exchange(data, order);
43}
44
45template <typename T, device_address_space address>
47 memory_order order) const -> value_type {
48
49 value_type tmp = 0;
50 compare_exchange_strong(tmp, tmp, order);
51 return tmp;
52}
53
54template <typename T, device_address_space address>
56 value_type data, memory_order order) const -> value_type {
57
58 value_type tmp = load();
59 while (!compare_exchange_strong(tmp, data, order))
60 ;
61 return tmp;
62}
63
64template <typename T, device_address_space address>
65VECMEM_HOST_AND_DEVICE bool
68
69 if (order == memory_order::acq_rel) {
70 return compare_exchange_strong(expected, desired, order,
71 memory_order::acquire);
72 } else if (order == memory_order::release) {
73 return compare_exchange_strong(expected, desired, order,
74 memory_order::relaxed);
75 } else {
76 return compare_exchange_strong(expected, desired, order, order);
77 }
78}
79
80template <typename T, device_address_space address>
81VECMEM_HOST_AND_DEVICE bool
84
85 // This is **NOT** a sane implementation of CAS!
86 value_type old = *m_ptr;
87 if (old == expected) {
88 *m_ptr = desired;
89 return true;
90 } else {
91 expected = old;
92 return false;
93 }
94}
95
96template <typename T, device_address_space address>
98 value_type data, memory_order order) const -> value_type {
99
100 value_type tmp = load();
101 while (!compare_exchange_strong(tmp, tmp + data, order))
102 ;
103 return tmp;
104}
105
106template <typename T, device_address_space address>
108 value_type data, memory_order order) const -> value_type {
109
110 value_type tmp = load();
111 while (!compare_exchange_strong(tmp, tmp - data, order))
112 ;
113 return tmp;
114}
115
116template <typename T, device_address_space address>
118 value_type data, memory_order order) const -> value_type {
119
120 value_type tmp = load();
121 while (!compare_exchange_strong(tmp, tmp & data, order))
122 ;
123 return tmp;
124}
125
126template <typename T, device_address_space address>
128 value_type data, memory_order order) const -> value_type {
129
130 value_type tmp = load();
131 while (!compare_exchange_strong(tmp, tmp | data, order))
132 ;
133 return tmp;
134}
135
136template <typename T, device_address_space address>
138 value_type data, memory_order order) const -> value_type {
139
140 value_type tmp = load();
141 while (!compare_exchange_strong(tmp, tmp ^ data, order))
142 ;
143 return tmp;
144}
145
146} // namespace vecmem
Dummy / No-op atomic reference for unsupported devices / compilers.
Definition dummy_device_atomic_ref.hpp:27
VECMEM_HOST_AND_DEVICE value_type fetch_sub(value_type data, memory_order order=memory_order::seq_cst) const
Substitute a chosen value from the stored variable.
Definition dummy_device_atomic_ref.ipp:107
VECMEM_HOST_AND_DEVICE value_type exchange(value_type data, memory_order order=memory_order::seq_cst) const
Exchange the current value of the variable with a different one.
Definition dummy_device_atomic_ref.ipp:55
value_type & reference
Reference to a value given by the user.
Definition dummy_device_atomic_ref.hpp:40
VECMEM_HOST_AND_DEVICE void store(value_type data, memory_order order=memory_order::seq_cst) const
Set the variable to the desired value.
Definition dummy_device_atomic_ref.ipp:39
VECMEM_HOST_AND_DEVICE value_type fetch_add(value_type data, memory_order order=memory_order::seq_cst) const
Add a chosen value to the stored variable.
Definition dummy_device_atomic_ref.ipp:97
VECMEM_HOST_AND_DEVICE dummy_device_atomic_ref(reference ref)
Constructor, with a pointer to the managed variable.
Definition dummy_device_atomic_ref.ipp:21
dummy_device_atomic_ref & operator=(const dummy_device_atomic_ref &)=delete
Disable the assignment operator.
VECMEM_HOST_AND_DEVICE value_type load(memory_order order=memory_order::seq_cst) const
Get the value of the variable.
Definition dummy_device_atomic_ref.ipp:46
T value_type
Type managed by the object.
Definition dummy_device_atomic_ref.hpp:34
VECMEM_HOST_AND_DEVICE value_type fetch_and(value_type data, memory_order order=memory_order::seq_cst) const
Replace the current value with the specified value AND-ed to it.
Definition dummy_device_atomic_ref.ipp:117
VECMEM_HOST_AND_DEVICE value_type fetch_or(value_type data, memory_order order=memory_order::seq_cst) const
Replace the current value with the specified value OR-d to it.
Definition dummy_device_atomic_ref.ipp:127
VECMEM_HOST_AND_DEVICE bool compare_exchange_strong(reference expected, value_type desired, memory_order success, memory_order failure) const
Compare against the current value, and exchange only if different.
Definition dummy_device_atomic_ref.ipp:82
VECMEM_HOST_AND_DEVICE value_type fetch_xor(value_type data, memory_order order=memory_order::seq_cst) const
Replace the current value with the specified value XOR-d to it.
Definition dummy_device_atomic_ref.ipp:137
Main namespace for the vecmem classes/functions.
Definition atomic_ref.hpp:16
std::vector< T, vecmem::polymorphic_allocator< T > > vector
Alias type for vectors with our polymorphic allocator.
Definition vector.hpp:35
memory_order
Custom (dummy) definition for the memory order.
Definition memory_order.hpp:31