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