OSDN Git Service

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