1 /* Copyright (C) 2002-2018 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, see
17 <http://www.gnu.org/licenses/>. */
25 static pthread_mutex_t mut
= PTHREAD_MUTEX_INITIALIZER
;
26 static pthread_cond_t cond
= PTHREAD_COND_INITIALIZER
;
28 static pthread_barrier_t bar
;
37 printf ("child %d: lock\n", i
);
39 err
= pthread_mutex_lock (&mut
);
41 error (EXIT_FAILURE
, err
, "locking in child failed");
43 printf ("child %d: sync\n", i
);
45 int e
= pthread_barrier_wait (&bar
);
46 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
48 puts ("child: barrier_wait failed");
52 printf ("child %d: wait\n", i
);
54 err
= pthread_cond_wait (&cond
, &mut
);
56 error (EXIT_FAILURE
, err
, "child %d: failed to wait", i
);
58 printf ("child %d: woken up\n", i
);
60 err
= pthread_mutex_unlock (&mut
);
62 error (EXIT_FAILURE
, err
, "child %d: unlock[2] failed", i
);
64 printf ("child %d: done\n", i
);
80 printf ("&cond = %p\n&mut = %p\n", &cond
, &mut
);
82 if (pthread_barrier_init (&bar
, NULL
, 2) != 0)
84 puts ("barrier_init failed");
90 if (pthread_attr_init (&at
) != 0)
92 puts ("attr_init failed");
96 if (pthread_attr_setstacksize (&at
, 1 * 1024 * 1024) != 0)
98 puts ("attr_setstacksize failed");
102 for (i
= 0; i
< N
; ++i
)
104 printf ("create thread %d\n", i
);
106 err
= pthread_create (&th
[i
], &at
, tf
, (void *) (long int) i
);
108 error (EXIT_FAILURE
, err
, "cannot create thread %d", i
);
110 printf ("wait for child %d\n", i
);
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
)
117 puts ("barrier_wait failed");
122 if (pthread_attr_destroy (&at
) != 0)
124 puts ("attr_destroy failed");
128 puts ("get lock outselves");
130 err
= pthread_mutex_lock (&mut
);
132 error (EXIT_FAILURE
, err
, "mut locking failed");
136 /* Wake up all threads. */
137 err
= pthread_cond_broadcast (&cond
);
139 error (EXIT_FAILURE
, err
, "parent: broadcast failed");
141 err
= pthread_mutex_unlock (&mut
);
143 error (EXIT_FAILURE
, err
, "mut unlocking failed");
145 /* Join all threads. */
146 for (i
= 0; i
< N
; ++i
)
148 printf ("join thread %d\n", i
);
150 err
= pthread_join (th
[i
], NULL
);
152 error (EXIT_FAILURE
, err
, "join of child %d failed", i
);
161 #define TEST_FUNCTION do_test ()
162 #include "../test-skeleton.c"