Howard Hinnant | 4777bf2 | 2010-12-06 23:10:08 +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 | // ... assertion fails line 38 |
Howard Hinnant | 4777bf2 | 2010-12-06 23:10:08 +0000 | [diff] [blame] | 12 | |
| 13 | // <atomic> |
| 14 | |
| 15 | // template <class T> |
| 16 | // bool |
| 17 | // atomic_compare_exchange_strong_explicit(volatile atomic<T>* obj, T* expc, |
| 18 | // T desr, |
| 19 | // memory_order s, memory_order f); |
| 20 | // |
| 21 | // template <class T> |
| 22 | // bool |
| 23 | // atomic_compare_exchange_strong_explicit(atomic<T>* obj, T* expc, T desr, |
| 24 | // memory_order s, memory_order f); |
| 25 | |
| 26 | #include <atomic> |
Richard Smith | 9efdc0b | 2012-04-19 00:50:47 +0000 | [diff] [blame] | 27 | #include <type_traits> |
Howard Hinnant | 4777bf2 | 2010-12-06 23:10:08 +0000 | [diff] [blame] | 28 | #include <cassert> |
| 29 | |
| 30 | template <class T> |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 31 | void |
| 32 | test() |
| 33 | { |
Howard Hinnant | 4777bf2 | 2010-12-06 23:10:08 +0000 | [diff] [blame] | 34 | { |
| 35 | typedef std::atomic<T> A; |
| 36 | A a; |
| 37 | T t(T(1)); |
| 38 | std::atomic_init(&a, t); |
| 39 | assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(2), |
| 40 | std::memory_order_seq_cst, std::memory_order_seq_cst) == true); |
| 41 | assert(a == T(2)); |
| 42 | assert(t == T(1)); |
| 43 | assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(3), |
| 44 | std::memory_order_seq_cst, std::memory_order_seq_cst) == false); |
| 45 | assert(a == T(2)); |
David Chisnall | d3eca75 | 2012-04-05 13:48:16 +0000 | [diff] [blame] | 46 | assert(t == T(2)); |
Howard Hinnant | 4777bf2 | 2010-12-06 23:10:08 +0000 | [diff] [blame] | 47 | } |
| 48 | { |
| 49 | typedef std::atomic<T> A; |
| 50 | volatile A a; |
| 51 | T t(T(1)); |
| 52 | std::atomic_init(&a, t); |
| 53 | assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(2), |
| 54 | std::memory_order_seq_cst, std::memory_order_seq_cst) == true); |
| 55 | assert(a == T(2)); |
| 56 | assert(t == T(1)); |
| 57 | assert(std::atomic_compare_exchange_strong_explicit(&a, &t, T(3), |
| 58 | std::memory_order_seq_cst, std::memory_order_seq_cst) == false); |
| 59 | assert(a == T(2)); |
David Chisnall | d3eca75 | 2012-04-05 13:48:16 +0000 | [diff] [blame] | 60 | assert(t == T(2)); |
Howard Hinnant | 4777bf2 | 2010-12-06 23:10:08 +0000 | [diff] [blame] | 61 | } |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 62 | } |
| 63 | |
| 64 | struct A |
| 65 | { |
| 66 | int i; |
| 67 | |
| 68 | explicit A(int d = 0) noexcept {i=d;} |
| 69 | |
| 70 | friend bool operator==(const A& x, const A& y) |
| 71 | {return x.i == y.i;} |
Howard Hinnant | 4777bf2 | 2010-12-06 23:10:08 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | int main() |
| 75 | { |
Dan Albert | 1d4a1ed | 2016-05-25 22:36:09 -0700 | [diff] [blame^] | 76 | test<A>(); |
| 77 | test<char>(); |
| 78 | test<signed char>(); |
| 79 | test<unsigned char>(); |
| 80 | test<short>(); |
| 81 | test<unsigned short>(); |
| 82 | test<int>(); |
| 83 | test<unsigned int>(); |
| 84 | test<long>(); |
| 85 | test<unsigned long>(); |
| 86 | test<long long>(); |
| 87 | test<unsigned long long>(); |
| 88 | test<wchar_t>(); |
| 89 | #ifndef _LIBCPP_HAS_NO_UNICODE_CHARS |
| 90 | test<char16_t>(); |
| 91 | test<char32_t>(); |
| 92 | #endif // _LIBCPP_HAS_NO_UNICODE_CHARS |
| 93 | test<int*>(); |
| 94 | test<const int*>(); |
Howard Hinnant | 4777bf2 | 2010-12-06 23:10:08 +0000 | [diff] [blame] | 95 | } |