| 1 | #include <stdio.h> |
| 2 | #include <arch_atomic.h> |
| 3 | #include <assert.h> |
| 4 | |
| 5 | #if (defined(__i386__) || defined(__x86_64__)) |
| 6 | #define HAS_ATOMIC_BYTE |
| 7 | #define HAS_ATOMIC_SHORT |
| 8 | #endif |
| 9 | |
| 10 | struct testvals { |
| 11 | #ifdef HAS_ATOMIC_BYTE |
| 12 | unsigned char c; |
| 13 | #endif |
| 14 | #ifdef HAS_ATOMIC_SHORT |
| 15 | unsigned short s; |
| 16 | #endif |
| 17 | unsigned int i; |
| 18 | unsigned long l; |
| 19 | }; |
| 20 | |
| 21 | static struct testvals vals; |
| 22 | |
| 23 | #define do_test(ptr) \ |
| 24 | do { \ |
| 25 | __typeof__(*(ptr)) v; \ |
| 26 | \ |
| 27 | atomic_add(ptr, 10); \ |
| 28 | assert(atomic_read(ptr) == 10); \ |
| 29 | atomic_add(ptr, -11UL); \ |
| 30 | assert(atomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \ |
| 31 | v = cmpxchg(ptr, -1UL, 22); \ |
| 32 | assert(atomic_read(ptr) == 22); \ |
| 33 | assert(v == (__typeof__(*(ptr)))-1UL); \ |
| 34 | v = cmpxchg(ptr, 33, 44); \ |
| 35 | assert(atomic_read(ptr) == 22); \ |
| 36 | assert(v == 22); \ |
| 37 | v = xchg(ptr, 55); \ |
| 38 | assert(atomic_read(ptr) == 55); \ |
| 39 | assert(v == 22); \ |
| 40 | atomic_set(ptr, 22); \ |
| 41 | atomic_inc(ptr); \ |
| 42 | assert(atomic_read(ptr) == 23); \ |
| 43 | atomic_dec(ptr); \ |
| 44 | assert(atomic_read(ptr) == 22); \ |
| 45 | v = atomic_add_return(ptr, 100); \ |
| 46 | assert(v == 122); \ |
| 47 | assert(atomic_read(ptr) == 122); \ |
| 48 | v = atomic_sub_return(ptr, 1); \ |
| 49 | assert(v == 121); \ |
| 50 | assert(atomic_read(ptr) == 121); \ |
| 51 | } while (0) |
| 52 | |
| 53 | int main(int argc, char **argv) |
| 54 | { |
| 55 | #ifdef HAS_ATOMIC_BYTE |
| 56 | do_test(&vals.c); |
| 57 | #endif |
| 58 | #ifdef HAS_ATOMIC_SHORT |
| 59 | do_test(&vals.s); |
| 60 | #endif |
| 61 | do_test(&vals.i); |
| 62 | do_test(&vals.l); |
| 63 | printf("Atomic ops test OK\n"); |
| 64 | |
| 65 | return 0; |
| 66 | } |