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