annotate gcc/testsuite/gcc.dg/di-sync-multithread.c @ 144:8f4e72ab4e11

fix segmentation fault caused by nothing next cur_op to end
author Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Sun, 23 Dec 2018 21:23:56 +0900
parents 04ced10e8804
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* { dg-do run } */
kono
parents:
diff changeset
2 /* { dg-require-effective-target sync_long_long_runtime } */
kono
parents:
diff changeset
3 /* { dg-require-effective-target pthread_h } */
kono
parents:
diff changeset
4 /* { dg-require-effective-target pthread } */
kono
parents:
diff changeset
5 /* { dg-options "-pthread -std=gnu99" } */
kono
parents:
diff changeset
6 /* { dg-additional-options "-march=pentium" { target { { i?86-*-* x86_64-*-* } && ia32 } } } */
kono
parents:
diff changeset
7
kono
parents:
diff changeset
8 /* test of long long atomic ops performed in parallel in 3 pthreads
kono
parents:
diff changeset
9 david.gilbert@linaro.org */
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 #include <pthread.h>
kono
parents:
diff changeset
12 #include <unistd.h>
kono
parents:
diff changeset
13 #ifdef _WIN32
kono
parents:
diff changeset
14 #include <windows.h>
kono
parents:
diff changeset
15 #endif
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 /*#define DEBUGIT 1 */
kono
parents:
diff changeset
18
kono
parents:
diff changeset
19 #ifdef DEBUGIT
kono
parents:
diff changeset
20 #include <stdio.h>
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 #define DOABORT(x,...) {\
kono
parents:
diff changeset
23 fprintf (stderr, x, __VA_ARGS__); fflush (stderr); abort ();\
kono
parents:
diff changeset
24 }
kono
parents:
diff changeset
25
kono
parents:
diff changeset
26 #else
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 #define DOABORT(x,...) abort ();
kono
parents:
diff changeset
29
kono
parents:
diff changeset
30 #endif
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 /* Passed to each thread to describe which bits it is going to work on. */
kono
parents:
diff changeset
33 struct threadwork {
kono
parents:
diff changeset
34 unsigned long long count; /* incremented each time the worker loops. */
kono
parents:
diff changeset
35 unsigned int thread; /* ID */
kono
parents:
diff changeset
36 unsigned int addlsb; /* 8 bit */
kono
parents:
diff changeset
37 unsigned int logic1lsb; /* 5 bit */
kono
parents:
diff changeset
38 unsigned int logic2lsb; /* 8 bit */
kono
parents:
diff changeset
39 };
kono
parents:
diff changeset
40
kono
parents:
diff changeset
41 /* The shared word where all the atomic work is done. */
kono
parents:
diff changeset
42 static volatile long long workspace;
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 /* A shared word to tell the workers to quit when non-0. */
kono
parents:
diff changeset
45 static long long doquit;
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 extern void abort (void);
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* Note this test doesn't test the return values much. */
kono
parents:
diff changeset
50 void*
kono
parents:
diff changeset
51 worker (void* data)
kono
parents:
diff changeset
52 {
kono
parents:
diff changeset
53 struct threadwork *tw = (struct threadwork*)data;
kono
parents:
diff changeset
54 long long add1bit = 1ll << tw->addlsb;
kono
parents:
diff changeset
55 long long logic1bit = 1ll << tw->logic1lsb;
kono
parents:
diff changeset
56 long long logic2bit = 1ll << tw->logic2lsb;
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 /* Clear the bits we use. */
kono
parents:
diff changeset
59 __sync_and_and_fetch (&workspace, ~(0xffll * add1bit));
kono
parents:
diff changeset
60 __sync_fetch_and_and (&workspace, ~(0x1fll * logic1bit));
kono
parents:
diff changeset
61 __sync_fetch_and_and (&workspace, ~(0xffll * logic2bit));
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 do
kono
parents:
diff changeset
64 {
kono
parents:
diff changeset
65 long long tmp1, tmp2, tmp3;
kono
parents:
diff changeset
66 /* OK, lets try and do some stuff to the workspace - by the end
kono
parents:
diff changeset
67 of the main loop our area should be the same as it is now - i.e. 0. */
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 /* Push the arithmetic section up to 128 - one of the threads will
kono
parents:
diff changeset
70 case this to carry across the 32bit boundary. */
kono
parents:
diff changeset
71 for (tmp2 = 0; tmp2 < 64; tmp2++)
kono
parents:
diff changeset
72 {
kono
parents:
diff changeset
73 /* Add 2 using the two different adds. */
kono
parents:
diff changeset
74 tmp1 = __sync_add_and_fetch (&workspace, add1bit);
kono
parents:
diff changeset
75 tmp3 = __sync_fetch_and_add (&workspace, add1bit);
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 /* The value should be the intermediate add value in both cases. */
kono
parents:
diff changeset
78 if ((tmp1 & (add1bit * 0xff)) != (tmp3 & (add1bit * 0xff)))
kono
parents:
diff changeset
79 DOABORT ("Mismatch of add intermediates on thread %d "
kono
parents:
diff changeset
80 "workspace=0x%llx tmp1=0x%llx "
kono
parents:
diff changeset
81 "tmp2=0x%llx tmp3=0x%llx\n",
kono
parents:
diff changeset
82 tw->thread, workspace, tmp1, tmp2, tmp3);
kono
parents:
diff changeset
83 }
kono
parents:
diff changeset
84
kono
parents:
diff changeset
85 /* Set the logic bits. */
kono
parents:
diff changeset
86 tmp2=__sync_or_and_fetch (&workspace,
kono
parents:
diff changeset
87 0x1fll * logic1bit | 0xffll * logic2bit);
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 /* Check the logic bits are set and the arithmetic value is correct. */
kono
parents:
diff changeset
90 if ((tmp2 & (0x1fll * logic1bit | 0xffll * logic2bit
kono
parents:
diff changeset
91 | 0xffll * add1bit))
kono
parents:
diff changeset
92 != (0x1fll * logic1bit | 0xffll * logic2bit | 0x80ll * add1bit))
kono
parents:
diff changeset
93 DOABORT ("Midloop check failed on thread %d "
kono
parents:
diff changeset
94 "workspace=0x%llx tmp2=0x%llx "
kono
parents:
diff changeset
95 "masktmp2=0x%llx expected=0x%llx\n",
kono
parents:
diff changeset
96 tw->thread, workspace, tmp2,
kono
parents:
diff changeset
97 tmp2 & (0x1fll * logic1bit | 0xffll * logic2bit |
kono
parents:
diff changeset
98 0xffll * add1bit),
kono
parents:
diff changeset
99 (0x1fll * logic1bit | 0xffll * logic2bit | 0x80ll * add1bit));
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 /* Pull the arithmetic set back down to 0 - again this should cause a
kono
parents:
diff changeset
102 carry across the 32bit boundary in one thread. */
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 for (tmp2 = 0; tmp2 < 64; tmp2++)
kono
parents:
diff changeset
105 {
kono
parents:
diff changeset
106 /* Subtract 2 using the two different subs. */
kono
parents:
diff changeset
107 tmp1=__sync_sub_and_fetch (&workspace, add1bit);
kono
parents:
diff changeset
108 tmp3=__sync_fetch_and_sub (&workspace, add1bit);
kono
parents:
diff changeset
109
kono
parents:
diff changeset
110 /* The value should be the intermediate sub value in both cases. */
kono
parents:
diff changeset
111 if ((tmp1 & (add1bit * 0xff)) != (tmp3 & (add1bit * 0xff)))
kono
parents:
diff changeset
112 DOABORT ("Mismatch of sub intermediates on thread %d "
kono
parents:
diff changeset
113 "workspace=0x%llx tmp1=0x%llx "
kono
parents:
diff changeset
114 "tmp2=0x%llx tmp3=0x%llx\n",
kono
parents:
diff changeset
115 tw->thread, workspace, tmp1, tmp2, tmp3);
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118
kono
parents:
diff changeset
119 /* Clear the logic bits. */
kono
parents:
diff changeset
120 __sync_fetch_and_xor (&workspace, 0x1fll * logic1bit);
kono
parents:
diff changeset
121 tmp3=__sync_and_and_fetch (&workspace, ~(0xffll * logic2bit));
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 /* The logic bits and the arithmetic bits should be zero again. */
kono
parents:
diff changeset
124 if (tmp3 & (0x1fll * logic1bit | 0xffll * logic2bit | 0xffll * add1bit))
kono
parents:
diff changeset
125 DOABORT ("End of worker loop; bits none 0 on thread %d "
kono
parents:
diff changeset
126 "workspace=0x%llx tmp3=0x%llx "
kono
parents:
diff changeset
127 "mask=0x%llx maskedtmp3=0x%llx\n",
kono
parents:
diff changeset
128 tw->thread, workspace, tmp3, (0x1fll * logic1bit |
kono
parents:
diff changeset
129 0xffll * logic2bit | 0xffll * add1bit),
kono
parents:
diff changeset
130 tmp3 & (0x1fll * logic1bit | 0xffll * logic2bit | 0xffll * add1bit));
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 __sync_add_and_fetch (&tw->count, 1);
kono
parents:
diff changeset
133 }
kono
parents:
diff changeset
134 while (!__sync_bool_compare_and_swap (&doquit, 1, 1));
kono
parents:
diff changeset
135
kono
parents:
diff changeset
136 pthread_exit (0);
kono
parents:
diff changeset
137 }
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 int
kono
parents:
diff changeset
140 main ()
kono
parents:
diff changeset
141 {
kono
parents:
diff changeset
142 /* We have 3 threads doing three sets of operations, an 8 bit
kono
parents:
diff changeset
143 arithmetic field, a 5 bit logic field and an 8 bit logic
kono
parents:
diff changeset
144 field (just to pack them all in).
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 6 5 4 4 3 2 1
kono
parents:
diff changeset
147 3 6 8 0 2 4 6 8 0
kono
parents:
diff changeset
148 |...,...|...,...|...,...|...,...|...,...|...,...|...,...|...,...
kono
parents:
diff changeset
149 - T0 -- T1 -- T2 --T2 -- T0 -*- T2-- T1-- T1 -***- T0-
kono
parents:
diff changeset
150 logic2 logic2 arith log2 arith log1 log1 arith log1
kono
parents:
diff changeset
151
kono
parents:
diff changeset
152 */
kono
parents:
diff changeset
153 unsigned int t;
kono
parents:
diff changeset
154 long long tmp;
kono
parents:
diff changeset
155 int err;
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 struct threadwork tw[3]={
kono
parents:
diff changeset
158 { 0ll, 0, 27, 0, 56 },
kono
parents:
diff changeset
159 { 0ll, 1, 8,16, 48 },
kono
parents:
diff changeset
160 { 0ll, 2, 40,21, 35 }
kono
parents:
diff changeset
161 };
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 pthread_t threads[3];
kono
parents:
diff changeset
164
kono
parents:
diff changeset
165 __sync_lock_release (&doquit);
kono
parents:
diff changeset
166
kono
parents:
diff changeset
167 /* Get the work space into a known value - All 1's. */
kono
parents:
diff changeset
168 __sync_lock_release (&workspace); /* Now all 0. */
kono
parents:
diff changeset
169 tmp = __sync_val_compare_and_swap (&workspace, 0, -1ll);
kono
parents:
diff changeset
170 if (tmp!=0)
kono
parents:
diff changeset
171 DOABORT ("Initial __sync_val_compare_and_swap wasn't 0 workspace=0x%llx "
kono
parents:
diff changeset
172 "tmp=0x%llx\n", workspace,tmp);
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 for (t = 0; t < 3; t++)
kono
parents:
diff changeset
175 {
kono
parents:
diff changeset
176 err=pthread_create (&threads[t], NULL , worker, &tw[t]);
kono
parents:
diff changeset
177 if (err) DOABORT ("pthread_create failed on thread %d with error %d\n",
kono
parents:
diff changeset
178 t, err);
kono
parents:
diff changeset
179 };
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 #ifdef _WIN32
kono
parents:
diff changeset
182 Sleep (5000);
kono
parents:
diff changeset
183 #else
kono
parents:
diff changeset
184 sleep (5);
kono
parents:
diff changeset
185 #endif
kono
parents:
diff changeset
186
kono
parents:
diff changeset
187 /* Stop please. */
kono
parents:
diff changeset
188 __sync_lock_test_and_set (&doquit, 1ll);
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 for (t = 0; t < 3; t++)
kono
parents:
diff changeset
191 {
kono
parents:
diff changeset
192 err=pthread_join (threads[t], NULL);
kono
parents:
diff changeset
193 if (err)
kono
parents:
diff changeset
194 DOABORT ("pthread_join failed on thread %d with error %d\n", t, err);
kono
parents:
diff changeset
195 };
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 __sync_synchronize ();
kono
parents:
diff changeset
198
kono
parents:
diff changeset
199 /* OK, so all the workers have finished -
kono
parents:
diff changeset
200 the workers should have zero'd their workspace, the unused areas
kono
parents:
diff changeset
201 should still be 1. */
kono
parents:
diff changeset
202 if (!__sync_bool_compare_and_swap (&workspace, 0x040000e0ll, 0))
kono
parents:
diff changeset
203 DOABORT ("End of run workspace mismatch, got %llx\n", workspace);
kono
parents:
diff changeset
204
kono
parents:
diff changeset
205 /* All the workers should have done some work. */
kono
parents:
diff changeset
206 for (t = 0; t < 3; t++)
kono
parents:
diff changeset
207 {
kono
parents:
diff changeset
208 if (tw[t].count == 0) DOABORT ("Worker %d gave 0 count\n", t);
kono
parents:
diff changeset
209 };
kono
parents:
diff changeset
210
kono
parents:
diff changeset
211 return 0;
kono
parents:
diff changeset
212 }
kono
parents:
diff changeset
213