2 * Copyright (c) 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include "ovs-atomic.h"
22 #include "ovs-thread.h"
24 #define TEST_ATOMIC_TYPE(ATOMIC_TYPE, BASE_TYPE) \
26 ATOMIC_TYPE x = ATOMIC_VAR_INIT(1); \
27 BASE_TYPE value, orig; \
29 atomic_read(&x, &value); \
30 ovs_assert(value == 1); \
32 atomic_store(&x, 2); \
33 atomic_read(&x, &value); \
34 ovs_assert(value == 2); \
37 atomic_read(&x, &value); \
38 ovs_assert(value == 3); \
40 atomic_add(&x, 1, &orig); \
41 ovs_assert(orig == 3); \
42 atomic_read(&x, &value); \
43 ovs_assert(value == 4); \
45 atomic_sub(&x, 2, &orig); \
46 ovs_assert(orig == 4); \
47 atomic_read(&x, &value); \
48 ovs_assert(value == 2); \
50 atomic_or(&x, 6, &orig); \
51 ovs_assert(orig == 2); \
52 atomic_read(&x, &value); \
53 ovs_assert(value == 6); \
55 atomic_and(&x, 10, &orig); \
56 ovs_assert(orig == 6); \
57 atomic_read(&x, &value); \
58 ovs_assert(value == 2); \
60 atomic_xor(&x, 10, &orig); \
61 ovs_assert(orig == 2); \
62 atomic_read(&x, &value); \
63 ovs_assert(value == 8); \
66 #define TEST_ATOMIC_TYPE_EXPLICIT(ATOMIC_TYPE, BASE_TYPE, \
67 ORDER_READ, ORDER_STORE, ORDER_RMW) \
69 ATOMIC_TYPE x = ATOMIC_VAR_INIT(1); \
70 BASE_TYPE value, orig; \
72 atomic_read_explicit(&x, &value, ORDER_READ); \
73 ovs_assert(value == 1); \
75 atomic_store_explicit(&x, 2, ORDER_STORE); \
76 atomic_read_explicit(&x, &value, ORDER_READ); \
77 ovs_assert(value == 2); \
80 atomic_read_explicit(&x, &value, ORDER_READ); \
81 ovs_assert(value == 3); \
83 atomic_add_explicit(&x, 1, &orig, ORDER_RMW); \
84 ovs_assert(orig == 3); \
85 atomic_read_explicit(&x, &value, ORDER_READ); \
86 ovs_assert(value == 4); \
88 atomic_sub_explicit(&x, 2, &orig, ORDER_RMW); \
89 ovs_assert(orig == 4); \
90 atomic_read_explicit(&x, &value, ORDER_READ); \
91 ovs_assert(value == 2); \
93 atomic_or_explicit(&x, 6, &orig, ORDER_RMW); \
94 ovs_assert(orig == 2); \
95 atomic_read_explicit(&x, &value, ORDER_READ); \
96 ovs_assert(value == 6); \
98 atomic_and_explicit(&x, 10, &orig, ORDER_RMW); \
99 ovs_assert(orig == 6); \
100 atomic_read_explicit(&x, &value, ORDER_READ); \
101 ovs_assert(value == 2); \
103 atomic_xor_explicit(&x, 10, &orig, ORDER_RMW); \
104 ovs_assert(orig == 2); \
105 atomic_read_explicit(&x, &value, ORDER_READ); \
106 ovs_assert(value == 8); \
110 #define TEST_ATOMIC_ORDER(ORDER_READ, ORDER_STORE, ORDER_RMW) \
112 TEST_ATOMIC_TYPE_EXPLICIT(atomic_char, char, \
113 ORDER_READ, ORDER_STORE, ORDER_RMW); \
114 TEST_ATOMIC_TYPE_EXPLICIT(atomic_uchar, unsigned char, \
115 ORDER_READ, ORDER_STORE, ORDER_RMW); \
116 TEST_ATOMIC_TYPE_EXPLICIT(atomic_schar, signed char, \
117 ORDER_READ, ORDER_STORE, ORDER_RMW); \
118 TEST_ATOMIC_TYPE_EXPLICIT(atomic_short, short, \
119 ORDER_READ, ORDER_STORE, ORDER_RMW); \
120 TEST_ATOMIC_TYPE_EXPLICIT(atomic_ushort, unsigned short, \
121 ORDER_READ, ORDER_STORE, ORDER_RMW); \
122 TEST_ATOMIC_TYPE_EXPLICIT(atomic_int, int, \
123 ORDER_READ, ORDER_STORE, ORDER_RMW); \
124 TEST_ATOMIC_TYPE_EXPLICIT(atomic_uint, unsigned int, \
125 ORDER_READ, ORDER_STORE, ORDER_RMW); \
126 TEST_ATOMIC_TYPE_EXPLICIT(atomic_long, long int, \
127 ORDER_READ, ORDER_STORE, ORDER_RMW); \
128 TEST_ATOMIC_TYPE_EXPLICIT(atomic_ulong, unsigned long int, \
129 ORDER_READ, ORDER_STORE, ORDER_RMW); \
130 TEST_ATOMIC_TYPE_EXPLICIT(atomic_llong, long long int, \
131 ORDER_READ, ORDER_STORE, ORDER_RMW); \
132 TEST_ATOMIC_TYPE_EXPLICIT(atomic_ullong, unsigned long long int, \
133 ORDER_READ, ORDER_STORE, ORDER_RMW); \
134 TEST_ATOMIC_TYPE_EXPLICIT(atomic_size_t, size_t, \
135 ORDER_READ, ORDER_STORE, ORDER_RMW); \
136 TEST_ATOMIC_TYPE_EXPLICIT(atomic_ptrdiff_t, ptrdiff_t, \
137 ORDER_READ, ORDER_STORE, ORDER_RMW); \
138 TEST_ATOMIC_TYPE_EXPLICIT(atomic_intmax_t, intmax_t, \
139 ORDER_READ, ORDER_STORE, ORDER_RMW); \
140 TEST_ATOMIC_TYPE_EXPLICIT(atomic_uintmax_t, uintmax_t, \
141 ORDER_READ, ORDER_STORE, ORDER_RMW); \
142 TEST_ATOMIC_TYPE_EXPLICIT(atomic_intptr_t, intptr_t, \
143 ORDER_READ, ORDER_STORE, ORDER_RMW); \
144 TEST_ATOMIC_TYPE_EXPLICIT(atomic_uintptr_t, uintptr_t, \
145 ORDER_READ, ORDER_STORE, ORDER_RMW); \
146 TEST_ATOMIC_TYPE_EXPLICIT(atomic_uint8_t, uint8_t, \
147 ORDER_READ, ORDER_STORE, ORDER_RMW); \
148 TEST_ATOMIC_TYPE_EXPLICIT(atomic_int8_t, int8_t, \
149 ORDER_READ, ORDER_STORE, ORDER_RMW); \
150 TEST_ATOMIC_TYPE_EXPLICIT(atomic_uint16_t, uint16_t, \
151 ORDER_READ, ORDER_STORE, ORDER_RMW); \
152 TEST_ATOMIC_TYPE_EXPLICIT(atomic_int16_t, int16_t, \
153 ORDER_READ, ORDER_STORE, ORDER_RMW); \
154 TEST_ATOMIC_TYPE_EXPLICIT(atomic_uint32_t, uint32_t, \
155 ORDER_READ, ORDER_STORE, ORDER_RMW); \
156 TEST_ATOMIC_TYPE_EXPLICIT(atomic_int32_t, int32_t, \
157 ORDER_READ, ORDER_STORE, ORDER_RMW); \
161 test_atomic_flag(void)
163 atomic_flag flag = ATOMIC_FLAG_INIT;
164 ovs_assert(atomic_flag_test_and_set(&flag) == false);
165 ovs_assert(atomic_flag_test_and_set(&flag) == true);
166 atomic_flag_clear(&flag);
167 ovs_assert(atomic_flag_test_and_set(&flag) == false);
173 atomic_uint32_t count;
175 ATOMIC(uint32_t *) data;
178 static ATOMIC(struct atomic_aux *) paux = ATOMIC_VAR_INIT(NULL);
179 static struct atomic_aux *auxes = NULL;
181 #define ATOMIC_ITEM_COUNT 1000000
184 atomic_consumer(void * arg1 OVS_UNUSED)
186 struct atomic_aux *old_aux = NULL;
190 struct atomic_aux *aux;
193 /* Wait for a new item. We may not be fast enough to process every
194 * item, but we are guaranteed to see the last one. */
196 atomic_read_explicit(&paux, &aux, memory_order_consume);
197 } while (aux == old_aux);
200 atomic_read_explicit(&aux->count, &count, memory_order_relaxed);
201 ovs_assert(b == count + 42);
204 } while (count < ATOMIC_ITEM_COUNT - 1);
210 atomic_producer(void * arg1 OVS_UNUSED)
214 for (i = 0; i < ATOMIC_ITEM_COUNT; i++) {
215 struct atomic_aux *aux = &auxes[i];
217 aux->count = ATOMIC_VAR_INIT(i);
220 /* Publish the new item. */
221 atomic_store_explicit(&paux, aux, memory_order_release);
230 pthread_t reader, writer;
232 atomic_init(&paux, NULL);
234 auxes = xmalloc(sizeof *auxes * ATOMIC_ITEM_COUNT);
236 reader = ovs_thread_create("consumer", atomic_consumer, NULL);
237 writer = ovs_thread_create("producer", atomic_producer, NULL);
239 xpthread_join(reader, NULL);
240 xpthread_join(writer, NULL);
246 atomic_reader(void *aux_)
248 struct atomic_aux *aux = aux_;
253 /* Non-synchronized add. */
254 atomic_add_explicit(&aux->count, 1, &count, memory_order_relaxed);
257 atomic_read_explicit(&aux->data, &data, memory_order_acquire);
260 ovs_assert(*data == a && *data == aux->b && a == aux->b);
262 atomic_read_explicit(&aux->count, &count, memory_order_relaxed);
264 ovs_assert(count == 2 * a && count == 2 * aux->b && count == 2 * *data);
266 atomic_store_explicit(&aux->data, NULL, memory_order_release);
267 } while (count < 2 * ATOMIC_ITEM_COUNT);
273 atomic_writer(void *aux_)
275 struct atomic_aux *aux = aux_;
280 for (i = 0; i < ATOMIC_ITEM_COUNT; i++) {
281 /* Wait for the reader to be done with the data. */
283 atomic_read_explicit(&aux->data, &data, memory_order_acquire);
287 atomic_add_explicit(&aux->count, 1, &old_count, memory_order_relaxed);
289 atomic_store_explicit(&aux->data,
290 (i & 1) ? &aux->b : &a, memory_order_release);
299 pthread_t reader, writer;
300 struct atomic_aux *aux = xmalloc(sizeof *aux);
305 aux->count = ATOMIC_VAR_INIT(0);
306 atomic_init(&aux->data, NULL);
308 reader = ovs_thread_create("reader", atomic_reader, aux);
309 writer = ovs_thread_create("writer", atomic_writer, aux);
311 xpthread_join(reader, NULL);
312 xpthread_join(writer, NULL);
317 test_atomic_plain(void)
319 TEST_ATOMIC_TYPE(atomic_char, char);
320 TEST_ATOMIC_TYPE(atomic_uchar, unsigned char);
321 TEST_ATOMIC_TYPE(atomic_schar, signed char);
322 TEST_ATOMIC_TYPE(atomic_short, short);
323 TEST_ATOMIC_TYPE(atomic_ushort, unsigned short);
324 TEST_ATOMIC_TYPE(atomic_int, int);
325 TEST_ATOMIC_TYPE(atomic_uint, unsigned int);
326 TEST_ATOMIC_TYPE(atomic_long, long int);
327 TEST_ATOMIC_TYPE(atomic_ulong, unsigned long int);
328 TEST_ATOMIC_TYPE(atomic_llong, long long int);
329 TEST_ATOMIC_TYPE(atomic_ullong, unsigned long long int);
330 TEST_ATOMIC_TYPE(atomic_size_t, size_t);
331 TEST_ATOMIC_TYPE(atomic_ptrdiff_t, ptrdiff_t);
332 TEST_ATOMIC_TYPE(atomic_intmax_t, intmax_t);
333 TEST_ATOMIC_TYPE(atomic_uintmax_t, uintmax_t);
334 TEST_ATOMIC_TYPE(atomic_intptr_t, intptr_t);
335 TEST_ATOMIC_TYPE(atomic_uintptr_t, uintptr_t);
336 TEST_ATOMIC_TYPE(atomic_uint8_t, uint8_t);
337 TEST_ATOMIC_TYPE(atomic_int8_t, int8_t);
338 TEST_ATOMIC_TYPE(atomic_uint16_t, uint16_t);
339 TEST_ATOMIC_TYPE(atomic_int16_t, int16_t);
340 TEST_ATOMIC_TYPE(atomic_uint32_t, uint32_t);
341 TEST_ATOMIC_TYPE(atomic_int32_t, int32_t);
345 test_atomic_relaxed(void)
347 TEST_ATOMIC_ORDER(memory_order_relaxed, memory_order_relaxed,
348 memory_order_relaxed);
352 test_atomic_consume(void)
354 TEST_ATOMIC_ORDER(memory_order_consume, memory_order_release,
355 memory_order_release);
359 test_atomic_acquire(void)
361 TEST_ATOMIC_ORDER(memory_order_acquire, memory_order_release,
362 memory_order_release);
366 test_atomic_acq_rel(void)
368 TEST_ATOMIC_ORDER(memory_order_acquire, memory_order_release,
369 memory_order_acq_rel);
373 test_atomic_seq_cst(void)
375 TEST_ATOMIC_ORDER(memory_order_seq_cst, memory_order_seq_cst,
376 memory_order_seq_cst);
380 test_atomic_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
383 test_atomic_relaxed();
384 test_atomic_consume();
385 test_atomic_acquire();
386 test_atomic_acq_rel();
387 test_atomic_seq_cst();
395 OVSTEST_REGISTER("test-atomic", test_atomic_main);