OSDN Git Service

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