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