2 * Copyright (c) 2013 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * This test ensures that the PNaCl backends can deal with C++11
9 * synchronization primitives as would be written by regular users,
10 * including PNaCl's ABI stabilization and target lowering.
12 * See other synchronization_* tests in this directory.
14 * This is a syntactical check as we do not run this multithreaded. Some
15 * real testing is done here: tests/threads/thread_test.c
26 #define STR(A) STR_(A)
28 #define CHECK_EQ(LHS, RHS, MSG) do { \
29 printf("\t" MSG ":\t" STR(LHS) "=%" PRIu64 \
30 " and " STR(RHS) "=%" PRIu64 "\n", \
31 (uint64_t)(LHS), (uint64_t)(RHS)); \
32 if ((LHS) != (RHS)) { \
33 fprintf(stderr, "ERROR: " MSG ": `" \
34 STR(LHS) " != " STR(RHS) "` " \
44 * These macros must be compile-time constants, and for PNaCl the value
45 * should be 1, which means that the corresponding type may be
46 * lock-free: we can't guarantee that all our platforms are lock-free.
48 void test_lock_free_macros() {
49 static_assert(ATOMIC_BOOL_LOCK_FREE == 1, "should be compile-time 1");
50 static_assert(ATOMIC_CHAR_LOCK_FREE == 1, "should be compile-time 1");
51 static_assert(ATOMIC_CHAR16_T_LOCK_FREE == 1, "should be compile-time 1");
52 static_assert(ATOMIC_CHAR32_T_LOCK_FREE == 1, "should be compile-time 1");
53 static_assert(ATOMIC_WCHAR_T_LOCK_FREE == 1, "should be compile-time 1");
54 static_assert(ATOMIC_SHORT_LOCK_FREE == 1, "should be compile-time 1");
55 static_assert(ATOMIC_INT_LOCK_FREE == 1, "should be compile-time 1");
56 static_assert(ATOMIC_LONG_LOCK_FREE == 1, "should be compile-time 1");
57 static_assert(ATOMIC_LLONG_LOCK_FREE == 1, "should be compile-time 1");
58 static_assert(ATOMIC_POINTER_LOCK_FREE == 1, "should be compile-time 1");
61 #define TEST_IS_LOCK_FREE(TYPE) do { \
62 CHECK_EQ(std::atomic<TYPE>().is_lock_free(), true, \
63 "expected lock-free for `" STR(TYPE) "`"); \
66 void test_is_lock_free() {
67 TEST_IS_LOCK_FREE(bool);
69 TEST_IS_LOCK_FREE(char);
70 TEST_IS_LOCK_FREE(signed char);
71 TEST_IS_LOCK_FREE(unsigned char);
72 TEST_IS_LOCK_FREE(short);
73 TEST_IS_LOCK_FREE(unsigned short);
74 TEST_IS_LOCK_FREE(int);
75 TEST_IS_LOCK_FREE(unsigned int);
76 TEST_IS_LOCK_FREE(long);
77 TEST_IS_LOCK_FREE(unsigned long);
78 TEST_IS_LOCK_FREE(long long);
79 TEST_IS_LOCK_FREE(unsigned long long);
80 TEST_IS_LOCK_FREE(char16_t);
81 TEST_IS_LOCK_FREE(char32_t);
82 TEST_IS_LOCK_FREE(wchar_t);
85 TEST_IS_LOCK_FREE(int_least8_t);
86 TEST_IS_LOCK_FREE(uint_least8_t);
87 TEST_IS_LOCK_FREE(int_least16_t);
88 TEST_IS_LOCK_FREE(uint_least16_t);
89 TEST_IS_LOCK_FREE(int_least32_t);
90 TEST_IS_LOCK_FREE(uint_least32_t);
91 TEST_IS_LOCK_FREE(int_least64_t);
92 TEST_IS_LOCK_FREE(uint_least64_t);
93 TEST_IS_LOCK_FREE(int_fast8_t);
94 TEST_IS_LOCK_FREE(uint_fast8_t);
95 TEST_IS_LOCK_FREE(int_fast16_t);
96 TEST_IS_LOCK_FREE(uint_fast16_t);
97 TEST_IS_LOCK_FREE(int_fast32_t);
98 TEST_IS_LOCK_FREE(uint_fast32_t);
99 TEST_IS_LOCK_FREE(int_fast64_t);
100 TEST_IS_LOCK_FREE(uint_fast64_t);
101 TEST_IS_LOCK_FREE(intptr_t);
102 TEST_IS_LOCK_FREE(uintptr_t);
103 TEST_IS_LOCK_FREE(size_t);
104 TEST_IS_LOCK_FREE(ptrdiff_t);
105 TEST_IS_LOCK_FREE(intmax_t);
106 TEST_IS_LOCK_FREE(uintmax_t);
109 // TODO(jfb) Test C++11 atomic features.
110 // - std::atomic and their functions (including is_lock_free).
111 // - std::atomic_flag.
112 // - std::atomic_thread_fence (atomic_signal_fence currently unsupported).
113 // - 6 memory orders.
116 test_lock_free_macros();