Blame nptl/tst-rwlock16.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 with a reader-preferring rwlock, all readers are woken if
Packit 6c4009
   one reader "steals" lock ownership from a blocked writer.  */
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 <semaphore.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
/* If we strictly prefer writers over readers, a program must not expect
Packit 6c4009
   that, in the presence of concurrent writers, one reader will also acquire
Packit 6c4009
   the lock when another reader has already done so.  Thus, use the
Packit 6c4009
   default rwlock type that does not strictly prefer writers.  */
Packit 6c4009
static pthread_rwlock_t r = PTHREAD_RWLOCK_INITIALIZER;
Packit 6c4009
Packit 6c4009
static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
Packit 6c4009
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
Packit 6c4009
Packit 6c4009
/* Avoid using glibc-internal atomic operations.  */
Packit 6c4009
static sem_t stop;
Packit 6c4009
static int consumer_stop = 0;
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
writer (void *arg)
Packit 6c4009
{
Packit 6c4009
  int s;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      if (pthread_rwlock_wrlock (&r) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("wrlock failed");
Packit 6c4009
	  exit (EXIT_FAILURE);
Packit 6c4009
	}
Packit 6c4009
      if (pthread_rwlock_unlock (&r) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("unlock failed");
Packit 6c4009
	  exit (EXIT_FAILURE);
Packit 6c4009
	}
Packit 6c4009
      sem_getvalue (&stop, &s);
Packit 6c4009
    }
Packit 6c4009
  while (s == 0);
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
reader_producer (void *arg)
Packit 6c4009
{
Packit 6c4009
  int s;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      if (pthread_rwlock_rdlock (&r) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("rdlock reader failed");
Packit 6c4009
	  exit (EXIT_FAILURE);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      sem_getvalue (&stop, &s);
Packit 6c4009
Packit 6c4009
      pthread_mutex_lock (&m);
Packit 6c4009
      if (s != 0)
Packit 6c4009
	consumer_stop = 1;
Packit 6c4009
      pthread_cond_signal (&cv;;
Packit 6c4009
      pthread_mutex_unlock (&m);
Packit 6c4009
Packit 6c4009
      if (pthread_rwlock_unlock (&r) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("unlock reader failed");
Packit 6c4009
	  exit (EXIT_FAILURE);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  while (s == 0);
Packit 6c4009
  puts ("producer finished");
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
reader_consumer (void *arg)
Packit 6c4009
{
Packit 6c4009
  int s;
Packit 6c4009
Packit 6c4009
  do
Packit 6c4009
    {
Packit 6c4009
      if (pthread_rwlock_rdlock (&r) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("rdlock reader failed");
Packit 6c4009
	  exit (EXIT_FAILURE);
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      pthread_mutex_lock (&m);
Packit 6c4009
      s = consumer_stop;
Packit 6c4009
      if (s == 0)
Packit 6c4009
	pthread_cond_wait (&cv, &m);
Packit 6c4009
      pthread_mutex_unlock (&m);
Packit 6c4009
Packit 6c4009
      if (pthread_rwlock_unlock (&r) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("unlock reader failed");
Packit 6c4009
	  exit (EXIT_FAILURE);
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  while (s == 0);
Packit 6c4009
    puts ("consumer finished");
Packit 6c4009
  return NULL;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  pthread_t w1, w2, rp, rc;
Packit 6c4009
Packit 6c4009
  if (pthread_create (&w1, NULL, writer, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_create (&w2, NULL, writer, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_create (&rc, NULL, reader_consumer, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_create (&rp, NULL, reader_producer, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("create failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  sleep (2);
Packit 6c4009
  sem_post (&stop);
Packit 6c4009
Packit 6c4009
  if (pthread_join (w1, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("w1 join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_join (w2, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("w2 join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_join (rp, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("reader_producer join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  if (pthread_join (rc, NULL) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("reader_consumer join failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
Packit 6c4009
#define TIMEOUT 3
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"