Debian Codegraph @

tests/test_atomic_ops.c

raw
1 /*
2 * Copyright (c) 2017 Ivan Maidanski
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /* Minimal testing of atomic operations used by the BDWGC. Primary use */
15 /* is to determine whether compiler atomic intrinsics can be relied on. */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif
20
21 #include <stdio.h>
22
23 #if defined(GC_BUILTIN_ATOMIC) || defined(GC_THREADS)
24
25 # include <stdlib.h>
26
27 # ifdef PARALLEL_MARK
28 # define AO_REQUIRE_CAS
29 # if !defined(__GNUC__) && !defined(AO_ASSUME_WINDOWS98)
30 # define AO_ASSUME_WINDOWS98
31 # endif
32 # endif
33
34 # include "private/gc_atomic_ops.h"
35
36 # define TA_assert(e) \
37 if (!(e)) { \
38 fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \
39 exit(-1); \
40 }
41
42 int main(void) {
43 AO_t x = 13;
44 # if defined(AO_HAVE_char_load) || defined(AO_HAVE_char_store)
45 unsigned char c = 117;
46 # endif
47 # ifdef AO_HAVE_test_and_set_acquire
48 AO_TS_t z = AO_TS_INITIALIZER;
49
50 TA_assert(AO_test_and_set_acquire(&z) == AO_TS_CLEAR);
51 TA_assert(AO_test_and_set_acquire(&z) == AO_TS_SET);
52 AO_CLEAR(&z);
53 # endif
54 AO_compiler_barrier();
55 # ifdef AO_HAVE_nop_full
56 AO_nop_full();
57 # endif
58 # ifdef AO_HAVE_char_load
59 TA_assert(AO_char_load(&c) == 117);
60 # endif
61 # ifdef AO_HAVE_char_store
62 AO_char_store(&c, 119);
63 TA_assert(c == 119);
64 # endif
65 # ifdef AO_HAVE_load_acquire
66 TA_assert(AO_load_acquire(&x) == 13);
67 # endif
68 # if defined(AO_HAVE_fetch_and_add) && defined(AO_HAVE_fetch_and_add1)
69 TA_assert(AO_fetch_and_add(&x, 42) == 13);
70 TA_assert(AO_fetch_and_add(&x, (AO_t)(-43)) == 55);
71 TA_assert(AO_fetch_and_add1(&x) == 12);
72 # endif
73 # ifdef AO_HAVE_compare_and_swap_release
74 TA_assert(!AO_compare_and_swap(&x, 14, 42));
75 TA_assert(x == 13);
76 TA_assert(AO_compare_and_swap_release(&x, 13, 42));
77 TA_assert(x == 42);
78 # else
79 if (*(volatile AO_t *)&x == 13)
80 *(volatile AO_t *)&x = 42;
81 # endif
82 # ifdef AO_HAVE_or
83 AO_or(&x, 66);
84 TA_assert(x == 106);
85 # endif
86 # ifdef AO_HAVE_store_release
87 AO_store_release(&x, 113);
88 TA_assert(x == 113);
89 # endif
90 return 0;
91 }
92
93 #else
94
95 int main(void)
96 {
97 printf("test_atomic_ops skipped\n");
98 return 0;
99 }
100
101 #endif