File: self_relative_ptr_atomic.hpp

package info (click to toggle)
libpmemobj-cpp 1.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 8,388 kB
  • sloc: cpp: 136,076; sh: 1,022; perl: 381; ansic: 163; makefile: 13
file content (308 lines) | stat: -rw-r--r-- 7,370 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2020, Intel Corporation */

#include "thread_helpers.hpp"
#include "unittest.hpp"

#include <libpmemobj++/experimental/atomic_self_relative_ptr.hpp>
#include <libpmemobj++/experimental/self_relative_ptr.hpp>

#include <algorithm>
#include <memory>
#include <vector>

constexpr size_t CONCURRENCY = 20;
constexpr size_t MEAN_CONCURRENCY = CONCURRENCY * 2;
constexpr size_t HIGH_CONCURRENCY = CONCURRENCY * 5;

using pmem::obj::experimental::self_relative_ptr;

template <typename T, bool need_volatile>
using atomic_type = typename std::conditional<
	need_volatile,
	typename std::add_volatile<std::atomic<self_relative_ptr<T>>>::type,
	std::atomic<self_relative_ptr<T>>>::type;

template <bool volatile_atomic>
void
test_fetch()
{
	constexpr size_t count_iterations = 300;
	constexpr size_t arr_size = CONCURRENCY * count_iterations;
	std::vector<int> vptr(arr_size, 0);

	atomic_type<int, volatile_atomic> ptr{vptr.data()};

	parallel_exec(CONCURRENCY, [&](size_t) {
		for (size_t i = 0; i < count_iterations; ++i) {
			auto element = ptr.fetch_add(1);
			*element += 1;
		}
	});

	UT_ASSERT(vptr.data() + arr_size == ptr.load().get());
	for (auto element : vptr) {
		UT_ASSERTeq(element, 1);
	}

	parallel_exec(CONCURRENCY, [&](size_t) {
		for (size_t i = 0; i < count_iterations; ++i) {
			auto element = ptr.fetch_sub(1) - 1;
			*element += 1;
		}
	});

	UT_ASSERT(vptr.data() == ptr.load().get());
	for (auto element : vptr) {
		UT_ASSERTeq(element, 2);
	}

	parallel_exec(CONCURRENCY, [&](size_t) {
		for (size_t i = 0; i < count_iterations; ++i) {
			auto element = ptr++;
			*element += 1;
		}
	});

	UT_ASSERT(vptr.data() + arr_size == ptr.load().get());
	for (auto element : vptr) {
		UT_ASSERTeq(element, 3);
	}

	parallel_exec(CONCURRENCY, [&](size_t) {
		for (size_t i = 0; i < count_iterations; ++i) {
			auto element = --ptr;
			*element += 1;
		}
	});

	UT_ASSERT(vptr.data() == ptr.load().get());
	for (auto element : vptr) {
		UT_ASSERTeq(element, 4);
	}

	parallel_exec(CONCURRENCY, [&](size_t) {
		for (size_t i = 0; i < count_iterations; ++i) {
			auto element = ++ptr - 1;
			*element += 1;
		}
	});

	UT_ASSERT(vptr.data() + arr_size == ptr.load().get());
	for (auto element : vptr) {
		UT_ASSERTeq(element, 5);
	}

	parallel_exec(CONCURRENCY, [&](size_t) {
		for (size_t i = 0; i < count_iterations; ++i) {
			auto element = ptr-- - 1;
			*element += 1;
		}
	});

	UT_ASSERT(vptr.data() == ptr.load().get());
	for (auto element : vptr) {
		UT_ASSERTeq(element, 6);
	}

	parallel_exec(CONCURRENCY, [&](size_t) {
		for (size_t i = 0; i < count_iterations; ++i) {
			auto element = (ptr += 1) - 1;
			*element += 1;
		}
	});

	UT_ASSERT(vptr.data() + arr_size == ptr.load().get());
	for (auto element : vptr) {
		UT_ASSERTeq(element, 7);
	}

	parallel_exec(CONCURRENCY, [&](size_t) {
		for (size_t i = 0; i < count_iterations; ++i) {
			auto element = (ptr -= 1);
			*element += 1;
		}
	});

	UT_ASSERT(vptr.data() == ptr.load().get());
	for (auto element : vptr) {
		UT_ASSERTeq(element, 8);
	}
}

template <bool volatile_atomic>
void
test_exchange()
{
	self_relative_ptr<int> first = reinterpret_cast<int *>(uintptr_t{0});
	self_relative_ptr<int> second = reinterpret_cast<int *>(~uintptr_t{0});

	atomic_type<int, volatile_atomic> ptr;

	UT_ASSERT(ptr.load(std::memory_order_acquire).is_null());

	ptr.store(first, std::memory_order_release);

	UT_ASSERT(ptr.load() == first);

	auto before_ptr = ptr.exchange(second, std::memory_order_acq_rel);

	UT_ASSERT(ptr.load(std::memory_order_acquire) == second);

	parallel_exec(MEAN_CONCURRENCY, [&](size_t i) {
		for (size_t j = 0; j < 1000000; j++) {
			auto before = ptr.exchange(i % 2 == 0 ? first : second,
						   std::memory_order_acq_rel);
			UT_ASSERT(before == first || before == second);
		}
	});

	auto last_ptr = ptr.load();
	UT_ASSERT(last_ptr == first || last_ptr == second);
}

template <bool volatile_atomic>
void
test_compare_exchange()
{
	int *first = reinterpret_cast<int *>(uintptr_t{0});
	int *second = reinterpret_cast<int *>(~uintptr_t{0});
	atomic_type<int, volatile_atomic> atomic_ptr{first};
	std::atomic<size_t> exchanged(0);

	parallel_exec(CONCURRENCY, [&](size_t) {
		// tst_val != atomic_ptr  ==>  tst_val is modified
		// tst_val == atomic_ptr  ==>  atomic_ptr is modified

		self_relative_ptr<int> tst_val{first}, new_val{second};
		if (atomic_ptr.compare_exchange_strong(tst_val, new_val)) {
			++exchanged;
		} else {
			UT_ASSERT(tst_val == new_val);
		}
	});

	UT_ASSERTeq(exchanged.load(), 1);
	UT_ASSERT(atomic_ptr.load().get() == second);

	atomic_ptr = first;
	parallel_exec(CONCURRENCY, [&](size_t) {
		// tst_val != atomic_ptr  ==>  tst_val is modified
		// tst_val == atomic_ptr  ==>  atomic_ptr is modified

		self_relative_ptr<int> tst_val{first}, new_val{second};
		if (atomic_ptr.compare_exchange_strong(
			    tst_val, new_val, std::memory_order_acquire,
			    std::memory_order_relaxed)) {
			++exchanged;
		} else {
			UT_ASSERT(tst_val == new_val);
		}
	});

	UT_ASSERTeq(exchanged.load(), 2);
	UT_ASSERT(atomic_ptr.load().get() == second);
}

/**
 * Small lock free stack for tests
 */
template <bool volatile_atomic, std::memory_order... weak_args>
class test_stack {
public:
	struct node;

	using value_type = size_t;
	using node_ptr_type = self_relative_ptr<node>;

	struct node {
		size_t value;
		node_ptr_type next;
	};

	void
	push(const value_type &data)
	{
		node_ptr_type new_node = new node{data, nullptr};

		auto next_node = head.load(std::memory_order_acquire);

		while (!head.compare_exchange_weak(next_node, new_node,
						   weak_args...))
			; // empty
		new_node->next = next_node;
	}

	std::vector<value_type>
	get_all()
	{
		auto current_node = head.load();
		std::vector<value_type> values;
		while (current_node != nullptr) {
			values.push_back(current_node->value);
			current_node = current_node->next;
		}
		return values;
	}

	~test_stack()
	{
		auto current_node = head.load();
		while (current_node != nullptr) {
			auto prev_node = current_node.get();
			current_node = current_node->next;
			delete prev_node;
		}
	}

private:
	atomic_type<node, volatile_atomic> head;
};

template <bool volatile_atomic, std::memory_order... weak_args>
void
test_stack_based_on_atomic()
{
	test_stack<volatile_atomic, weak_args...> stack;
	constexpr size_t count_iterations = 1000;
	parallel_exec(HIGH_CONCURRENCY, [&](size_t i) {
		for (size_t j = 0; j < count_iterations; j++) {
			stack.push(j + (i * count_iterations));
		}
	});
	auto all = stack.get_all();
	std::sort(all.begin(), all.end());
	for (size_t i = 0; i < HIGH_CONCURRENCY * count_iterations; i++) {
		UT_ASSERTeq(all[i], i);
	}
}

template <bool volatile_atomic>
void
test_the_stack()
{
	test_stack_based_on_atomic<volatile_atomic, std::memory_order_acquire,
				   std::memory_order_relaxed>();
	test_stack_based_on_atomic<volatile_atomic,
				   std::memory_order_seq_cst>();
}

template <bool volatile_atomic>
void
test_is_lock_free()
{
	atomic_type<int, volatile_atomic> a;
	((void)(a.is_lock_free()));
	((void)(std::atomic_is_lock_free(&a)));
}

template <bool volatile_atomic>
void
test(int argc, char *argv[])
{
	test_fetch<volatile_atomic>();
	test_exchange<volatile_atomic>();
	test_compare_exchange<volatile_atomic>();
	test_the_stack<volatile_atomic>();
	test_is_lock_free<volatile_atomic>();
}