|
Packit |
6c4009 |
/* Verify that condition variables synchronized by PI mutexes don't hang.
|
|
Packit |
6c4009 |
Copyright (C) 2012-2018 Free Software Foundation, Inc.
|
|
Packit |
6c4009 |
This file is part of the GNU C Library.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit |
6c4009 |
modify it under the terms of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License as published by the Free Software Foundation; either
|
|
Packit |
6c4009 |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit |
6c4009 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
6c4009 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
6c4009 |
Lesser General Public License for more details.
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
You should have received a copy of the GNU Lesser General Public
|
|
Packit |
6c4009 |
License along with the GNU C Library; if not, see
|
|
Packit |
6c4009 |
<http://www.gnu.org/licenses/>. */
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#include <pthread.h>
|
|
Packit |
6c4009 |
#include <stdio.h>
|
|
Packit |
6c4009 |
#include <stdlib.h>
|
|
Packit |
6c4009 |
#include <string.h>
|
|
Packit |
6c4009 |
#include <errno.h>
|
|
Packit |
6c4009 |
#include <sys/types.h>
|
|
Packit |
6c4009 |
#include <sys/syscall.h>
|
|
Packit |
6c4009 |
#include <unistd.h>
|
|
Packit |
6c4009 |
#include <sys/time.h>
|
|
Packit |
6c4009 |
#include <time.h>
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define THREADS_NUM 5
|
|
Packit |
6c4009 |
#define MAXITER 50000
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static pthread_mutex_t mutex;
|
|
Packit |
6c4009 |
static pthread_mutexattr_t mutex_attr;
|
|
Packit |
6c4009 |
static pthread_cond_t cond;
|
|
Packit |
6c4009 |
static pthread_t threads[THREADS_NUM];
|
|
Packit |
6c4009 |
static int pending = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
typedef void * (*threadfunc) (void *);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
void *
|
|
Packit |
6c4009 |
thread_fun_timed (void *arg)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int *ret = arg;
|
|
Packit |
6c4009 |
int rv, i;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("Started thread_fun_timed[%d]\n", *ret);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (i = 0; i < MAXITER / THREADS_NUM; i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
rv = pthread_mutex_lock (&mutex);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutex_lock: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
*ret = 1;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
while (!pending)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
struct timespec ts;
|
|
Packit |
6c4009 |
clock_gettime(CLOCK_REALTIME, &ts);
|
|
Packit |
6c4009 |
ts.tv_sec += 20;
|
|
Packit |
6c4009 |
rv = pthread_cond_timedwait (&cond, &mutex, &ts);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
/* There should be no timeout either. */
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_cond_wait: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
*ret = 1;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
pending--;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rv = pthread_mutex_unlock (&mutex);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutex_unlock: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
*ret = 1;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
*ret = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
out:
|
|
Packit |
6c4009 |
return ret;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
void *
|
|
Packit |
6c4009 |
thread_fun (void *arg)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int *ret = arg;
|
|
Packit |
6c4009 |
int rv, i;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
printf ("Started thread_fun[%d]\n", *ret);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (i = 0; i < MAXITER / THREADS_NUM; i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
rv = pthread_mutex_lock (&mutex);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutex_lock: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
*ret = 1;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
while (!pending)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
rv = pthread_cond_wait (&cond, &mutex);
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_cond_wait: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
*ret = 1;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
pending--;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rv = pthread_mutex_unlock (&mutex);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutex_unlock: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
*ret = 1;
|
|
Packit |
6c4009 |
goto out;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
*ret = 0;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
out:
|
|
Packit |
6c4009 |
return ret;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test_wait (threadfunc f)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
int i;
|
|
Packit |
6c4009 |
int rv;
|
|
Packit |
6c4009 |
int counter = 0;
|
|
Packit |
6c4009 |
int retval[THREADS_NUM];
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
puts ("Starting test");
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rv = pthread_mutexattr_init (&mutex_attr);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutexattr_init: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rv = pthread_mutexattr_setprotocol (&mutex_attr, PTHREAD_PRIO_INHERIT);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutexattr_setprotocol: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rv = pthread_mutex_init (&mutex, &mutex_attr);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutex_init: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rv = pthread_cond_init (&cond, NULL);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_cond_init: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (i = 0; i < THREADS_NUM; i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
retval[i] = i;
|
|
Packit |
6c4009 |
rv = pthread_create (&threads[i], NULL, f, &retval[i]);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_create: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (; counter < MAXITER; counter++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
rv = pthread_mutex_lock (&mutex);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutex_lock: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
if (!(counter % 100))
|
|
Packit |
6c4009 |
printf ("counter: %d\n", counter);
|
|
Packit |
6c4009 |
pending += 1;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rv = pthread_cond_signal (&cond;;
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_cond_signal: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
rv = pthread_mutex_unlock (&mutex);
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_mutex_unlock: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
for (i = 0; i < THREADS_NUM; i++)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
void *ret;
|
|
Packit |
6c4009 |
rv = pthread_join (threads[i], &ret;;
|
|
Packit |
6c4009 |
if (rv)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("pthread_join: %s(%d)\n", strerror (rv), rv);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
if (ret && *(int *)ret)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
printf ("Thread %d returned with an error\n", i);
|
|
Packit |
6c4009 |
return 1;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
return 0;
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
static int
|
|
Packit |
6c4009 |
do_test (void)
|
|
Packit |
6c4009 |
{
|
|
Packit |
6c4009 |
puts ("Testing pthread_cond_wait");
|
|
Packit |
6c4009 |
int ret = do_test_wait (thread_fun);
|
|
Packit |
6c4009 |
if (ret)
|
|
Packit |
6c4009 |
return ret;
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
puts ("Testing pthread_cond_timedwait");
|
|
Packit |
6c4009 |
return do_test_wait (thread_fun_timed);
|
|
Packit |
6c4009 |
}
|
|
Packit |
6c4009 |
|
|
Packit |
6c4009 |
#define TIMEOUT 20
|
|
Packit |
6c4009 |
#define TEST_FUNCTION do_test ()
|
|
Packit |
6c4009 |
#include "../test-skeleton.c"
|