Debian Codegraph @

gc/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 # endif
30
31 # include "private/gc_atomic_ops.h"
32
33 # define TA_assert(e) \
34 if (!(e)) { \
35 fprintf(stderr, "Assertion failure, line %d: " #e "\n", __LINE__); \
36 exit(-1); \
37 }
38
39 int main(void) {
40 AO_t x = 13;
41 # if defined(AO_HAVE_char_load) || defined(AO_HAVE_char_store)
42 unsigned char c = 117;
43 # endif
44 # ifdef AO_HAVE_test_and_set_acquire
45 AO_TS_t z = AO_TS_INITIALIZER;
46
47 TA_assert(AO_test_and_set_acquire(&z) == AO_TS_CLEAR);
48 TA_assert(AO_test_and_set_acquire(&z) == AO_TS_SET);
49 AO_CLEAR(&z);
50 # endif
51 AO_compiler_barrier();
52 # ifdef AO_HAVE_nop_full
53 AO_nop_full();
54 # endif
55 # ifdef AO_HAVE_char_load
56 TA_assert(AO_char_load(&c) == 117);
57 # endif
58 # ifdef AO_HAVE_char_store
59 AO_char_store(&c, 119);
60 TA_assert(c == 119);
61 # endif
62 # ifdef AO_HAVE_load_acquire
63 TA_assert(AO_load_acquire(&x) == 13);
64 # endif
65 # if defined(AO_HAVE_fetch_and_add) && defined(AO_HAVE_fetch_and_add1)
66 TA_assert(AO_fetch_and_add(&x, 42) == 13);
67 TA_assert(AO_fetch_and_add(&x, (AO_t)(-43)) == 55);
68 TA_assert(AO_fetch_and_add1(&x) == 12);
69 # endif
70 # ifdef AO_HAVE_compare_and_swap_release
71 TA_assert(!AO_compare_and_swap(&x, 14, 42));
72 TA_assert(x == 13);
73 TA_assert(AO_compare_and_swap_release(&x, 13, 42));
74 TA_assert(x == 42);
75 # else
76 if (*(volatile AO_t *)&x == 13)
77 *(volatile AO_t *)&x = 42;
78 # endif
79 # ifdef AO_HAVE_or
80 AO_or(&x, 66);
81 TA_assert(x == 106);
82 # endif
83 # ifdef AO_HAVE_store_release
84 AO_store_release(&x, 113);
85 TA_assert(x == 113);
86 # endif
87 return 0;
88 }
89
90 #else
91
92 int main(void)
93 {
94 printf("test_atomic_ops skipped\n");
95 return 0;
96 }
97
98 #endif