1*77fe1f22Sjoerg /* $NetBSD: cpp_atomic_ops_linkable.cc,v 1.5 2017/01/11 12:10:26 joerg Exp $ */
2069f2c5dSmartin
3069f2c5dSmartin /*-
4069f2c5dSmartin * Copyright (c) 2014 The NetBSD Foundation, Inc.
5069f2c5dSmartin * All rights reserved.
6069f2c5dSmartin *
7069f2c5dSmartin * This code is derived from software contributed to The NetBSD Foundation
8069f2c5dSmartin * by Martin Husemann <martin@NetBSD.org>.
9069f2c5dSmartin *
10069f2c5dSmartin * Redistribution and use in source and binary forms, with or without
11069f2c5dSmartin * modification, are permitted provided that the following conditions
12069f2c5dSmartin * are met:
13069f2c5dSmartin * 1. Redistributions of source code must retain the above copyright
14069f2c5dSmartin * notice, this list of conditions and the following disclaimer.
15069f2c5dSmartin * 2. Redistributions in binary form must reproduce the above copyright
16069f2c5dSmartin * notice, this list of conditions and the following disclaimer in the
17069f2c5dSmartin * documentation and/or other materials provided with the distribution.
18069f2c5dSmartin *
19069f2c5dSmartin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20069f2c5dSmartin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21069f2c5dSmartin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22069f2c5dSmartin * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23069f2c5dSmartin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24069f2c5dSmartin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25069f2c5dSmartin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26069f2c5dSmartin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27069f2c5dSmartin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28069f2c5dSmartin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29069f2c5dSmartin * POSSIBILITY OF SUCH DAMAGE.
30069f2c5dSmartin */
31069f2c5dSmartin
32069f2c5dSmartin /*
33069f2c5dSmartin * This is a simple link-time test to verify all builtin atomic sync
34069f2c5dSmartin * operations for C++ <atomic> are available.
35069f2c5dSmartin */
36069f2c5dSmartin
37069f2c5dSmartin #include <atomic>
382d4c5dc1Smartin #include <machine/types.h> // for __HAVE_ATOMIC64_OPS
39069f2c5dSmartin
40069f2c5dSmartin template <class T>
41069f2c5dSmartin class ATest {
42069f2c5dSmartin public:
ATest()43069f2c5dSmartin ATest() : m_val(0)
44069f2c5dSmartin {
45069f2c5dSmartin m_val.exchange(std::atomic<T>(8));
46069f2c5dSmartin m_val--;
47069f2c5dSmartin m_val++;
48069f2c5dSmartin m_val ^= 0x0f;
49069f2c5dSmartin m_val &= 0x0f;
50069f2c5dSmartin m_val |= 2;
51069f2c5dSmartin
52069f2c5dSmartin T tval(1), other(42);
53069f2c5dSmartin m_val.compare_exchange_weak(tval, other,
54069f2c5dSmartin std::memory_order_release, std::memory_order_relaxed);
55069f2c5dSmartin }
56069f2c5dSmartin
57069f2c5dSmartin private:
58069f2c5dSmartin volatile std::atomic<T> m_val;
59069f2c5dSmartin };
60069f2c5dSmartin
main(int argc,char ** argv)61069f2c5dSmartin int main(int argc, char **argv)
62069f2c5dSmartin {
63069f2c5dSmartin ATest<char>();
64069f2c5dSmartin ATest<signed char>();
65069f2c5dSmartin ATest<unsigned char>();
66069f2c5dSmartin ATest<short>();
67069f2c5dSmartin ATest<unsigned short>();
68069f2c5dSmartin ATest<int>();
69069f2c5dSmartin ATest<unsigned int>();
70069f2c5dSmartin ATest<long>();
71069f2c5dSmartin ATest<unsigned long>();
72495b7050Smartin #ifdef __HAVE_ATOMIC64_OPS
73069f2c5dSmartin ATest<long long>();
74069f2c5dSmartin ATest<unsigned long long>();
75495b7050Smartin #endif
76069f2c5dSmartin ATest<char16_t>();
77069f2c5dSmartin ATest<char32_t>();
78069f2c5dSmartin ATest<wchar_t>();
79069f2c5dSmartin ATest<int_least8_t>();
80069f2c5dSmartin ATest<uint_least8_t>();
81069f2c5dSmartin ATest<int_least16_t>();
82069f2c5dSmartin ATest<uint_least16_t>();
83069f2c5dSmartin ATest<int_least32_t>();
84069f2c5dSmartin ATest<uint_least32_t>();
852d4c5dc1Smartin #ifdef __HAVE_ATOMIC64_OPS
86069f2c5dSmartin ATest<int_least64_t>();
87069f2c5dSmartin ATest<uint_least64_t>();
882d4c5dc1Smartin #endif
89069f2c5dSmartin ATest<int_fast8_t>();
90069f2c5dSmartin ATest<uint_fast8_t>();
91069f2c5dSmartin ATest<int_fast16_t>();
92069f2c5dSmartin ATest<uint_fast16_t>();
93069f2c5dSmartin ATest<int_fast32_t>();
94069f2c5dSmartin ATest<uint_fast32_t>();
952d4c5dc1Smartin #ifdef __HAVE_ATOMIC64_OPS
96069f2c5dSmartin ATest<int_fast64_t>();
97069f2c5dSmartin ATest<uint_fast64_t>();
982d4c5dc1Smartin #endif
99069f2c5dSmartin ATest<intptr_t>();
100069f2c5dSmartin ATest<uintptr_t>();
101069f2c5dSmartin ATest<std::size_t>();
102069f2c5dSmartin ATest<std::ptrdiff_t>();
103495b7050Smartin #ifdef __HAVE_ATOMIC64_OPS
104069f2c5dSmartin ATest<intmax_t>();
105069f2c5dSmartin ATest<uintmax_t>();
106*77fe1f22Sjoerg #endif
107069f2c5dSmartin }
108