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, write to the Free
|
---|
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
---|
18 | 02111-1307 USA. */
|
---|
19 |
|
---|
20 | #include <error.h>
|
---|
21 | #include <pthread.h>
|
---|
22 | #include <stdio.h>
|
---|
23 | #include <stdlib.h>
|
---|
24 |
|
---|
25 |
|
---|
26 | static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
|
---|
27 | static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
|
---|
28 |
|
---|
29 | static pthread_barrier_t bar;
|
---|
30 |
|
---|
31 |
|
---|
32 | static void *
|
---|
33 | tf (void *a)
|
---|
34 | {
|
---|
35 | int i = (long int) a;
|
---|
36 | int err;
|
---|
37 |
|
---|
38 | printf ("child %d: lock\n", i);
|
---|
39 |
|
---|
40 | err = pthread_mutex_lock (&mut);
|
---|
41 | if (err != 0)
|
---|
42 | error (EXIT_FAILURE, err, "locking in child failed");
|
---|
43 |
|
---|
44 | printf ("child %d: sync\n", i);
|
---|
45 |
|
---|
46 | int e = pthread_barrier_wait (&bar);
|
---|
47 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
|
---|
48 | {
|
---|
49 | puts ("child: barrier_wait failed");
|
---|
50 | exit (1);
|
---|
51 | }
|
---|
52 |
|
---|
53 | printf ("child %d: wait\n", i);
|
---|
54 |
|
---|
55 | err = pthread_cond_wait (&cond, &mut);
|
---|
56 | if (err != 0)
|
---|
57 | error (EXIT_FAILURE, err, "child %d: failed to wait", i);
|
---|
58 |
|
---|
59 | printf ("child %d: woken up\n", i);
|
---|
60 |
|
---|
61 | err = pthread_mutex_unlock (&mut);
|
---|
62 | if (err != 0)
|
---|
63 | error (EXIT_FAILURE, err, "child %d: unlock[2] failed", i);
|
---|
64 |
|
---|
65 | printf ("child %d: done\n", i);
|
---|
66 |
|
---|
67 | return NULL;
|
---|
68 | }
|
---|
69 |
|
---|
70 |
|
---|
71 | #define N 10
|
---|
72 |
|
---|
73 |
|
---|
74 | static int
|
---|
75 | do_test (void)
|
---|
76 | {
|
---|
77 | pthread_t th[N];
|
---|
78 | int i;
|
---|
79 | int err;
|
---|
80 |
|
---|
81 | printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
|
---|
82 |
|
---|
83 | if (pthread_barrier_init (&bar, NULL, 2) != 0)
|
---|
84 | {
|
---|
85 | puts ("barrier_init failed");
|
---|
86 | exit (1);
|
---|
87 | }
|
---|
88 |
|
---|
89 | pthread_attr_t at;
|
---|
90 |
|
---|
91 | if (pthread_attr_init (&at) != 0)
|
---|
92 | {
|
---|
93 | puts ("attr_init failed");
|
---|
94 | return 1;
|
---|
95 | }
|
---|
96 |
|
---|
97 | if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
|
---|
98 | {
|
---|
99 | puts ("attr_setstacksize failed");
|
---|
100 | return 1;
|
---|
101 | }
|
---|
102 |
|
---|
103 | for (i = 0; i < N; ++i)
|
---|
104 | {
|
---|
105 | printf ("create thread %d\n", i);
|
---|
106 |
|
---|
107 | err = pthread_create (&th[i], &at, tf, (void *) (long int) i);
|
---|
108 | if (err != 0)
|
---|
109 | error (EXIT_FAILURE, err, "cannot create thread %d", i);
|
---|
110 |
|
---|
111 | printf ("wait for child %d\n", i);
|
---|
112 |
|
---|
113 | /* Wait for the child to start up and get the mutex for the
|
---|
114 | conditional variable. */
|
---|
115 | int e = pthread_barrier_wait (&bar);
|
---|
116 | if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
|
---|
117 | {
|
---|
118 | puts ("barrier_wait failed");
|
---|
119 | exit (1);
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (pthread_attr_destroy (&at) != 0)
|
---|
124 | {
|
---|
125 | puts ("attr_destroy failed");
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 |
|
---|
129 | puts ("get lock outselves");
|
---|
130 |
|
---|
131 | err = pthread_mutex_lock (&mut);
|
---|
132 | if (err != 0)
|
---|
133 | error (EXIT_FAILURE, err, "mut locking failed");
|
---|
134 |
|
---|
135 | puts ("broadcast");
|
---|
136 |
|
---|
137 | /* Wake up all threads. */
|
---|
138 | err = pthread_cond_broadcast (&cond);
|
---|
139 | if (err != 0)
|
---|
140 | error (EXIT_FAILURE, err, "parent: broadcast failed");
|
---|
141 |
|
---|
142 | err = pthread_mutex_unlock (&mut);
|
---|
143 | if (err != 0)
|
---|
144 | error (EXIT_FAILURE, err, "mut unlocking failed");
|
---|
145 |
|
---|
146 | /* Join all threads. */
|
---|
147 | for (i = 0; i < N; ++i)
|
---|
148 | {
|
---|
149 | printf ("join thread %d\n", i);
|
---|
150 |
|
---|
151 | err = pthread_join (th[i], NULL);
|
---|
152 | if (err != 0)
|
---|
153 | error (EXIT_FAILURE, err, "join of child %d failed", i);
|
---|
154 | }
|
---|
155 |
|
---|
156 | puts ("done");
|
---|
157 |
|
---|
158 | return 0;
|
---|
159 | }
|
---|
160 |
|
---|
161 |
|
---|
162 | #define TEST_FUNCTION do_test ()
|
---|
163 | #include "../test-skeleton.c"
|
---|