Howard Hinnant | 91e2f26 | 2010-12-07 20:46:14 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
| 11 | // ... test crashes clang |
Howard Hinnant | 91e2f26 | 2010-12-07 20:46:14 +0000 | [diff] [blame] | 12 | |
| 13 | // <atomic> |
| 14 | |
| 15 | // template <class Integral> |
| 16 | // Integral |
| 17 | // atomic_fetch_sub_explicit(volatile atomic<Integral>* obj, Integral op, |
| 18 | // memory_order m); |
| 19 | // template <class Integral> |
| 20 | // Integral |
| 21 | // atomic_fetch_sub_explicit(atomic<Integral>* obj, Integral op, |
| 22 | // memory_order m); |
| 23 | // |
| 24 | // template <class T> |
| 25 | // T* |
| 26 | // atomic_fetch_sub_explicit(volatile atomic<T*>* obj, ptrdiff_t op, |
| 27 | // memory_order m); |
| 28 | // template <class T> |
| 29 | // T* |
| 30 | // atomic_fetch_sub_explicit(atomic<T*>* obj, ptrdiff_t op, memory_order m); |
| 31 | |
| 32 | #include <atomic> |
Richard Smith | 9efdc0b | 2012-04-19 00:50:47 +0000 | [diff] [blame] | 33 | #include <type_traits> |
Howard Hinnant | 91e2f26 | 2010-12-07 20:46:14 +0000 | [diff] [blame] | 34 | #include <cassert> |
| 35 | |
| 36 | template <class T> |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 37 | void |
| 38 | test() |
| 39 | { |
Howard Hinnant | 91e2f26 | 2010-12-07 20:46:14 +0000 | [diff] [blame] | 40 | { |
| 41 | typedef std::atomic<T> A; |
| 42 | A t; |
| 43 | std::atomic_init(&t, T(3)); |
| 44 | assert(std::atomic_fetch_sub_explicit(&t, T(2), |
| 45 | std::memory_order_seq_cst) == T(3)); |
| 46 | assert(t == T(1)); |
| 47 | } |
| 48 | { |
| 49 | typedef std::atomic<T> A; |
| 50 | volatile A t; |
| 51 | std::atomic_init(&t, T(3)); |
| 52 | assert(std::atomic_fetch_sub_explicit(&t, T(2), |
| 53 | std::memory_order_seq_cst) == T(3)); |
| 54 | assert(t == T(1)); |
| 55 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 56 | } |
Howard Hinnant | 91e2f26 | 2010-12-07 20:46:14 +0000 | [diff] [blame] | 57 | |
Eric Fiselier | 00f4a49 | 2015-08-19 17:21:46 +0000 | [diff] [blame] | 58 | template <class T> |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 59 | void |
| 60 | testp() |
Eric Fiselier | 00f4a49 | 2015-08-19 17:21:46 +0000 | [diff] [blame] | 61 | { |
| 62 | { |
| 63 | typedef std::atomic<T> A; |
| 64 | typedef typename std::remove_pointer<T>::type X; |
| 65 | A t; |
| 66 | std::atomic_init(&t, T(3*sizeof(X))); |
| 67 | assert(std::atomic_fetch_sub_explicit(&t, 2, |
| 68 | std::memory_order_seq_cst) == T(3*sizeof(X))); |
| 69 | assert(t == T(1*sizeof(X))); |
| 70 | } |
| 71 | { |
| 72 | typedef std::atomic<T> A; |
| 73 | typedef typename std::remove_pointer<T>::type X; |
| 74 | volatile A t; |
| 75 | std::atomic_init(&t, T(3*sizeof(X))); |
| 76 | assert(std::atomic_fetch_sub_explicit(&t, 2, |
| 77 | std::memory_order_seq_cst) == T(3*sizeof(X))); |
| 78 | assert(t == T(1*sizeof(X))); |
| 79 | } |
| 80 | } |
| 81 | |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 82 | struct A |
| 83 | { |
| 84 | int i; |
| 85 | |
| 86 | explicit A(int d = 0) noexcept {i=d;} |
| 87 | A(const A& a) : i(a.i) {} |
| 88 | A(const volatile A& a) : i(a.i) {} |
| 89 | |
| 90 | void operator=(const volatile A& a) volatile {i = a.i;} |
| 91 | |
| 92 | friend bool operator==(const A& x, const A& y) |
| 93 | {return x.i == y.i;} |
| 94 | }; |
| 95 | |
Howard Hinnant | 91e2f26 | 2010-12-07 20:46:14 +0000 | [diff] [blame] | 96 | int main() |
| 97 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 98 | test<char>(); |
| 99 | test<signed char>(); |
| 100 | test<unsigned char>(); |
| 101 | test<short>(); |
| 102 | test<unsigned short>(); |
| 103 | test<int>(); |
| 104 | test<unsigned int>(); |
| 105 | test<long>(); |
| 106 | test<unsigned long>(); |
| 107 | test<long long>(); |
| 108 | test<unsigned long long>(); |
| 109 | test<wchar_t>(); |
| 110 | #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS |
| 111 | test<char16_t>(); |
| 112 | test<char32_t>(); |
| 113 | #endif // _LIBCPP_HAS_NO_UNICODE_CHARS |
Howard Hinnant | 91e2f26 | 2010-12-07 20:46:14 +0000 | [diff] [blame] | 114 | testp<int*>(); |
| 115 | testp<const int*>(); |
| 116 | } |