Blame nptl/tst-rwlock15.c

Packit 6c4009
/* Copyright (C) 2015-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
/* This tests that a writer that is preferred -- but times out due to a
Packit 6c4009
   reader being present -- does not miss to wake other readers blocked on the
Packit 6c4009
   writer's pending lock acquisition.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <time.h>
Packit 6c4009
Packit 6c4009
/* The bug existed in the code that strictly prefers writers over readers.  */
Packit 6c4009
static pthread_rwlock_t r = PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
writer (void *arg)
Packit 6c4009
{
Packit 6c4009
  struct timespec ts;
Packit 6c4009
  if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("clock_gettime failed");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  ts.tv_sec += 1;
Packit 6c4009
  int e = pthread_rwlock_timedwrlock (&r, &ts);
Packit 6c4009
  if (e != ETIMEDOUT)
Packit 6c4009
    {
Packit 6c4009
      puts ("timedwrlock did not time out");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
reader (void *arg)
Packit 6c4009
{
Packit 6c4009
  /* This isn't a reliable way to get the interleaving we need (because a
Packit 6c4009
     failed trylock doesn't synchronize with the writer, and because we could
Packit 6c4009
     try to lock after the writer has already timed out).  However, both will
Packit 6c4009
     just lead to false positives.  */
Packit 6c4009
  int e;
Packit 6c4009
  while ((e = pthread_rwlock_tryrdlock (&r)) != EBUSY)
Packit 6c4009
    {
Packit 6c4009
      if (e != 0)
Packit 6c4009
	exit (EXIT_FAILURE);
Packit 6c4009
      pthread_rwlock_unlock (&r);
Packit 6c4009
    }
Packit 6c4009
  e = pthread_rwlock_rdlock (&r);
Packit 6c4009
  if (e != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("reader rdlock failed");
Packit 6c4009
      exit (EXIT_FAILURE);
Packit 6c4009
    }
Packit 6c4009
  pthread_rwlock_unlock (&r);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  /* Grab a rdlock, then create a writer and a reader, and wait until they
Packit 6c4009
     finished.  */
Packit 6c4009
Packit 6c4009
  if (pthread_rwlock_rdlock (&r) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("initial rdlock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  pthread_t thw;
Packit 6c4009
  if (pthread_create (&thw, NULL, writer, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  pthread_t thr;
Packit 6c4009
  if (pthread_create (&thr, NULL, reader, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_join (thw, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("writer join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_join (thr, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("reader join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"