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_sub(volatile atomic<Integral>* obj, Integral op);
18 //
19 // template <class Integral>
20 // Integral
21 // atomic_fetch_sub(atomic<Integral>* obj, Integral op);
22 //
23 // template <class T>
24 // T*
25 // atomic_fetch_sub(volatile atomic<T*>* obj, ptrdiff_t op);
26 //
27 // template <class T>
28 // T*
29 // atomic_fetch_sub(atomic<T*>* obj, ptrdiff_t op);
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(3));
44 assert(std::atomic_fetch_sub(&t, T(2)) == T(3));
45 assert(t == T(1));
46 }
47 {
48 typedef std::atomic<T> A;
49 volatile A t;
50 std::atomic_init(&t, T(3));
51 assert(std::atomic_fetch_sub(&t, T(2)) == T(3));
52 assert(t == T(1));
53 }
54 }
55 };
56
57 template <class T>
testp()58 void testp()
59 {
60 {
61 typedef std::atomic<T> A;
62 typedef typename std::remove_pointer<T>::type X;
63 A t;
64 std::atomic_init(&t, T(3*sizeof(X)));
65 assert(std::atomic_fetch_sub(&t, 2) == T(3*sizeof(X)));
66 assert(t == T(1*sizeof(X)));
67 }
68 {
69 typedef std::atomic<T> A;
70 typedef typename std::remove_pointer<T>::type X;
71 volatile A t;
72 std::atomic_init(&t, T(3*sizeof(X)));
73 assert(std::atomic_fetch_sub(&t, 2) == T(3*sizeof(X)));
74 assert(t == T(1*sizeof(X)));
75 }
76 }
77
main()78 int main()
79 {
80 TestEachIntegralType<TestFn>()();
81 testp<int*>();
82 testp<const int*>();
83 }
84