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 //===----------------------------------------------------------------------===//
9 //
10 // UNSUPPORTED: libcpp-has-no-threads
11 //  ... test crashes clang
12 
13 // <atomic>
14 
15 // template <class Integral>
16 //     Integral
17 //     atomic_fetch_add_explicit(volatile atomic<Integral>* obj, Integral op,
18 //                               memory_order m);
19 // template <class Integral>
20 //     Integral
21 //     atomic_fetch_add_explicit(atomic<Integral>* obj, Integral op,
22 //                               memory_order m);
23 // template <class T>
24 //     T*
25 //     atomic_fetch_add_explicit(volatile atomic<T*>* obj, ptrdiff_t op,
26 //                               memory_order m);
27 // template <class T>
28 //     T*
29 //     atomic_fetch_add_explicit(atomic<T*>* obj, ptrdiff_t op, memory_order m);
30 
31 #include <atomic>
32 #include <type_traits>
33 #include <cassert>
34 
35 #include "atomic_helpers.h"
36 
37 template <class T>
38 struct TestFn {
operator ()TestFn39   void operator()() const {
40     {
41         typedef std::atomic<T> A;
42         A t;
43         std::atomic_init(&t, T(1));
44         assert(std::atomic_fetch_add_explicit(&t, T(2),
45                                             std::memory_order_seq_cst) == T(1));
46         assert(t == T(3));
47     }
48     {
49         typedef std::atomic<T> A;
50         volatile A t;
51         std::atomic_init(&t, T(1));
52         assert(std::atomic_fetch_add_explicit(&t, T(2),
53                                             std::memory_order_seq_cst) == T(1));
54         assert(t == T(3));
55     }
56   }
57 };
58 
59 template <class T>
60 void
testp()61 testp()
62 {
63     {
64         typedef std::atomic<T> A;
65         typedef typename std::remove_pointer<T>::type X;
66         A t;
67         std::atomic_init(&t, T(1*sizeof(X)));
68         assert(std::atomic_fetch_add_explicit(&t, 2,
69                                   std::memory_order_seq_cst) == T(1*sizeof(X)));
70         assert(t == T(3*sizeof(X)));
71     }
72     {
73         typedef std::atomic<T> A;
74         typedef typename std::remove_pointer<T>::type X;
75         volatile A t;
76         std::atomic_init(&t, T(1*sizeof(X)));
77         assert(std::atomic_fetch_add_explicit(&t, 2,
78                                   std::memory_order_seq_cst) == T(1*sizeof(X)));
79         assert(t == T(3*sizeof(X)));
80     }
81 }
82 
main()83 int main()
84 {
85     TestEachIntegralType<TestFn>()();
86     testp<int*>();
87     testp<const int*>();
88 }
89