]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/test/nptl/tst-cond2.c
update
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / test / nptl / tst-cond2.c
1 /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3    Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU Lesser General Public
16    License along with the GNU C Library; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <error.h>
20 #include <pthread.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23
24
25 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
26 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
27
28 static pthread_barrier_t bar;
29
30
31 static void *
32 tf (void *a)
33 {
34   int i = (long int) a;
35   int err;
36
37   printf ("child %d: lock\n", i);
38
39   err = pthread_mutex_lock (&mut);
40   if (err != 0)
41     error (EXIT_FAILURE, err, "locking in child failed");
42
43   printf ("child %d: sync\n", i);
44
45   int e = pthread_barrier_wait (&bar);
46   if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
47     {
48       puts ("child: barrier_wait failed");
49       exit (1);
50     }
51
52   printf ("child %d: wait\n", i);
53
54   err = pthread_cond_wait (&cond, &mut);
55   if (err != 0)
56     error (EXIT_FAILURE, err, "child %d: failed to wait", i);
57
58   printf ("child %d: woken up\n", i);
59
60   err = pthread_mutex_unlock (&mut);
61   if (err != 0)
62     error (EXIT_FAILURE, err, "child %d: unlock[2] failed", i);
63
64   printf ("child %d: done\n", i);
65
66   return NULL;
67 }
68
69
70 #define N 10
71
72
73 static int
74 do_test (void)
75 {
76   pthread_t th[N];
77   int i;
78   int err;
79
80   printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
81
82   if (pthread_barrier_init (&bar, NULL, 2) != 0)
83     {
84       puts ("barrier_init failed");
85       exit (1);
86     }
87
88   pthread_attr_t at;
89
90   if (pthread_attr_init (&at) != 0)
91     {
92       puts ("attr_init failed");
93       return 1;
94     }
95
96   if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
97     {
98       puts ("attr_setstacksize failed");
99       return 1;
100     }
101
102   for (i = 0; i < N; ++i)
103     {
104       printf ("create thread %d\n", i);
105
106       err = pthread_create (&th[i], &at, tf, (void *) (long int) i);
107       if (err != 0)
108         error (EXIT_FAILURE, err, "cannot create thread %d", i);
109
110       printf ("wait for child %d\n", i);
111
112       /* Wait for the child to start up and get the mutex for the
113          conditional variable.  */
114       int e = pthread_barrier_wait (&bar);
115       if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
116         {
117           puts ("barrier_wait failed");
118           exit (1);
119         }
120     }
121
122   if (pthread_attr_destroy (&at) != 0)
123     {
124       puts ("attr_destroy failed");
125       return 1;
126     }
127
128   puts ("get lock outselves");
129
130   err = pthread_mutex_lock (&mut);
131   if (err != 0)
132     error (EXIT_FAILURE, err, "mut locking failed");
133
134   puts ("broadcast");
135
136   /* Wake up all threads.  */
137   err = pthread_cond_broadcast (&cond);
138   if (err != 0)
139     error (EXIT_FAILURE, err, "parent: broadcast failed");
140
141   err = pthread_mutex_unlock (&mut);
142   if (err != 0)
143     error (EXIT_FAILURE, err, "mut unlocking failed");
144
145   /* Join all threads.  */
146   for (i = 0; i < N; ++i)
147     {
148       printf ("join thread %d\n", i);
149
150       err = pthread_join (th[i], NULL);
151       if (err != 0)
152         error (EXIT_FAILURE, err, "join of child %d failed", i);
153     }
154
155   puts ("done");
156
157   return 0;
158 }
159
160
161 #define TEST_FUNCTION do_test ()
162 #include "../test-skeleton.c"