Blame nptl/tst-spin2.c

Packit 6c4009
/* Copyright (C) 2002-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
   Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
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 <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/wait.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  size_t ps = sysconf (_SC_PAGESIZE);
Packit 6c4009
  char tmpfname[] = "/tmp/tst-spin2.XXXXXX";
Packit 6c4009
  char data[ps];
Packit 6c4009
  void *mem;
Packit 6c4009
  int fd;
Packit 6c4009
  pthread_spinlock_t *s;
Packit 6c4009
  pid_t pid;
Packit 6c4009
  char *p;
Packit 6c4009
  int err;
Packit 6c4009
Packit 6c4009
  fd = mkstemp (tmpfname);
Packit 6c4009
  if (fd == -1)
Packit 6c4009
    {
Packit 6c4009
      printf ("cannot open temporary file: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Make sure it is always removed.  */
Packit 6c4009
  unlink (tmpfname);
Packit 6c4009
Packit 6c4009
  /* Create one page of data.  */
Packit 6c4009
  memset (data, '\0', ps);
Packit 6c4009
Packit 6c4009
  /* Write the data to the file.  */
Packit 6c4009
  if (write (fd, data, ps) != (ssize_t) ps)
Packit 6c4009
    {
Packit 6c4009
      puts ("short write");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
Packit 6c4009
  if (mem == MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      printf ("mmap failed: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  s = (pthread_spinlock_t *) (((uintptr_t) mem
Packit 6c4009
			       + __alignof (pthread_spinlock_t))
Packit 6c4009
			      & ~(__alignof (pthread_spinlock_t) - 1));
Packit 6c4009
  p = (char *) (s + 1);
Packit 6c4009
Packit 6c4009
  if (pthread_spin_init (s, PTHREAD_PROCESS_SHARED) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("spin_init failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (pthread_spin_lock (s) != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("spin_lock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  err = pthread_spin_trylock (s);
Packit 6c4009
  if (err == 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st spin_trylock succeeded");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  else if (err != EBUSY)
Packit 6c4009
    {
Packit 6c4009
      puts ("1st spin_trylock didn't return EBUSY");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  err = pthread_spin_unlock (s);
Packit 6c4009
  if (err != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("parent: spin_unlock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  err = pthread_spin_trylock (s);
Packit 6c4009
  if (err != 0)
Packit 6c4009
    {
Packit 6c4009
      puts ("2nd spin_trylock failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  *p = 0;
Packit 6c4009
Packit 6c4009
  puts ("going to fork now");
Packit 6c4009
  pid = fork ();
Packit 6c4009
  if (pid == -1)
Packit 6c4009
    {
Packit 6c4009
      puts ("fork failed");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  else if (pid == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Play some lock ping-pong.  It's our turn to unlock first.  */
Packit 6c4009
      if ((*p)++ != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("child: *p != 0");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      if (pthread_spin_unlock (s) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("child: 1st spin_unlock failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      puts ("child done");
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (pthread_spin_lock (s) != 0)
Packit 6c4009
	{
Packit 6c4009
	  puts ("parent: 2nd spin_lock failed");
Packit 6c4009
	  return 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      puts ("waiting for child");
Packit 6c4009
Packit 6c4009
      waitpid (pid, NULL, 0);
Packit 6c4009
Packit 6c4009
      puts ("parent done");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"