]> git.ipfire.org Git - thirdparty/glibc.git/blob - linuxthreads/Examples/ex14.c
Test for stack alignment.
[thirdparty/glibc.git] / linuxthreads / Examples / ex14.c
1 /* Test of POSIX barriers. */
2
3 #include <pthread.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7
8 #define NTHREADS 20
9
10 #define ROUNDS 20
11
12 static pthread_barrier_t barriers[NTHREADS];
13
14 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
15 static int counters[NTHREADS];
16 static int serial[NTHREADS];
17
18 static void *
19 worker (void *arg)
20 {
21 void *result = NULL;
22 int nr = (long int) arg;
23 int i;
24
25 for (i = 0; i < ROUNDS; ++i)
26 {
27 int j;
28 int retval;
29
30 if (nr == 0)
31 {
32 memset (counters, '\0', sizeof (counters));
33 memset (serial, '\0', sizeof (serial));
34 }
35
36 retval = pthread_barrier_wait (&barriers[NTHREADS - 1]);
37 if (retval != 0 && retval != PTHREAD_BARRIER_SERIAL_THREAD)
38 {
39 printf ("thread %d failed to wait for all the others\n", nr);
40 result = (void *) 1;
41 }
42
43 for (j = nr; j < NTHREADS; ++j)
44 {
45 /* Increment the counter for this round. */
46 pthread_mutex_lock (&lock);
47 ++counters[j];
48 pthread_mutex_unlock (&lock);
49
50 /* Wait for the rest. */
51 retval = pthread_barrier_wait (&barriers[j]);
52
53 /* Test the result. */
54 if (nr == 0 && counters[j] != j + 1)
55 {
56 printf ("barrier in round %d released but count is %d\n",
57 j, counters[j]);
58 result = (void *) 1;
59 }
60
61 if (retval != 0)
62 {
63 if (retval != PTHREAD_BARRIER_SERIAL_THREAD)
64 {
65 printf ("thread %d in round %d has nonzero return value != PTHREAD_BARRIER_SERIAL_THREAD\n",
66 nr, j);
67 result = (void *) 1;
68 }
69 else
70 {
71 pthread_mutex_lock (&lock);
72 ++serial[j];
73 pthread_mutex_unlock (&lock);
74 }
75 }
76
77 /* Wait for the rest again. */
78 retval = pthread_barrier_wait (&barriers[j]);
79
80 /* Now we can check whether exactly one thread was serializing. */
81 if (nr == 0 && serial[j] != 1)
82 {
83 printf ("not exactly one serial thread in round %d\n", j);
84 result = (void *) 1;
85 }
86 }
87 }
88
89 return result;
90 }
91
92
93 #define TEST_FUNCTION do_test ()
94 #define TIMEOUT 60
95 static int
96 do_test (void)
97 {
98 pthread_t threads[NTHREADS];
99 int i;
100 void *res;
101 int result = 0;
102
103 /* Initialized the barrier variables. */
104 for (i = 0; i < NTHREADS; ++i)
105 if (pthread_barrier_init (&barriers[i], NULL, i + 1) != 0)
106 {
107 printf ("Failed to initialize barrier %d\n", i);
108 exit (1);
109 }
110
111 /* Start the threads. */
112 for (i = 0; i < NTHREADS; ++i)
113 if (pthread_create (&threads[i], NULL, worker, (void *) (long int) i) != 0)
114 {
115 printf ("Failed to start thread %d\n", i);
116 exit (1);
117 }
118
119 /* And wait for them. */
120 for (i = 0; i < NTHREADS; ++i)
121 if (pthread_join (threads[i], &res) != 0 || res != NULL)
122 {
123 printf ("thread %d returned a failure\n", i);
124 result = 1;
125 }
126
127 if (result == 0)
128 puts ("all OK");
129
130 return result;
131 }
132
133 #include "../test-skeleton.c"