-
Notifications
You must be signed in to change notification settings - Fork 10.7k
/
operator.minus_equals.pass.cpp
105 lines (88 loc) · 2.78 KB
/
operator.minus_equals.pass.cpp
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
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: target={{.+}}-windows-gnu
// XFAIL: LIBCXX-AIX-FIXME
// ADDITIONAL_COMPILE_FLAGS(has-latomic): -latomic
// XFAIL: !has-64-bit-atomics
// Hangs with msan.
// UNSUPPORTED: msan
// floating-point-type operator-=(floating-point-type) volatile noexcept;
// floating-point-type operator-=(floating-point-type) noexcept;
#include <atomic>
#include <cassert>
#include <concepts>
#include <type_traits>
#include <vector>
#include "test_helper.h"
#include "test_macros.h"
#ifndef TEST_HAS_NO_THREADS
# include "make_test_thread.h"
# include <thread>
#endif
template <class T>
concept HasVolatileMinusEquals = requires(volatile std::atomic<T>& a, T t) { a -= t; };
template <class T, template <class> class MaybeVolatile = std::type_identity_t>
void test_impl() {
static_assert(HasVolatileMinusEquals<T> == std::atomic<T>::is_always_lock_free);
static_assert(noexcept(std::declval<MaybeVolatile<std::atomic<T>>&>() -= T(0)));
// -=
{
MaybeVolatile<std::atomic<T>> a(3.1);
std::same_as<T> decltype(auto) r = a -= T(1.2);
assert(r == T(3.1) - T(1.2));
assert(a.load() == T(3.1) - T(1.2));
}
#ifndef TEST_HAS_NO_THREADS
// -= concurrent
{
constexpr auto number_of_threads = 4;
constexpr auto loop = 1000;
MaybeVolatile<std::atomic<T>> at;
std::vector<std::thread> threads;
threads.reserve(number_of_threads);
for (auto i = 0; i < number_of_threads; ++i) {
threads.push_back(support::make_test_thread([&at]() {
for (auto j = 0; j < loop; ++j) {
at -= T(1.234);
}
}));
}
for (auto& thread : threads) {
thread.join();
}
const auto accu_neg = [](T t, int n) {
T res(0);
for (auto i = 0; i < n; ++i) {
res -= t;
}
return res;
};
assert(at.load() == accu_neg(1.234, number_of_threads * loop));
}
#endif
// memory_order::seq_cst
{
auto minus_equals = [](MaybeVolatile<std::atomic<T>>& x, T old_value, T new_val) { x -= (old_value - new_val); };
auto load = [](MaybeVolatile<std::atomic<T>>& x) { return x.load(); };
test_seq_cst<T, MaybeVolatile>(minus_equals, load);
}
}
template <class T>
void test() {
test_impl<T>();
if constexpr (std::atomic<T>::is_always_lock_free) {
test_impl<T, std::add_volatile_t>();
}
}
int main(int, char**) {
test<float>();
test<double>();
test<long double>();
return 0;
}