1 // Copyright (c) 2011 Helge Bahmann
2 // Copyright (c) 2020 Andrey Semashev
4 // Distributed under the Boost Software License, Version 1.0.
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // Verify that definition of the "LOCK_FREE" macros and the
9 // "is_lock_free" members is consistent and matches expectations.
10 // Also, if any operation is lock-free, then the platform
11 // implementation must provide overridden fence implementations.
13 #include <boost/atomic.hpp>
16 #include <boost/config.hpp>
17 #include <boost/core/lightweight_test.hpp>
18 #include "aligned_object.hpp"
20 static const char* const lock_free_level
[] =
27 template< typename T
>
28 void verify_lock_free(const char* type_name
, int lock_free_macro_val
, int lock_free_expect
)
30 BOOST_TEST(lock_free_macro_val
>= 0 && lock_free_macro_val
<= 2);
31 BOOST_TEST(lock_free_macro_val
== lock_free_expect
);
33 boost::atomic
<T
> value
;
35 if (lock_free_macro_val
== 0)
36 BOOST_TEST(!value
.is_lock_free());
37 if (lock_free_macro_val
== 2)
38 BOOST_TEST(value
.is_lock_free());
40 BOOST_TEST_EQ(boost::atomic
<T
>::is_always_lock_free
, (lock_free_expect
== 2));
42 std::cout
<< "atomic<" << type_name
<< "> is " << lock_free_level
[lock_free_macro_val
] << " lock free\n";
44 // atomic<T> may use larger storage than sizeof(T) to achieve lock-free property. In this case atomic_ref<T> may not be lock-free.
45 if (sizeof(boost::atomic
<T
>) == sizeof(T
))
47 aligned_object
<T
, boost::atomic_ref
<T
>::required_alignment
> object
;
48 boost::atomic_ref
<T
> ref(object
.get());
50 BOOST_TEST_EQ(ref
.is_lock_free(), value
.is_lock_free());
51 BOOST_TEST_EQ(boost::atomic_ref
<T
>::is_always_lock_free
, boost::atomic
<T
>::is_always_lock_free
);
55 #if (defined(__GNUC__) || defined(__SUNPRO_CC)) && defined(__i386__)
57 #define EXPECT_CHAR_LOCK_FREE 2
58 #define EXPECT_SHORT_LOCK_FREE 2
59 #define EXPECT_INT_LOCK_FREE 2
60 #define EXPECT_LONG_LOCK_FREE 2
61 #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B)
62 #define EXPECT_LLONG_LOCK_FREE 2
64 #define EXPECT_LLONG_LOCK_FREE 0
66 #define EXPECT_INT128_LOCK_FREE 0
67 #define EXPECT_POINTER_LOCK_FREE 2
68 #define EXPECT_BOOL_LOCK_FREE 2
70 #elif (defined(__GNUC__) || defined(__SUNPRO_CC)) && defined(__x86_64__)
72 #define EXPECT_CHAR_LOCK_FREE 2
73 #define EXPECT_SHORT_LOCK_FREE 2
74 #define EXPECT_INT_LOCK_FREE 2
75 #define EXPECT_LONG_LOCK_FREE 2
76 #define EXPECT_LLONG_LOCK_FREE 2
77 #if defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG16B)
78 #define EXPECT_INT128_LOCK_FREE 2
80 #define EXPECT_INT128_LOCK_FREE 0
82 #define EXPECT_POINTER_LOCK_FREE 2
83 #define EXPECT_BOOL_LOCK_FREE 2
85 #elif defined(__GNUC__) && (defined(__POWERPC__) || defined(__PPC__))
87 #define EXPECT_CHAR_LOCK_FREE 2
88 #define EXPECT_CHAR16_T_LOCK_FREE 2
89 #define EXPECT_CHAR32_T_LOCK_FREE 2
90 #define EXPECT_WCHAR_T_LOCK_FREE 2
91 #define EXPECT_SHORT_LOCK_FREE 2
92 #define EXPECT_INT_LOCK_FREE 2
93 #define EXPECT_LONG_LOCK_FREE 2
94 #if defined(__powerpc64__)
95 #define EXPECT_LLONG_LOCK_FREE 2
97 #define EXPECT_LLONG_LOCK_FREE 0
99 #define EXPECT_INT128_LOCK_FREE 0
100 #define EXPECT_POINTER_LOCK_FREE 2
101 #define EXPECT_BOOL_LOCK_FREE 2
103 #elif defined(__GNUC__) && defined(__alpha__)
105 #define EXPECT_CHAR_LOCK_FREE 2
106 #define EXPECT_CHAR16_T_LOCK_FREE 2
107 #define EXPECT_CHAR32_T_LOCK_FREE 2
108 #define EXPECT_WCHAR_T_LOCK_FREE 2
109 #define EXPECT_SHORT_LOCK_FREE 2
110 #define EXPECT_INT_LOCK_FREE 2
111 #define EXPECT_LONG_LOCK_FREE 2
112 #define EXPECT_LLONG_LOCK_FREE 2
113 #define EXPECT_INT128_LOCK_FREE 0
114 #define EXPECT_POINTER_LOCK_FREE 2
115 #define EXPECT_BOOL_LOCK_FREE 2
117 #elif defined(__GNUC__) &&\
119 defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) ||\
120 defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6Z__) ||\
121 defined(__ARM_ARCH_6ZK__) ||\
122 defined(__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) ||\
123 defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) ||\
124 defined(__ARM_ARCH_7EM__) || defined(__ARM_ARCH_7S__)\
127 #define EXPECT_CHAR_LOCK_FREE 2
128 #define EXPECT_SHORT_LOCK_FREE 2
129 #define EXPECT_INT_LOCK_FREE 2
130 #define EXPECT_LONG_LOCK_FREE 2
131 #if !(defined(__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) || defined(__ARM_ARCH_6Z__)\
132 || ((defined(__ARM_ARCH_6K__) || defined(__ARM_ARCH_6ZK__)) && defined(__thumb__)) || defined(__ARM_ARCH_7M__))
133 #define EXPECT_LLONG_LOCK_FREE 2
135 #define EXPECT_LLONG_LOCK_FREE 0
137 #define EXPECT_INT128_LOCK_FREE 0
138 #define EXPECT_POINTER_LOCK_FREE 2
139 #define EXPECT_BOOL_LOCK_FREE 2
141 #elif defined(__linux__) && defined(__arm__)
143 #define EXPECT_CHAR_LOCK_FREE 2
144 #define EXPECT_SHORT_LOCK_FREE 2
145 #define EXPECT_INT_LOCK_FREE 2
146 #define EXPECT_LONG_LOCK_FREE 2
147 #define EXPECT_LLONG_LOCK_FREE 0
148 #define EXPECT_INT128_LOCK_FREE 0
149 #define EXPECT_POINTER_LOCK_FREE 2
150 #define EXPECT_BOOL_LOCK_FREE 2
152 #elif (defined(__GNUC__) || defined(__SUNPRO_CC)) && (defined(__sparcv8plus) || defined(__sparc_v9__))
154 #define EXPECT_CHAR_LOCK_FREE 2
155 #define EXPECT_SHORT_LOCK_FREE 2
156 #define EXPECT_INT_LOCK_FREE 2
157 #define EXPECT_LONG_LOCK_FREE 2
158 #define EXPECT_LLONG_LOCK_FREE 2
159 #define EXPECT_INT128_LOCK_FREE 0
160 #define EXPECT_POINTER_LOCK_FREE 2
161 #define EXPECT_BOOL_LOCK_FREE 2
163 #elif defined(BOOST_USE_WINDOWS_H) || defined(_WIN32_CE) || defined(BOOST_MSVC) || defined(BOOST_INTEL_WIN) || defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
165 #define EXPECT_CHAR_LOCK_FREE 2
166 #define EXPECT_SHORT_LOCK_FREE 2
167 #define EXPECT_INT_LOCK_FREE 2
168 #define EXPECT_LONG_LOCK_FREE 2
169 #if defined(_WIN64) || defined(BOOST_ATOMIC_DETAIL_X86_HAS_CMPXCHG8B) || defined(_M_AMD64) || defined(_M_IA64) || (_MSC_VER >= 1700 && (defined(_M_ARM) || defined(_M_ARM64)))
170 #define EXPECT_LLONG_LOCK_FREE 2
172 #define EXPECT_LLONG_LOCK_FREE 0
174 #define EXPECT_INT128_LOCK_FREE 0
175 #define EXPECT_POINTER_LOCK_FREE 2
176 #define EXPECT_BOOL_LOCK_FREE 2
180 #define EXPECT_CHAR_LOCK_FREE 0
181 #define EXPECT_SHORT_LOCK_FREE 0
182 #define EXPECT_INT_LOCK_FREE 0
183 #define EXPECT_LONG_LOCK_FREE 0
184 #define EXPECT_LLONG_LOCK_FREE 0
185 #define EXPECT_INT128_LOCK_FREE 0
186 #define EXPECT_POINTER_LOCK_FREE 0
187 #define EXPECT_BOOL_LOCK_FREE 0
191 int main(int, char *[])
193 verify_lock_free
<char>("char", BOOST_ATOMIC_CHAR_LOCK_FREE
, EXPECT_CHAR_LOCK_FREE
);
194 verify_lock_free
<short>("short", BOOST_ATOMIC_SHORT_LOCK_FREE
, EXPECT_SHORT_LOCK_FREE
);
195 verify_lock_free
<int>("int", BOOST_ATOMIC_INT_LOCK_FREE
, EXPECT_INT_LOCK_FREE
);
196 verify_lock_free
<long>("long", BOOST_ATOMIC_LONG_LOCK_FREE
, EXPECT_LONG_LOCK_FREE
);
197 #ifdef BOOST_HAS_LONG_LONG
198 verify_lock_free
<long long>("long long", BOOST_ATOMIC_LLONG_LOCK_FREE
, EXPECT_LLONG_LOCK_FREE
);
200 #ifdef BOOST_HAS_INT128
201 verify_lock_free
<boost::int128_type
>("int128", BOOST_ATOMIC_INT128_LOCK_FREE
, EXPECT_INT128_LOCK_FREE
);
203 verify_lock_free
<void *>("void *", BOOST_ATOMIC_POINTER_LOCK_FREE
, EXPECT_SHORT_LOCK_FREE
);
204 verify_lock_free
<bool>("bool", BOOST_ATOMIC_BOOL_LOCK_FREE
, EXPECT_BOOL_LOCK_FREE
);
206 #ifndef BOOST_ATOMIC_NO_FLOATING_POINT
208 verify_lock_free
<float>("float", BOOST_ATOMIC_FLOAT_LOCK_FREE
,
209 sizeof(float) == 1 ? EXPECT_CHAR_LOCK_FREE
: (sizeof(float) == 2 ? EXPECT_SHORT_LOCK_FREE
:
210 (sizeof(float) <= 4 ? EXPECT_INT_LOCK_FREE
: (sizeof(float) <= 8 ? EXPECT_LLONG_LOCK_FREE
: (sizeof(float) <= 16 ? EXPECT_INT128_LOCK_FREE
: 0)))));
212 verify_lock_free
<double>("double", BOOST_ATOMIC_DOUBLE_LOCK_FREE
,
213 sizeof(double) == 1 ? EXPECT_CHAR_LOCK_FREE
: (sizeof(double) == 2 ? EXPECT_SHORT_LOCK_FREE
:
214 (sizeof(double) <= 4 ? EXPECT_INT_LOCK_FREE
: (sizeof(double) <= 8 ? EXPECT_LLONG_LOCK_FREE
: (sizeof(double) <= 16 ? EXPECT_INT128_LOCK_FREE
: 0)))));
216 verify_lock_free
<long double>("long double", BOOST_ATOMIC_LONG_DOUBLE_LOCK_FREE
,
217 sizeof(long double) == 1 ? EXPECT_CHAR_LOCK_FREE
: (sizeof(long double) == 2 ? EXPECT_SHORT_LOCK_FREE
:
218 (sizeof(long double) <= 4 ? EXPECT_INT_LOCK_FREE
: (sizeof(long double) <= 8 ? EXPECT_LLONG_LOCK_FREE
: (sizeof(long double) <= 16 ? EXPECT_INT128_LOCK_FREE
: 0)))));
220 #if defined(BOOST_HAS_INT128) && defined(BOOST_HAS_FLOAT128)
221 verify_lock_free
<boost::float128_type
>("float128", BOOST_ATOMIC_INT128_LOCK_FREE
, EXPECT_INT128_LOCK_FREE
);
224 #endif // BOOST_ATOMIC_NO_FLOATING_POINT
227 BOOST_ATOMIC_CHAR_LOCK_FREE
> 0 ||
228 BOOST_ATOMIC_SHORT_LOCK_FREE
> 0 ||
229 BOOST_ATOMIC_INT_LOCK_FREE
> 0 ||
230 BOOST_ATOMIC_LONG_LOCK_FREE
> 0 ||
231 BOOST_ATOMIC_LLONG_LOCK_FREE
> 0 ||
232 BOOST_ATOMIC_BOOL_LOCK_FREE
> 0;
234 BOOST_TEST(!any_lock_free
|| BOOST_ATOMIC_THREAD_FENCE
> 0);
236 return boost::report_errors();