Debian Codegraph @

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

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