Advanced Multi-Physics (AMP)
On-Line Documentation
AtomicList.h
Go to the documentation of this file.
1#ifndef included_AMP_AtomicList
2#define included_AMP_AtomicList
3
4#include <atomic>
5#include <csignal>
6#include <functional>
7
8
9namespace AMP {
10
11
18template<class TYPE, class COMPARE = std::less<TYPE>>
19class AtomicList final
20{
21public:
23 AtomicList( size_t capacity = 1024,
24 const TYPE &default_value = TYPE(),
25 const COMPARE &comp = COMPARE() );
26
29
42 template<typename Compare, class... Args>
43 inline TYPE remove( Compare compare, const Args &...args );
44
46 inline TYPE remove_first();
47
53 inline void insert( const TYPE &x );
54
59 inline size_t size() const { return d_N.load(); }
60
65 inline bool empty() const { return d_N.load() == 0; }
66
71 inline void clear()
72 {
73 while ( !empty() )
75 }
76
81 inline constexpr size_t capacity() const { return d_capacity; }
82
87 inline constexpr size_t available() const { return d_capacity - size(); }
88
89
98 inline bool check();
99
100
102 inline size_t N_insert() const { return d_N_insert.load(); }
103
104
106 inline size_t N_remove() const { return d_N_remove.load(); }
107
108private:
109 // Data members
110 COMPARE d_compare;
111 const size_t d_capacity;
112 volatile TYPE d_default;
113 volatile TYPE *d_objects;
114 volatile std::atomic_int32_t d_N;
115 volatile std::atomic_int32_t *d_next;
116 volatile std::atomic_int32_t d_unused;
117 volatile std::atomic_int64_t d_N_insert;
118 volatile std::atomic_int64_t d_N_remove;
119
120private:
121 inline int lock( int i )
122 {
123 if ( i == -1 )
124 return -1;
125 int tmp = 0;
126 do {
127 tmp = d_next[i].fetch_and( 0 );
128 } while ( tmp == 0 );
129 return tmp;
130 }
131 inline void unlock( int i, int value )
132 {
133 if ( i != -1 )
134 d_next[i].fetch_or( value );
135 }
136 inline int get_unused()
137 {
138 int i = 0;
139 do {
140 i = d_unused.fetch_and( 0 );
141 } while ( i == 0 );
142 d_unused.fetch_or( -( d_next[i] + 4 ) + 1 );
143 d_next[i] = -3;
144 return i;
145 }
146 inline void put_unused( int i )
147 {
148 int j = 0;
149 do {
150 j = d_unused.exchange( j );
151 } while ( j == 0 );
152 d_next[i] = -3 - j;
153 d_unused.fetch_or( i );
154 }
155
156
157public:
158 AtomicList( const AtomicList & ) = delete;
159 AtomicList &operator=( const AtomicList & ) = delete;
160};
161
162
163} // namespace AMP
164
165#include "AMP/utils/threadpool/AtomicList.hpp"
166
167#endif
Maintain a sorted list of entries.
Definition AtomicList.h:20
size_t size() const
Return the size of the list.
Definition AtomicList.h:59
volatile std::atomic_int64_t d_N_insert
Definition AtomicList.h:117
TYPE remove(Compare compare, const Args &...args)
Remove an item from the list.
size_t N_insert() const
Return the total number of inserts since object creation.
Definition AtomicList.h:102
~AtomicList()
Destructor.
COMPARE d_compare
Definition AtomicList.h:110
TYPE remove_first()
Remove the first from the list.
AtomicList(const AtomicList &)=delete
volatile std::atomic_int32_t d_unused
Definition AtomicList.h:116
void put_unused(int i)
Definition AtomicList.h:146
void insert(const TYPE &x)
Insert an item.
int lock(int i)
Definition AtomicList.h:121
constexpr size_t available() const
Return the available space.
Definition AtomicList.h:87
void clear()
Clear the list.
Definition AtomicList.h:71
volatile std::atomic_int32_t * d_next
Definition AtomicList.h:115
size_t N_remove() const
Return the total number of removals since object creation.
Definition AtomicList.h:106
volatile std::atomic_int32_t d_N
Definition AtomicList.h:114
AtomicList(size_t capacity=1024, const TYPE &default_value=TYPE(), const COMPARE &comp=COMPARE())
Default constructor.
volatile TYPE * d_objects
Definition AtomicList.h:113
volatile std::atomic_int64_t d_N_remove
Definition AtomicList.h:118
const size_t d_capacity
Definition AtomicList.h:111
bool check()
Check the list.
AtomicList & operator=(const AtomicList &)=delete
volatile TYPE d_default
Definition AtomicList.h:112
constexpr size_t capacity() const
Return the capacity of the list.
Definition AtomicList.h:81
void unlock(int i, int value)
Definition AtomicList.h:131
bool empty() const
Check if the list is empty.
Definition AtomicList.h:65



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page