|
Packit Service |
82fcde |
/* Copyright (C) 2005-2018 Free Software Foundation, Inc.
|
|
Packit Service |
82fcde |
This file is part of the GNU C Library.
|
|
Packit Service |
82fcde |
Contributed by Ulrich Drepper <drepper@redhat.com>, 2005.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Service |
82fcde |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License as published by the Free Software Foundation; either
|
|
Packit Service |
82fcde |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Service |
82fcde |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
82fcde |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
82fcde |
Lesser General Public License for more details.
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
82fcde |
License along with the GNU C Library; if not, see
|
|
Packit Service |
82fcde |
<http://www.gnu.org/licenses/>. */
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <errno.h>
|
|
Packit Service |
82fcde |
#include <pthread.h>
|
|
Packit Service |
82fcde |
#include <stdbool.h>
|
|
Packit Service |
82fcde |
#include <stdio.h>
|
|
Packit Service |
82fcde |
#include <stdlib.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static pthread_barrier_t b;
|
|
Packit Service |
82fcde |
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
|
|
Packit Service |
82fcde |
static pthread_mutex_t m;
|
|
Packit Service |
82fcde |
static bool first = true;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static void *
|
|
Packit Service |
82fcde |
tf (void *arg)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
long int n = (long int) arg;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutex_lock (&m) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("thread %ld: mutex_lock failed\n", n + 1);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
int e = pthread_barrier_wait (&b);
|
|
Packit Service |
82fcde |
if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("thread %ld: barrier_wait failed\n", n + 1);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
e = pthread_cond_wait (&c, &m);
|
|
Packit Service |
82fcde |
if (first)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (e != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("thread %ld: cond_wait failed\n", n + 1);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
first = false;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (e != EOWNERDEAD)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("thread %ld: cond_wait did not return EOWNERDEAD\n", n + 1);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_cancel (pthread_self ()) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("thread %ld: cancel failed\n", n + 1);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
pthread_testcancel ();
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
printf ("thread %ld: testcancel returned\n", n + 1);
|
|
Packit Service |
82fcde |
exit (1);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
static int
|
|
Packit Service |
82fcde |
do_test (void)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
pthread_mutexattr_t a;
|
|
Packit Service |
82fcde |
if (pthread_mutexattr_init (&a) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutexattr_init failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutexattr_setrobust_np (&a, PTHREAD_MUTEX_ROBUST_NP) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutexattr_setrobust failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#ifdef ENABLE_PI
|
|
Packit Service |
82fcde |
if (pthread_mutexattr_setprotocol (&a, PTHREAD_PRIO_INHERIT) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("pthread_mutexattr_setprotocol failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
int e;
|
|
Packit Service |
82fcde |
e = pthread_mutex_init (&m, &a);
|
|
Packit Service |
82fcde |
if (e != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
#ifdef ENABLE_PI
|
|
Packit Service |
82fcde |
if (e == ENOTSUP)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("PI robust mutexes not supported");
|
|
Packit Service |
82fcde |
return 0;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
#endif
|
|
Packit Service |
82fcde |
puts ("mutex_init failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutexattr_destroy (&a) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutexattr_destroy failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_barrier_init (&b, NULL, 2) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("barrier_init failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define N 5
|
|
Packit Service |
82fcde |
pthread_t th[N];
|
|
Packit Service |
82fcde |
for (long int n = 0; n < N; ++n)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (pthread_create (&th[n], NULL, tf, (void *) n) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("pthread_create loop %ld failed\n", n + 1);
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
e = pthread_barrier_wait (&b);
|
|
Packit Service |
82fcde |
if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("parent: barrier_wait failed in round %ld\n", n + 1);
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutex_lock (&m) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("parent: mutex_lock failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutex_unlock (&m) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("parent: mutex_unlock failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_cond_broadcast (&c) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("cond_broadcast failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
for (int n = 0; n < N; ++n)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
void *res;
|
|
Packit Service |
82fcde |
if (pthread_join (th[n], &res) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("join round %d failed\n", n + 1);
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (res != PTHREAD_CANCELED)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
printf ("thread %d not canceled\n", n + 1);
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
e = pthread_mutex_lock (&m);
|
|
Packit Service |
82fcde |
if (e == 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("parent: 2nd mutex_lock succeeded");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (e != EOWNERDEAD)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("parent: mutex_lock did not return EOWNERDEAD");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutex_unlock (&m) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("parent: 2nd mutex_unlock failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
if (pthread_mutex_destroy (&m) != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
puts ("mutex_destroy failed");
|
|
Packit Service |
82fcde |
return 1;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
return 0;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#define TEST_FUNCTION do_test ()
|
|
Packit Service |
82fcde |
#include "../test-skeleton.c"
|