Blame nptl/tst-mtx-timedlock.c

Packit 6c4009
/* C11 threads timed mutex tests.
Packit 6c4009
   Copyright (C) 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 <threads.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#include <support/check.h>
Packit 6c4009
Packit 6c4009
/* Shared mutex between child and parent.  */
Packit 6c4009
static mtx_t mutex;
Packit 6c4009
Packit 6c4009
/* Shared counter to check possible race conditions.  */
Packit 6c4009
static char shrd_counter;
Packit 6c4009
Packit 6c4009
/* Maximum amount of time waiting for mutex.  */
Packit 6c4009
static struct timespec wait_time;
Packit 6c4009
Packit 6c4009
/* Function to choose an action to do, depending on mtx_timedlock
Packit 6c4009
   return value.  */
Packit 6c4009
static inline void
Packit 6c4009
choose_action (int action, char* thread_name)
Packit 6c4009
{
Packit 6c4009
  switch (action)
Packit 6c4009
    {
Packit 6c4009
      case thrd_success:
Packit 6c4009
        ++shrd_counter;
Packit 6c4009
Packit 6c4009
	if (mtx_unlock (&mutex) != thrd_success)
Packit 6c4009
	  FAIL_EXIT1 ("mtx_unlock failed");
Packit 6c4009
      break;
Packit 6c4009
Packit 6c4009
      case thrd_timedout:
Packit 6c4009
        break;
Packit 6c4009
Packit 6c4009
      case thrd_error:
Packit 6c4009
	FAIL_EXIT1 ("%s lock error", thread_name);
Packit 6c4009
        break;
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
child_add (void *arg)
Packit 6c4009
{
Packit 6c4009
  char child_name[] = "child";
Packit 6c4009
Packit 6c4009
  /* Try to lock mutex.  */
Packit 6c4009
  choose_action (mtx_timedlock (&mutex, &wait_time), child_name);
Packit 6c4009
  thrd_exit (thrd_success);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  thrd_t id;
Packit 6c4009
  char parent_name[] = "parent";
Packit 6c4009
Packit 6c4009
  if (mtx_init (&mutex, mtx_timed) != thrd_success)
Packit 6c4009
    FAIL_EXIT1 ("mtx_init failed");
Packit 6c4009
Packit 6c4009
  if (clock_gettime (CLOCK_REALTIME, &wait_time) != 0)
Packit 6c4009
    FAIL_EXIT1 ("clock_gettime failed");
Packit 6c4009
  /* Tiny amount of time, to assure that if any thread finds it busy.
Packit 6c4009
     It will receive thrd_timedout.  */
Packit 6c4009
  wait_time.tv_nsec += 1;
Packit 6c4009
  if (wait_time.tv_nsec == 1000 * 1000 * 1000)
Packit 6c4009
    {
Packit 6c4009
      wait_time.tv_sec += 1;
Packit 6c4009
      wait_time.tv_nsec = 0;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  if (thrd_create (&id, child_add, NULL) != thrd_success)
Packit 6c4009
    FAIL_EXIT1 ("thrd_create failed");
Packit 6c4009
Packit 6c4009
  choose_action (mtx_timedlock (&mutex, &wait_time), parent_name);
Packit 6c4009
Packit 6c4009
  if (thrd_join (id, NULL) != thrd_success)
Packit 6c4009
    FAIL_EXIT1 ("thrd_join failed");
Packit 6c4009
Packit 6c4009
  if (shrd_counter != 2 && shrd_counter != 1)
Packit 6c4009
    FAIL_EXIT1 ("shrd_counter != {1,2} (%d)", shrd_counter);
Packit 6c4009
Packit 6c4009
  mtx_destroy (&mutex);
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>