Commit | Line | Data |
---|---|---|
3282a76b MD |
1 | /* |
2 | * test_uatomic.c | |
3 | * | |
4 | * Userspace RCU library - test atomic operations | |
5 | * | |
6 | * Copyright February 2009 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; if not, write to the Free Software Foundation, Inc., | |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
21 | */ | |
22 | ||
a81b8e5e | 23 | #include <stdio.h> |
a81b8e5e | 24 | #include <assert.h> |
a2e7bf9c | 25 | #include <urcu/uatomic.h> |
a81b8e5e MD |
26 | |
27 | struct testvals { | |
95bc7fb9 | 28 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
a81b8e5e | 29 | unsigned char c; |
95bc7fb9 MD |
30 | #endif |
31 | #ifdef UATOMIC_HAS_ATOMIC_SHORT | |
a81b8e5e | 32 | unsigned short s; |
95bc7fb9 | 33 | #endif |
a81b8e5e MD |
34 | unsigned int i; |
35 | unsigned long l; | |
36 | }; | |
37 | ||
38 | static struct testvals vals; | |
39 | ||
87322fe8 MD |
40 | #define do_test(ptr) \ |
41 | do { \ | |
6edb297e | 42 | __typeof__(*(ptr)) v; \ |
87322fe8 | 43 | \ |
ec4e58a3 MD |
44 | uatomic_add(ptr, 10); \ |
45 | assert(uatomic_read(ptr) == 10); \ | |
46 | uatomic_add(ptr, -11UL); \ | |
47 | assert(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \ | |
48 | v = uatomic_cmpxchg(ptr, -1UL, 22); \ | |
49 | assert(uatomic_read(ptr) == 22); \ | |
6edb297e | 50 | assert(v == (__typeof__(*(ptr)))-1UL); \ |
ec4e58a3 MD |
51 | v = uatomic_cmpxchg(ptr, 33, 44); \ |
52 | assert(uatomic_read(ptr) == 22); \ | |
87322fe8 | 53 | assert(v == 22); \ |
ec4e58a3 MD |
54 | v = uatomic_xchg(ptr, 55); \ |
55 | assert(uatomic_read(ptr) == 55); \ | |
87322fe8 | 56 | assert(v == 22); \ |
ec4e58a3 MD |
57 | uatomic_set(ptr, 22); \ |
58 | uatomic_inc(ptr); \ | |
59 | assert(uatomic_read(ptr) == 23); \ | |
60 | uatomic_dec(ptr); \ | |
61 | assert(uatomic_read(ptr) == 22); \ | |
985b35b1 PB |
62 | v = uatomic_add_return(ptr, 74); \ |
63 | assert(v == 96); \ | |
64 | assert(uatomic_read(ptr) == 96); \ | |
65 | uatomic_or(ptr, 58); \ | |
ec4e58a3 MD |
66 | assert(uatomic_read(ptr) == 122); \ |
67 | v = uatomic_sub_return(ptr, 1); \ | |
0fad128b | 68 | assert(v == 121); \ |
e56d99bf MD |
69 | uatomic_sub(ptr, (unsigned int) 2); \ |
70 | assert(uatomic_read(ptr) == 119); \ | |
71 | uatomic_inc(ptr); \ | |
72 | uatomic_inc(ptr); \ | |
ec4e58a3 | 73 | assert(uatomic_read(ptr) == 121); \ |
bf33aaea PB |
74 | uatomic_and(ptr, 129); \ |
75 | assert(uatomic_read(ptr) == 1); \ | |
87322fe8 MD |
76 | } while (0) |
77 | ||
78 | int main(int argc, char **argv) | |
a81b8e5e | 79 | { |
f469d839 | 80 | #ifdef UATOMIC_HAS_ATOMIC_BYTE |
87322fe8 | 81 | do_test(&vals.c); |
4d78cb54 | 82 | #endif |
f469d839 | 83 | #ifdef UATOMIC_HAS_ATOMIC_SHORT |
87322fe8 | 84 | do_test(&vals.s); |
4d78cb54 | 85 | #endif |
87322fe8 MD |
86 | do_test(&vals.i); |
87 | do_test(&vals.l); | |
88 | printf("Atomic ops test OK\n"); | |
89 | ||
90 | return 0; | |
a81b8e5e | 91 | } |