xref: /freebsd/contrib/netbsd-tests/lib/libc/sync/cpp_atomic_ops_linkable.cc (revision d370fd1cd8fc69e87dc63f4f4a82e5a8b4956c93)
1*cdebaff8SEnji Cooper /* $NetBSD: cpp_atomic_ops_linkable.cc,v 1.5 2017/01/11 12:10:26 joerg Exp $ */
2640235e2SEnji Cooper 
3640235e2SEnji Cooper /*-
4640235e2SEnji Cooper  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5640235e2SEnji Cooper  * All rights reserved.
6640235e2SEnji Cooper  *
7640235e2SEnji Cooper  * This code is derived from software contributed to The NetBSD Foundation
8640235e2SEnji Cooper  * by Martin Husemann <martin@NetBSD.org>.
9640235e2SEnji Cooper  *
10640235e2SEnji Cooper  * Redistribution and use in source and binary forms, with or without
11640235e2SEnji Cooper  * modification, are permitted provided that the following conditions
12640235e2SEnji Cooper  * are met:
13640235e2SEnji Cooper  * 1. Redistributions of source code must retain the above copyright
14640235e2SEnji Cooper  *    notice, this list of conditions and the following disclaimer.
15640235e2SEnji Cooper  * 2. Redistributions in binary form must reproduce the above copyright
16640235e2SEnji Cooper  *    notice, this list of conditions and the following disclaimer in the
17640235e2SEnji Cooper  *    documentation and/or other materials provided with the distribution.
18640235e2SEnji Cooper  *
19640235e2SEnji Cooper  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20640235e2SEnji Cooper  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21640235e2SEnji Cooper  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22640235e2SEnji Cooper  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23640235e2SEnji Cooper  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24640235e2SEnji Cooper  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25640235e2SEnji Cooper  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26640235e2SEnji Cooper  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27640235e2SEnji Cooper  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28640235e2SEnji Cooper  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29640235e2SEnji Cooper  * POSSIBILITY OF SUCH DAMAGE.
30640235e2SEnji Cooper  */
31640235e2SEnji Cooper 
32640235e2SEnji Cooper /*
33640235e2SEnji Cooper  * This is a simple link-time test to verify all builtin atomic sync
34640235e2SEnji Cooper  * operations for C++ <atomic> are available.
35640235e2SEnji Cooper  */
36640235e2SEnji Cooper 
37640235e2SEnji Cooper #include <atomic>
38640235e2SEnji Cooper #include <machine/types.h>	// for __HAVE_ATOMIC64_OPS
39640235e2SEnji Cooper 
40640235e2SEnji Cooper template <class T>
41640235e2SEnji Cooper class ATest {
42640235e2SEnji Cooper public:
ATest()43640235e2SEnji Cooper   ATest() : m_val(0)
44640235e2SEnji Cooper   {
45640235e2SEnji Cooper     m_val.exchange(std::atomic<T>(8));
46640235e2SEnji Cooper     m_val--;
47640235e2SEnji Cooper     m_val++;
48640235e2SEnji Cooper     m_val ^= 0x0f;
49640235e2SEnji Cooper     m_val &= 0x0f;
50640235e2SEnji Cooper     m_val |= 2;
51640235e2SEnji Cooper 
52640235e2SEnji Cooper     T tval(1), other(42);
53640235e2SEnji Cooper     m_val.compare_exchange_weak(tval, other,
54640235e2SEnji Cooper       std::memory_order_release, std::memory_order_relaxed);
55640235e2SEnji Cooper   }
56640235e2SEnji Cooper 
57640235e2SEnji Cooper private:
58640235e2SEnji Cooper   volatile std::atomic<T> m_val;
59640235e2SEnji Cooper };
60640235e2SEnji Cooper 
main(int argc,char ** argv)61640235e2SEnji Cooper int main(int argc, char **argv)
62640235e2SEnji Cooper {
63640235e2SEnji Cooper   ATest<char>();
64640235e2SEnji Cooper   ATest<signed char>();
65640235e2SEnji Cooper   ATest<unsigned char>();
66640235e2SEnji Cooper   ATest<short>();
67640235e2SEnji Cooper   ATest<unsigned short>();
68640235e2SEnji Cooper   ATest<int>();
69640235e2SEnji Cooper   ATest<unsigned int>();
70640235e2SEnji Cooper   ATest<long>();
71640235e2SEnji Cooper   ATest<unsigned long>();
72640235e2SEnji Cooper #ifdef __HAVE_ATOMIC64_OPS
73640235e2SEnji Cooper   ATest<long long>();
74640235e2SEnji Cooper   ATest<unsigned long long>();
75640235e2SEnji Cooper #endif
76640235e2SEnji Cooper   ATest<char16_t>();
77640235e2SEnji Cooper   ATest<char32_t>();
78640235e2SEnji Cooper   ATest<wchar_t>();
79640235e2SEnji Cooper   ATest<int_least8_t>();
80640235e2SEnji Cooper   ATest<uint_least8_t>();
81640235e2SEnji Cooper   ATest<int_least16_t>();
82640235e2SEnji Cooper   ATest<uint_least16_t>();
83640235e2SEnji Cooper   ATest<int_least32_t>();
84640235e2SEnji Cooper   ATest<uint_least32_t>();
85640235e2SEnji Cooper #ifdef __HAVE_ATOMIC64_OPS
86640235e2SEnji Cooper   ATest<int_least64_t>();
87640235e2SEnji Cooper   ATest<uint_least64_t>();
88640235e2SEnji Cooper #endif
89640235e2SEnji Cooper   ATest<int_fast8_t>();
90640235e2SEnji Cooper   ATest<uint_fast8_t>();
91640235e2SEnji Cooper   ATest<int_fast16_t>();
92640235e2SEnji Cooper   ATest<uint_fast16_t>();
93640235e2SEnji Cooper   ATest<int_fast32_t>();
94640235e2SEnji Cooper   ATest<uint_fast32_t>();
95640235e2SEnji Cooper #ifdef __HAVE_ATOMIC64_OPS
96640235e2SEnji Cooper   ATest<int_fast64_t>();
97640235e2SEnji Cooper   ATest<uint_fast64_t>();
98640235e2SEnji Cooper #endif
99640235e2SEnji Cooper   ATest<intptr_t>();
100640235e2SEnji Cooper   ATest<uintptr_t>();
101640235e2SEnji Cooper   ATest<std::size_t>();
102640235e2SEnji Cooper   ATest<std::ptrdiff_t>();
103640235e2SEnji Cooper #ifdef __HAVE_ATOMIC64_OPS
104640235e2SEnji Cooper   ATest<intmax_t>();
105640235e2SEnji Cooper   ATest<uintmax_t>();
106*cdebaff8SEnji Cooper #endif
107640235e2SEnji Cooper }
108