Update To 11.40.268.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 #if defined(__pnacl__)
50   static_assert(ATOMIC_BOOL_LOCK_FREE == 1, "should be compile-time 1");
51   static_assert(ATOMIC_CHAR_LOCK_FREE == 1, "should be compile-time 1");
52   static_assert(ATOMIC_CHAR16_T_LOCK_FREE == 1, "should be compile-time 1");
53   static_assert(ATOMIC_CHAR32_T_LOCK_FREE == 1, "should be compile-time 1");
54   static_assert(ATOMIC_WCHAR_T_LOCK_FREE == 1, "should be compile-time 1");
55   static_assert(ATOMIC_SHORT_LOCK_FREE == 1, "should be compile-time 1");
56   static_assert(ATOMIC_INT_LOCK_FREE == 1, "should be compile-time 1");
57   static_assert(ATOMIC_LONG_LOCK_FREE == 1, "should be compile-time 1");
58   static_assert(ATOMIC_LLONG_LOCK_FREE == 1, "should be compile-time 1");
59   static_assert(ATOMIC_POINTER_LOCK_FREE == 1, "should be compile-time 1");
60 #elif defined(__x86_64__) || defined(__i386__) || defined(__arm__)
61   static_assert(ATOMIC_BOOL_LOCK_FREE == 2, "should be compile-time 2");
62   static_assert(ATOMIC_CHAR_LOCK_FREE == 2, "should be compile-time 2");
63   static_assert(ATOMIC_CHAR16_T_LOCK_FREE == 2, "should be compile-time 2");
64   static_assert(ATOMIC_CHAR32_T_LOCK_FREE == 2, "should be compile-time 2");
65   static_assert(ATOMIC_WCHAR_T_LOCK_FREE == 2, "should be compile-time 2");
66   static_assert(ATOMIC_SHORT_LOCK_FREE == 2, "should be compile-time 2");
67   static_assert(ATOMIC_INT_LOCK_FREE == 2, "should be compile-time 2");
68   static_assert(ATOMIC_LONG_LOCK_FREE == 2, "should be compile-time 2");
69   static_assert(ATOMIC_LLONG_LOCK_FREE == 2, "should be compile-time 2");
70   static_assert(ATOMIC_POINTER_LOCK_FREE == 2, "should be compile-time 2");
71 # else
72   // TODO: Other architechtures, such as mips
73 #endif
74 }
75
76 #define TEST_IS_LOCK_FREE(TYPE) do {                    \
77     CHECK_EQ(std::atomic<TYPE>().is_lock_free(), true,  \
78              "expected lock-free for `" STR(TYPE) "`"); \
79   } while (0)
80
81 void test_is_lock_free() {
82   TEST_IS_LOCK_FREE(bool);
83   // Table 145.
84   TEST_IS_LOCK_FREE(char);
85   TEST_IS_LOCK_FREE(signed char);
86   TEST_IS_LOCK_FREE(unsigned char);
87   TEST_IS_LOCK_FREE(short);
88   TEST_IS_LOCK_FREE(unsigned short);
89   TEST_IS_LOCK_FREE(int);
90   TEST_IS_LOCK_FREE(unsigned int);
91   TEST_IS_LOCK_FREE(long);
92   TEST_IS_LOCK_FREE(unsigned long);
93   TEST_IS_LOCK_FREE(long long);
94   TEST_IS_LOCK_FREE(unsigned long long);
95   TEST_IS_LOCK_FREE(char16_t);
96   TEST_IS_LOCK_FREE(char32_t);
97   TEST_IS_LOCK_FREE(wchar_t);
98
99   // Table 146.
100   TEST_IS_LOCK_FREE(int_least8_t);
101   TEST_IS_LOCK_FREE(uint_least8_t);
102   TEST_IS_LOCK_FREE(int_least16_t);
103   TEST_IS_LOCK_FREE(uint_least16_t);
104   TEST_IS_LOCK_FREE(int_least32_t);
105   TEST_IS_LOCK_FREE(uint_least32_t);
106   TEST_IS_LOCK_FREE(int_least64_t);
107   TEST_IS_LOCK_FREE(uint_least64_t);
108   TEST_IS_LOCK_FREE(int_fast8_t);
109   TEST_IS_LOCK_FREE(uint_fast8_t);
110   TEST_IS_LOCK_FREE(int_fast16_t);
111   TEST_IS_LOCK_FREE(uint_fast16_t);
112   TEST_IS_LOCK_FREE(int_fast32_t);
113   TEST_IS_LOCK_FREE(uint_fast32_t);
114   TEST_IS_LOCK_FREE(int_fast64_t);
115   TEST_IS_LOCK_FREE(uint_fast64_t);
116   TEST_IS_LOCK_FREE(intptr_t);
117   TEST_IS_LOCK_FREE(uintptr_t);
118   TEST_IS_LOCK_FREE(size_t);
119   TEST_IS_LOCK_FREE(ptrdiff_t);
120   TEST_IS_LOCK_FREE(intmax_t);
121   TEST_IS_LOCK_FREE(uintmax_t);
122 }
123
124 // TODO(jfb) Test C++11 atomic features.
125 //   - std::atomic and their functions (including is_lock_free).
126 //   - std::atomic_flag.
127 //   - std::atomic_thread_fence (atomic_signal_fence currently unsupported).
128 //   - 6 memory orders.
129
130 int main() {
131   test_lock_free_macros();
132   test_is_lock_free();
133   return 0;
134 }