Blame nptl/tst-abstime.c

Packit 6c4009
/* Copyright (C) 2010-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Andreas Schwab <schwab@redhat.com>, 2010.
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 <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <semaphore.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
Packit 6c4009
static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
Packit 6c4009
static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
Packit 6c4009
static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
Packit 6c4009
static pthread_rwlock_t rw1 = PTHREAD_RWLOCK_INITIALIZER;
Packit 6c4009
static pthread_rwlock_t rw2 = PTHREAD_RWLOCK_INITIALIZER;
Packit 6c4009
static sem_t sem;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
th (void *arg)
Packit 6c4009
{
Packit 6c4009
  long int res = 0;
Packit 6c4009
  int r;
Packit 6c4009
  struct timespec t = { -2, 0 };
Packit 6c4009
Packit 6c4009
  r = pthread_mutex_timedlock (&m1, &t);
Packit 6c4009
  if (r != ETIMEDOUT)
Packit 6c4009
    {
Packit 6c4009
      puts ("pthread_mutex_timedlock did not return ETIMEDOUT");
Packit 6c4009
      res = 1;
Packit 6c4009
    }
Packit 6c4009
  r = pthread_rwlock_timedrdlock (&rw1, &t);
Packit 6c4009
  if (r != ETIMEDOUT)
Packit 6c4009
    {
Packit 6c4009
      puts ("pthread_rwlock_timedrdlock did not return ETIMEDOUT");
Packit 6c4009
      res = 1;
Packit 6c4009
    }
Packit 6c4009
  r = pthread_rwlock_timedwrlock (&rw2, &t);
Packit 6c4009
  if (r != ETIMEDOUT)
Packit 6c4009
    {
Packit 6c4009
      puts ("pthread_rwlock_timedwrlock did not return ETIMEDOUT");
Packit 6c4009
      res = 1;
Packit 6c4009
    }
Packit 6c4009
  return (void *) res;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int res = 0;
Packit 6c4009
  int r;
Packit 6c4009
  struct timespec t = { -2, 0 };
Packit 6c4009
  pthread_t pth;
Packit 6c4009
Packit 6c4009
  sem_init (&sem, 0, 0);
Packit 6c4009
  r = sem_timedwait (&sem, &t);
Packit 6c4009
  if (r != -1 || errno != ETIMEDOUT)
Packit 6c4009
    {
Packit 6c4009
      puts ("sem_timedwait did not fail with ETIMEDOUT");
Packit 6c4009
      res = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pthread_mutex_lock (&m1;;
Packit 6c4009
  pthread_rwlock_wrlock (&rw1);
Packit 6c4009
  pthread_rwlock_rdlock (&rw2);
Packit 6c4009
  pthread_mutex_lock (&m2;;
Packit 6c4009
  if (pthread_create (&pth, 0, th, 0) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("cannot create thread");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  r = pthread_cond_timedwait (&c, &m2, &t);
Packit 6c4009
  if (r != ETIMEDOUT)
Packit 6c4009
    {
Packit 6c4009
      puts ("pthread_cond_timedwait did not return ETIMEDOUT");
Packit 6c4009
      res = 1;
Packit 6c4009
    }
Packit 6c4009
  void *thres;
Packit 6c4009
  pthread_join (pth, &thres);
Packit 6c4009
  return res | (thres != NULL);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"