1 // SPDX-FileCopyrightText: 2009 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 // SPDX-License-Identifier: GPL-2.0-or-later
6 #include <urcu/uatomic.h>
12 #define BYTE_PER_LONG (sizeof(unsigned long) / sizeof(unsigned char))
13 #define SHORT_PER_LONG (sizeof(unsigned long) / sizeof(unsigned short))
14 #define INT_PER_LONG (sizeof(unsigned long) / sizeof(unsigned int))
17 #ifdef UATOMIC_HAS_ATOMIC_BYTE
18 unsigned char c
[BYTE_PER_LONG
];
20 #ifdef UATOMIC_HAS_ATOMIC_SHORT
21 unsigned short s
[SHORT_PER_LONG
];
23 unsigned int i
[INT_PER_LONG
];
27 static struct testvals vals
;
29 #define do_test(ptr) \
31 __typeof__(*(ptr)) v; \
33 uatomic_add(ptr, 10); \
34 ok1(uatomic_read(ptr) == 10); \
36 uatomic_add(ptr, -11UL); \
37 ok1(uatomic_read(ptr) == (__typeof__(*(ptr)))-1UL); \
39 v = uatomic_cmpxchg(ptr, -1UL, 22); \
40 ok1(uatomic_read(ptr) == 22); \
41 ok1(v == (__typeof__(*(ptr)))-1UL); \
43 v = uatomic_cmpxchg(ptr, 33, 44); \
44 ok1(uatomic_read(ptr) == 22); \
47 v = uatomic_xchg(ptr, 55); \
48 ok1(uatomic_read(ptr) == 55); \
51 uatomic_set(ptr, 22); \
53 ok1(uatomic_read(ptr) == 23); \
56 ok1(uatomic_read(ptr) == 22); \
58 v = uatomic_add_return(ptr, 74); \
60 ok1(uatomic_read(ptr) == 96); \
62 uatomic_or(ptr, 58); \
63 ok1(uatomic_read(ptr) == 122); \
65 v = uatomic_sub_return(ptr, 1); \
68 uatomic_sub(ptr, (unsigned int) 2); \
69 ok1(uatomic_read(ptr) == 119); \
73 ok1(uatomic_read(ptr) == 121); \
75 uatomic_and(ptr, 129); \
76 ok1(uatomic_read(ptr) == 1); \
82 int nr_run
= INT_PER_LONG
+ 1;
85 #ifdef UATOMIC_HAS_ATOMIC_BYTE
86 nr_run
+= BYTE_PER_LONG
;
88 #ifdef UATOMIC_HAS_ATOMIC_SHORT
89 nr_run
+= SHORT_PER_LONG
;
92 plan_tests(nr_run
* NR_TESTS
);
93 #ifdef UATOMIC_HAS_ATOMIC_BYTE
94 for (i
= 0; i
< BYTE_PER_LONG
; i
++) {
95 diag("Test atomic ops on byte with %lu byte offset from long alignment",
100 #ifdef UATOMIC_HAS_ATOMIC_SHORT
101 for (i
= 0; i
< SHORT_PER_LONG
; i
++) {
102 diag("Test atomic ops on short with %lu byte offset from long alignment",
103 i
* sizeof(unsigned short));
107 for (i
= 0; i
< INT_PER_LONG
; i
++) {
108 diag("Test atomic ops on int with %lu byte offset from long alignment",
109 i
* sizeof(unsigned int));
112 diag("Test atomic ops on long");
115 return exit_status();
This page took 0.03211 seconds and 5 git commands to generate.