Debian Codegraph @

gcc/testsuite/gcc.dg/simulate-thread/atomic-load-longlong.c

raw
1 /* { dg-do link } */
2 /* { dg-require-effective-target sync_long_long_runtime } */
3 /* { dg-options "" } */
4 /* { dg-options "-march=pentium" { target { { i?86-*-* x86_64-*-* } && ia32 } } } */
5 /* { dg-final { simulate-thread } } */
6
7
8 #include <stdio.h>
9 #include "simulate-thread.h"
10
11
12 /* Testing load for atomicity is a little trickier.
13
14 Set up the atomic value so that it changes value after every instruction
15 is executed.
16
17 Simply alternating between 2 values wouldn't be sufficient since a load of
18 one part, followed by the load of the second part 2 instructions later would
19 appear to be valid.
20
21 set up a table of 16 values which change a bit in every byte of the value
22 each time, this will give us a 16 instruction cycle before repetition
23 kicks in, which should be sufficient to detect any issues. Just to be sure,
24 we also change the table cycle size during execution.
25
26 The end result is that all loads should always get one of the values from
27 the table. Any other pattern means the load failed. */
28
29 unsigned long long ret;
30 unsigned long long value = 0;
31 unsigned long long result = 0;
32 unsigned long long table[16] = {
33 0x0000000000000000,
34 0x1111111111111111,
35 0x2222222222222222,
36 0x3333333333333333,
37 0x4444444444444444,
38 0x5555555555555555,
39 0x6666666666666666,
40 0x7777777777777777,
41 0x8888888888888888,
42 0x9999999999999999,
43 0xAAAAAAAAAAAAAAAA,
44 0xBBBBBBBBBBBBBBBB,
45 0xCCCCCCCCCCCCCCCC,
46 0xDDDDDDDDDDDDDDDD,
47 0xEEEEEEEEEEEEEEEE,
48 0xFFFFFFFFFFFFFFFF
49 };
50
51 int table_cycle_size = 16;
52
53 /* Return 0 if 'result' is a valid value to have loaded. */
54 __attribute__((noinline))
55 int verify_result ()
56 {
57 int x;
58 int found = 0;
59
60 /* Check entire table for valid values. */
61 for (x = 0; x < 16 ; x++)
62 if (result == table[x])
63 {
64 found = 1;
65 break;
66 }
67
68 if (!found)
69 printf("FAIL: Invalid result returned from fetch\n");
70
71 return !found;
72 }
73
74 /* Iterate VALUE through the different valid values. */
75 __attribute__((noinline))
76 void simulate_thread_other_threads ()
77 {
78 static int current = 0;
79
80 if (++current >= table_cycle_size)
81 current = 0;
82 value = table[current];
83 }
84
85 __attribute__((noinline))
86 int simulate_thread_step_verify ()
87 {
88 return verify_result ();
89 }
90
91 __attribute__((noinline))
92 int simulate_thread_final_verify ()
93 {
94 return verify_result ();
95 }
96
97 __attribute__((noinline))
98 void simulate_thread_main()
99 {
100 int x;
101
102 /* Execute loads with value changing at various cyclic values. */
103 for (table_cycle_size = 16; table_cycle_size > 4 ; table_cycle_size--)
104 {
105 ret = __atomic_load_n (&value, __ATOMIC_SEQ_CST);
106 /* In order to verify the returned value (which is not atomic), it needs
107 to be atomically stored into another variable and check that. */
108 __atomic_store_n (&result, ret, __ATOMIC_SEQ_CST);
109
110 /* Execute the fetch/store a couple of times just to ensure the cycles
111 have a chance to be interesting. */
112 ret = __atomic_load_n (&value, __ATOMIC_SEQ_CST);
113 __atomic_store_n (&result, ret, __ATOMIC_SEQ_CST);
114 }
115 }
116
117 int
118 main()
119 {
120 simulate_thread_main ();
121 simulate_thread_done ();
122 return 0;
123 }