1 /* Copyright (C) 2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2005.
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 m
;
27 static pthread_barrier_t b
;
31 # define LOCK(m) pthread_mutex_lock (m)
38 long int round
= (long int) arg
;
40 if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE
, NULL
) != 0)
42 printf ("%ld: setcancelstate failed\n", round
);
49 printf ("%ld: child: mutex_lock failed with error %d\n", round
, e
);
53 e
= pthread_barrier_wait (&b
);
54 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
56 printf ("%ld: child: 1st barrier_wait failed\n", round
);
60 e
= pthread_barrier_wait (&b
);
61 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
63 printf ("%ld: child: 2nd barrier_wait failed\n", round
);
67 pthread_testcancel ();
69 printf ("%ld: testcancel returned\n", round
);
81 pthread_mutexattr_t a
;
82 if (pthread_mutexattr_init (&a
) != 0)
84 puts ("mutexattr_init failed");
87 if (pthread_mutexattr_setrobust_np (&a
, PTHREAD_MUTEX_ROBUST_NP
) != 0)
89 puts ("mutexattr_setrobust failed");
92 #ifndef NOT_CONSISTENT
93 if (pthread_mutex_init (&m
, &a
) != 0)
95 puts ("mutex_init failed");
100 if (pthread_barrier_init (&b
, NULL
, 2) != 0)
102 puts ("barrier_init failed");
106 for (long int round
= 1; round
< 5; ++round
)
108 #ifdef NOT_CONSISTENT
109 if (pthread_mutex_init (&m
, &a
) != 0)
111 puts ("mutex_init failed");
117 if (pthread_create (&th
, NULL
, tf
, (void *) round
) != 0)
119 printf ("%ld: create failed\n", round
);
123 int e
= pthread_barrier_wait (&b
);
124 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
126 printf ("%ld: parent: 1st barrier_wait failed\n", round
);
130 if (pthread_cancel (th
) != 0)
132 printf ("%ld: cancel failed\n", round
);
136 e
= pthread_barrier_wait (&b
);
137 if (e
!= 0 && e
!= PTHREAD_BARRIER_SERIAL_THREAD
)
139 printf ("%ld: parent: 2nd barrier_wait failed\n", round
);
148 if (pthread_join (th
, &res
) != 0)
150 printf ("%ld: join failed\n", round
);
153 if (res
!= PTHREAD_CANCELED
)
155 printf ("%ld: thread not canceled\n", round
);
163 printf ("%ld: parent: mutex_lock succeeded\n", round
);
168 printf ("%ld: parent: mutex_lock returned wrong code\n", round
);
173 if ((round
& 1) == 0)
176 if (pthread_join (th
, &res
) != 0)
178 printf ("%ld: join failed\n", round
);
181 if (res
!= PTHREAD_CANCELED
)
183 printf ("%ld: thread not canceled\n", round
);
189 #ifndef NOT_CONSISTENT
190 e
= pthread_mutex_consistent_np (&m
);
193 printf ("%ld: mutex_consistent failed with error %d\n", round
, e
);
198 e
= pthread_mutex_unlock (&m
);
201 printf ("%ld: mutex_unlocked failed\n", round
);
205 #ifdef NOT_CONSISTENT
209 printf ("%ld: locking inconsistent mutex succeeded\n", round
);
212 if (e
!= ENOTRECOVERABLE
)
214 printf ("%ld: locking inconsistent mutex failed with error %d\n",
219 if (pthread_mutex_destroy (&m
) != 0)
221 puts ("mutex_destroy failed");
227 #ifndef NOT_CONSISTENT
228 if (pthread_mutex_destroy (&m
) != 0)
230 puts ("mutex_destroy failed");
235 if (pthread_mutexattr_destroy (&a
) != 0)
237 puts ("mutexattr_destroy failed");
244 #define TEST_FUNCTION do_test ()
245 #include "../test-skeleton.c"