Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / native_client / tests / toolchain / synchronization_cpp11.cc
1 /*
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.
5  */
6
7 /*
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.
11  *
12  * See other synchronization_* tests in this directory.
13  *
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
16  */
17
18 #include <atomic>
19 #include <inttypes.h>
20 #include <stdint.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24
25 #define STR_(A) #A
26 #define STR(A) STR_(A)
27
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) "` "     \
35               "\n");                            \
36       exit(1);                                  \
37     }                                           \
38   } while (0)
39
40
41 /*
42  * ATOMIC_*_LOCK_FREE
43  *
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.
47  */
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");
59 }
60
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) "`"); \
64   } while (0)
65
66 void test_is_lock_free() {
67   TEST_IS_LOCK_FREE(bool);
68   // Table 145.
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);
83
84   // Table 146.
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);
107 }
108
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.
114
115 int main() {
116   test_lock_free_macros();
117   test_is_lock_free();
118   return 0;
119 }