1 | /* |
---|
2 | * Copyright (c) 2013-2014 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * Copyright (c) 2013 Deng Hengyi. |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #ifdef HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include <rtems/score/atomic.h> |
---|
22 | #include <stdio.h> |
---|
23 | #include <rtems.h> |
---|
24 | |
---|
25 | #include "tmacros.h" |
---|
26 | |
---|
27 | const char rtems_test_name[] = "SPATOMIC 1"; |
---|
28 | |
---|
29 | typedef struct { |
---|
30 | Atomic_Uint atomic_int_value; |
---|
31 | Atomic_Ulong atomic_value; |
---|
32 | } test_context; |
---|
33 | |
---|
34 | static test_context test_instance; |
---|
35 | |
---|
36 | static void test_static_and_dynamic_initialization(void) |
---|
37 | { |
---|
38 | #if (__SIZEOF_INT__ == 2) |
---|
39 | #define UINT_CONSTANT 0xc0feU |
---|
40 | #else |
---|
41 | #define UINT_CONSTANT 0xc01dc0feU |
---|
42 | #endif |
---|
43 | |
---|
44 | static Atomic_Uint static_uint = ATOMIC_INITIALIZER_UINT(UINT_CONSTANT); |
---|
45 | static Atomic_Ulong static_ulong = ATOMIC_INITIALIZER_ULONG(0xdeadbeefUL); |
---|
46 | static Atomic_Pointer static_ptr = ATOMIC_INITIALIZER_PTR(&static_ptr); |
---|
47 | static Atomic_Flag static_flag = ATOMIC_INITIALIZER_FLAG; |
---|
48 | |
---|
49 | Atomic_Uint stack_uint; |
---|
50 | Atomic_Ulong stack_ulong; |
---|
51 | Atomic_Pointer stack_ptr; |
---|
52 | Atomic_Flag stack_flag; |
---|
53 | |
---|
54 | puts("=== static and dynamic initialization test case ==="); |
---|
55 | |
---|
56 | _Atomic_Init_uint(&stack_uint, UINT_CONSTANT); |
---|
57 | _Atomic_Init_ulong(&stack_ulong, 0xdeadbeefUL); |
---|
58 | _Atomic_Init_ptr(&stack_ptr, &static_ptr); |
---|
59 | _Atomic_Flag_clear(&stack_flag, ATOMIC_ORDER_RELAXED); |
---|
60 | |
---|
61 | rtems_test_assert( |
---|
62 | memcmp(&stack_uint, &static_uint, sizeof(stack_uint)) == 0 |
---|
63 | ); |
---|
64 | rtems_test_assert( |
---|
65 | memcmp(&stack_ulong, &static_ulong, sizeof(stack_ulong)) == 0 |
---|
66 | ); |
---|
67 | rtems_test_assert( |
---|
68 | memcmp(&stack_ptr, &static_ptr, sizeof(stack_ptr)) == 0 |
---|
69 | ); |
---|
70 | rtems_test_assert( |
---|
71 | memcmp(&stack_flag, &static_flag, sizeof(stack_flag)) == 0 |
---|
72 | ); |
---|
73 | |
---|
74 | rtems_test_assert( |
---|
75 | _Atomic_Load_uint(&stack_uint, ATOMIC_ORDER_RELAXED) == 0xc01dc0feU |
---|
76 | ); |
---|
77 | rtems_test_assert( |
---|
78 | _Atomic_Load_ulong(&stack_ulong, ATOMIC_ORDER_RELAXED) == 0xdeadbeefUL |
---|
79 | ); |
---|
80 | rtems_test_assert( |
---|
81 | _Atomic_Load_ptr(&stack_ptr, ATOMIC_ORDER_RELAXED) == &static_ptr |
---|
82 | ); |
---|
83 | rtems_test_assert( |
---|
84 | !_Atomic_Flag_test_and_set(&stack_flag, ATOMIC_ORDER_RELAXED) |
---|
85 | ); |
---|
86 | } |
---|
87 | |
---|
88 | typedef void (*simple_test_body)(test_context *ctx); |
---|
89 | |
---|
90 | static void test_simple_atomic_add_body(test_context *ctx) |
---|
91 | { |
---|
92 | unsigned int ia = 8, ib = 4; |
---|
93 | unsigned int ic; |
---|
94 | unsigned long a = 2, b = 1; |
---|
95 | unsigned long c; |
---|
96 | |
---|
97 | puts("=== atomic simple add test case ==="); |
---|
98 | |
---|
99 | _Atomic_Store_uint(&ctx->atomic_int_value, ia, ATOMIC_ORDER_RELAXED); |
---|
100 | _Atomic_Fetch_add_uint(&ctx->atomic_int_value, ib, ATOMIC_ORDER_RELAXED); |
---|
101 | ic = _Atomic_Load_uint(&ctx->atomic_int_value, ATOMIC_ORDER_RELAXED); |
---|
102 | rtems_test_assert(ic == (ia + ib)); |
---|
103 | |
---|
104 | _Atomic_Store_ulong(&ctx->atomic_value, a, ATOMIC_ORDER_RELAXED); |
---|
105 | _Atomic_Fetch_add_ulong(&ctx->atomic_value, b, ATOMIC_ORDER_RELAXED); |
---|
106 | c = _Atomic_Load_ulong(&ctx->atomic_value, ATOMIC_ORDER_RELAXED); |
---|
107 | rtems_test_assert(c == (a + b)); |
---|
108 | } |
---|
109 | |
---|
110 | static void test_simple_atomic_sub_body(test_context *ctx) |
---|
111 | { |
---|
112 | unsigned int ia = 8, ib = 4; |
---|
113 | unsigned int ic; |
---|
114 | unsigned long a = 2, b = 1; |
---|
115 | unsigned long c; |
---|
116 | |
---|
117 | puts("=== atomic simple sub test case ==="); |
---|
118 | |
---|
119 | _Atomic_Store_uint(&ctx->atomic_int_value, ia, ATOMIC_ORDER_RELAXED); |
---|
120 | _Atomic_Fetch_sub_uint(&ctx->atomic_int_value, ib, ATOMIC_ORDER_RELAXED); |
---|
121 | ic = _Atomic_Load_uint(&ctx->atomic_int_value, ATOMIC_ORDER_RELAXED); |
---|
122 | rtems_test_assert(ic == (ia - ib)); |
---|
123 | |
---|
124 | _Atomic_Store_ulong(&ctx->atomic_value, a, ATOMIC_ORDER_RELAXED); |
---|
125 | _Atomic_Fetch_sub_ulong(&ctx->atomic_value, b, ATOMIC_ORDER_RELAXED); |
---|
126 | c = _Atomic_Load_ulong(&ctx->atomic_value, ATOMIC_ORDER_RELAXED); |
---|
127 | rtems_test_assert(c == (a - b)); |
---|
128 | } |
---|
129 | |
---|
130 | static void test_simple_atomic_or_body(test_context *ctx) |
---|
131 | { |
---|
132 | unsigned int ia = 8, ib = 4; |
---|
133 | unsigned int ic; |
---|
134 | unsigned long a = 2, b = 1; |
---|
135 | unsigned long c; |
---|
136 | |
---|
137 | puts("=== atomic simple or test case ==="); |
---|
138 | |
---|
139 | _Atomic_Store_uint(&ctx->atomic_int_value, ia, ATOMIC_ORDER_RELAXED); |
---|
140 | _Atomic_Fetch_or_uint(&ctx->atomic_int_value, ib, ATOMIC_ORDER_RELAXED); |
---|
141 | ic = _Atomic_Load_uint(&ctx->atomic_int_value, ATOMIC_ORDER_RELAXED); |
---|
142 | rtems_test_assert(ic == (ia | ib)); |
---|
143 | |
---|
144 | _Atomic_Store_ulong(&ctx->atomic_value, a, ATOMIC_ORDER_RELAXED); |
---|
145 | _Atomic_Fetch_or_ulong(&ctx->atomic_value, b, ATOMIC_ORDER_RELAXED); |
---|
146 | c = _Atomic_Load_ulong(&ctx->atomic_value, ATOMIC_ORDER_RELAXED); |
---|
147 | rtems_test_assert(c == (a | b)); |
---|
148 | } |
---|
149 | |
---|
150 | static void test_simple_atomic_and_body(test_context *ctx) |
---|
151 | { |
---|
152 | unsigned int ia = 8, ib = 4; |
---|
153 | unsigned int ic; |
---|
154 | unsigned long a = 2, b = 1; |
---|
155 | unsigned long c; |
---|
156 | |
---|
157 | puts("=== atomic simple and test case ==="); |
---|
158 | |
---|
159 | _Atomic_Store_uint(&ctx->atomic_int_value, ia, ATOMIC_ORDER_RELAXED); |
---|
160 | _Atomic_Fetch_and_uint(&ctx->atomic_int_value, ib, ATOMIC_ORDER_RELAXED); |
---|
161 | ic = _Atomic_Load_uint(&ctx->atomic_int_value, ATOMIC_ORDER_RELAXED); |
---|
162 | rtems_test_assert(ic == (ia & ib)); |
---|
163 | |
---|
164 | _Atomic_Store_ulong(&ctx->atomic_value, a, ATOMIC_ORDER_RELAXED); |
---|
165 | _Atomic_Fetch_and_ulong(&ctx->atomic_value, b, ATOMIC_ORDER_RELAXED); |
---|
166 | c = _Atomic_Load_ulong(&ctx->atomic_value, ATOMIC_ORDER_RELAXED); |
---|
167 | rtems_test_assert(c == (a & b)); |
---|
168 | } |
---|
169 | |
---|
170 | static void test_simple_atomic_exchange_body(test_context *ctx) |
---|
171 | { |
---|
172 | unsigned int ia = 8, ib = 4; |
---|
173 | unsigned int ic; |
---|
174 | unsigned long a = 2, b = 1; |
---|
175 | unsigned long c; |
---|
176 | |
---|
177 | puts("=== atomic simple exchange test case ==="); |
---|
178 | |
---|
179 | _Atomic_Store_uint(&ctx->atomic_int_value, ia, ATOMIC_ORDER_RELAXED); |
---|
180 | _Atomic_Exchange_uint(&ctx->atomic_int_value, ib, ATOMIC_ORDER_RELAXED); |
---|
181 | ic = _Atomic_Load_uint(&ctx->atomic_int_value, ATOMIC_ORDER_RELAXED); |
---|
182 | rtems_test_assert(ic == ib); |
---|
183 | |
---|
184 | _Atomic_Store_ulong(&ctx->atomic_value, a, ATOMIC_ORDER_RELAXED); |
---|
185 | _Atomic_Exchange_ulong(&ctx->atomic_value, b, ATOMIC_ORDER_RELAXED); |
---|
186 | c = _Atomic_Load_ulong(&ctx->atomic_value, ATOMIC_ORDER_RELAXED); |
---|
187 | rtems_test_assert(c == b); |
---|
188 | } |
---|
189 | |
---|
190 | static void test_simple_atomic_compare_exchange_body(test_context *ctx) |
---|
191 | { |
---|
192 | unsigned int ia = 8, ib = 4; |
---|
193 | unsigned int ic; |
---|
194 | unsigned long a = 2, b = 1; |
---|
195 | unsigned long c; |
---|
196 | |
---|
197 | puts("=== atomic simple compare exchange test case ==="); |
---|
198 | |
---|
199 | _Atomic_Store_uint(&ctx->atomic_int_value, ia, ATOMIC_ORDER_RELAXED); |
---|
200 | _Atomic_Compare_exchange_uint(&ctx->atomic_int_value, &ia, ib, |
---|
201 | ATOMIC_ORDER_RELAXED, ATOMIC_ORDER_RELAXED); |
---|
202 | ic = _Atomic_Load_uint(&ctx->atomic_int_value, ATOMIC_ORDER_RELAXED); |
---|
203 | rtems_test_assert(ic == ib); |
---|
204 | |
---|
205 | _Atomic_Store_ulong(&ctx->atomic_value, a, ATOMIC_ORDER_RELAXED); |
---|
206 | _Atomic_Compare_exchange_ulong(&ctx->atomic_value, &a, b, |
---|
207 | ATOMIC_ORDER_RELAXED, ATOMIC_ORDER_RELAXED); |
---|
208 | c = _Atomic_Load_ulong(&ctx->atomic_value, ATOMIC_ORDER_RELAXED); |
---|
209 | rtems_test_assert(c == b); |
---|
210 | } |
---|
211 | |
---|
212 | static const simple_test_body simple_test_bodies[] = { |
---|
213 | test_simple_atomic_add_body, |
---|
214 | test_simple_atomic_sub_body, |
---|
215 | test_simple_atomic_or_body, |
---|
216 | test_simple_atomic_and_body, |
---|
217 | test_simple_atomic_exchange_body, |
---|
218 | test_simple_atomic_compare_exchange_body, |
---|
219 | }; |
---|
220 | |
---|
221 | #define SIMPLE_TEST_COUNT RTEMS_ARRAY_SIZE(simple_test_bodies) |
---|
222 | |
---|
223 | static void simple_tests(void) |
---|
224 | { |
---|
225 | test_context *ctx = &test_instance; |
---|
226 | size_t test; |
---|
227 | |
---|
228 | for (test = 0; test < SIMPLE_TEST_COUNT; ++test) { |
---|
229 | const simple_test_body *test_body = &simple_test_bodies[test]; |
---|
230 | |
---|
231 | (*test_body)(ctx); |
---|
232 | } |
---|
233 | } |
---|
234 | |
---|
235 | static void Init(rtems_task_argument arg) |
---|
236 | { |
---|
237 | TEST_BEGIN(); |
---|
238 | |
---|
239 | test_static_and_dynamic_initialization(); |
---|
240 | simple_tests(); |
---|
241 | |
---|
242 | TEST_END(); |
---|
243 | rtems_test_exit(0); |
---|
244 | } |
---|
245 | |
---|
246 | #define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER |
---|
247 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
248 | |
---|
249 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
250 | |
---|
251 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
252 | |
---|
253 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
254 | |
---|
255 | #define CONFIGURE_INIT |
---|
256 | |
---|
257 | #include <rtems/confdefs.h> |
---|