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.
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.
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.
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
26 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
27 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
29 static pthread_barrier_t bar;
38 printf ("child %d: lock\n", i);
40 err = pthread_mutex_lock (&mut);
42 error (EXIT_FAILURE, err, "locking in child failed");
44 printf ("child %d: sync\n", i);
46 int e = pthread_barrier_wait (&bar);
47 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
49 puts ("child: barrier_wait failed");
53 printf ("child %d: wait\n", i);
55 err = pthread_cond_wait (&cond, &mut);
57 error (EXIT_FAILURE, err, "child %d: failed to wait", i);
59 printf ("child %d: woken up\n", i);
61 err = pthread_mutex_unlock (&mut);
63 error (EXIT_FAILURE, err, "child %d: unlock[2] failed", i);
65 printf ("child %d: done\n", i);
81 printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
83 if (pthread_barrier_init (&bar, NULL, 2) != 0)
85 puts ("barrier_init failed");
91 if (pthread_attr_init (&at) != 0)
93 puts ("attr_init failed");
97 if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
99 puts ("attr_setstacksize failed");
103 for (i = 0; i < N; ++i)
105 printf ("create thread %d\n", i);
107 err = pthread_create (&th[i], &at, tf, (void *) (long int) i);
109 error (EXIT_FAILURE, err, "cannot create thread %d", i);
111 printf ("wait for child %d\n", i);
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)
118 puts ("barrier_wait failed");
123 if (pthread_attr_destroy (&at) != 0)
125 puts ("attr_destroy failed");
129 puts ("get lock outselves");
131 err = pthread_mutex_lock (&mut);
133 error (EXIT_FAILURE, err, "mut locking failed");
137 /* Wake up all threads. */
138 err = pthread_cond_broadcast (&cond);
140 error (EXIT_FAILURE, err, "parent: broadcast failed");
142 err = pthread_mutex_unlock (&mut);
144 error (EXIT_FAILURE, err, "mut unlocking failed");
146 /* Join all threads. */
147 for (i = 0; i < N; ++i)
149 printf ("join thread %d\n", i);
151 err = pthread_join (th[i], NULL);
153 error (EXIT_FAILURE, err, "join of child %d failed", i);
162 #define TEST_FUNCTION do_test ()
163 #include "../test-skeleton.c"