Compact cache info data structure for x86/x86-64.
[glibc.git] / nptl / tst-cond2.c
blob36f0f294129297c70202c38e97a7be45704ad71d
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
18 02111-1307 USA. */
20 #include <error.h>
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
26 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
27 static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
29 static pthread_barrier_t bar;
32 static void *
33 tf (void *a)
35 int i = (long int) a;
36 int err;
38 printf ("child %d: lock\n", i);
40 err = pthread_mutex_lock (&mut);
41 if (err != 0)
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");
50 exit (1);
53 printf ("child %d: wait\n", i);
55 err = pthread_cond_wait (&cond, &mut);
56 if (err != 0)
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);
62 if (err != 0)
63 error (EXIT_FAILURE, err, "child %d: unlock[2] failed", i);
65 printf ("child %d: done\n", i);
67 return NULL;
71 #define N 10
74 static int
75 do_test (void)
77 pthread_t th[N];
78 int i;
79 int err;
81 printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
83 if (pthread_barrier_init (&bar, NULL, 2) != 0)
85 puts ("barrier_init failed");
86 exit (1);
89 pthread_attr_t at;
91 if (pthread_attr_init (&at) != 0)
93 puts ("attr_init failed");
94 return 1;
97 if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
99 puts ("attr_setstacksize failed");
100 return 1;
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);
108 if (err != 0)
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");
119 exit (1);
123 if (pthread_attr_destroy (&at) != 0)
125 puts ("attr_destroy failed");
126 return 1;
129 puts ("get lock outselves");
131 err = pthread_mutex_lock (&mut);
132 if (err != 0)
133 error (EXIT_FAILURE, err, "mut locking failed");
135 puts ("broadcast");
137 /* Wake up all threads. */
138 err = pthread_cond_broadcast (&cond);
139 if (err != 0)
140 error (EXIT_FAILURE, err, "parent: broadcast failed");
142 err = pthread_mutex_unlock (&mut);
143 if (err != 0)
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);
152 if (err != 0)
153 error (EXIT_FAILURE, err, "join of child %d failed", i);
156 puts ("done");
158 return 0;
162 #define TEST_FUNCTION do_test ()
163 #include "../test-skeleton.c"