1 /* $NetBSD: cpp_atomic_ops_linkable.cc,v 1.5 2017/01/11 12:10:26 joerg Exp $ */
2 
3 /*-
4  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Martin Husemann <martin@NetBSD.org>.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * This is a simple link-time test to verify all builtin atomic sync
34  * operations for C++ <atomic> are available.
35  */
36 
37 #include <atomic>
38 #include <machine/types.h>	// for __HAVE_ATOMIC64_OPS
39 
40 template <class T>
41 class ATest {
42 public:
43   ATest() : m_val(0)
44   {
45     m_val.exchange(std::atomic<T>(8));
46     m_val--;
47     m_val++;
48     m_val ^= 0x0f;
49     m_val &= 0x0f;
50     m_val |= 2;
51 
52     T tval(1), other(42);
53     m_val.compare_exchange_weak(tval, other,
54       std::memory_order_release, std::memory_order_relaxed);
55   }
56 
57 private:
58   volatile std::atomic<T> m_val;
59 };
60 
61 int main(int argc, char **argv)
62 {
63   ATest<char>();
64   ATest<signed char>();
65   ATest<unsigned char>();
66   ATest<short>();
67   ATest<unsigned short>();
68   ATest<int>();
69   ATest<unsigned int>();
70   ATest<long>();
71   ATest<unsigned long>();
72 #ifdef __HAVE_ATOMIC64_OPS
73   ATest<long long>();
74   ATest<unsigned long long>();
75 #endif
76   ATest<char16_t>();
77   ATest<char32_t>();
78   ATest<wchar_t>();
79   ATest<int_least8_t>();
80   ATest<uint_least8_t>();
81   ATest<int_least16_t>();
82   ATest<uint_least16_t>();
83   ATest<int_least32_t>();
84   ATest<uint_least32_t>();
85 #ifdef __HAVE_ATOMIC64_OPS
86   ATest<int_least64_t>();
87   ATest<uint_least64_t>();
88 #endif
89   ATest<int_fast8_t>();
90   ATest<uint_fast8_t>();
91   ATest<int_fast16_t>();
92   ATest<uint_fast16_t>();
93   ATest<int_fast32_t>();
94   ATest<uint_fast32_t>();
95 #ifdef __HAVE_ATOMIC64_OPS
96   ATest<int_fast64_t>();
97   ATest<uint_fast64_t>();
98 #endif
99   ATest<intptr_t>();
100   ATest<uintptr_t>();
101   ATest<std::size_t>();
102   ATest<std::ptrdiff_t>();
103 #ifdef __HAVE_ATOMIC64_OPS
104   ATest<intmax_t>();
105   ATest<uintmax_t>();
106 #endif
107 }
108