* sysdeps/unix/sysv/linux/m68k/semtimedop.S: New file.
[glibc/pb-stable.git] / nptl / tst-cond2.c
blob21bf817b135dd80f95189c8eeb93ff1f80f8f4ee
1 /* Copyright (C) 2002, 2003 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 for (i = 0; i < N; ++i)
91 printf ("create thread %d\n", i);
93 err = pthread_create (&th[i], NULL, tf, (void *) (long int) i);
94 if (err != 0)
95 error (EXIT_FAILURE, err, "cannot create thread %d", i);
97 printf ("wait for child %d\n", i);
99 /* Wait for the child to start up and get the mutex for the
100 conditional variable. */
101 int e = pthread_barrier_wait (&bar);
102 if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
104 puts ("barrier_wait failed");
105 exit (1);
109 puts ("get lock outselves");
111 err = pthread_mutex_lock (&mut);
112 if (err != 0)
113 error (EXIT_FAILURE, err, "mut locking failed");
115 puts ("broadcast");
117 /* Wake up all threads. */
118 err = pthread_cond_broadcast (&cond);
119 if (err != 0)
120 error (EXIT_FAILURE, err, "parent: broadcast failed");
122 err = pthread_mutex_unlock (&mut);
123 if (err != 0)
124 error (EXIT_FAILURE, err, "mut unlocking failed");
126 /* Join all threads. */
127 for (i = 0; i < N; ++i)
129 printf ("join thread %d\n", i);
131 err = pthread_join (th[i], NULL);
132 if (err != 0)
133 error (EXIT_FAILURE, err, "join of child %d failed", i);
136 puts ("done");
138 return 0;
142 #define TEST_FUNCTION do_test ()
143 #include "../test-skeleton.c"