Blame nptl/tst-cond-except.c

Packit 6c4009
/* Verify that exception table for pthread_cond_wait is correct.
Packit 6c4009
   Copyright (C) 2012-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 <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
pthread_mutex_t mutex;
Packit 6c4009
pthread_cond_t cond;
Packit 6c4009
Packit 6c4009
#define CHECK_RETURN_VAL_OR_FAIL(ret,str) \
Packit 6c4009
  ({ if ((ret) != 0) \
Packit 6c4009
       { \
Packit 6c4009
         printf ("%s failed: %s\n", (str), strerror (ret)); \
Packit 6c4009
         ret = 1; \
Packit 6c4009
         goto out; \
Packit 6c4009
       } \
Packit 6c4009
  })
Packit 6c4009
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
clean (void *arg)
Packit 6c4009
{
Packit 6c4009
  puts ("clean: Unlocking mutex...");
Packit 6c4009
  pthread_mutex_unlock ((pthread_mutex_t *) arg);
Packit 6c4009
  puts ("clean: Mutex unlocked...");
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
thr (void *arg)
Packit 6c4009
{
Packit 6c4009
  int ret = 0;
Packit 6c4009
  pthread_mutexattr_t mutexAttr;
Packit 6c4009
  ret = pthread_mutexattr_init (&mutexAttr);
Packit 6c4009
  CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_mutexattr_init");
Packit 6c4009
Packit 6c4009
  ret = pthread_mutexattr_setprotocol (&mutexAttr, PTHREAD_PRIO_INHERIT);
Packit 6c4009
  CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_mutexattr_setprotocol");
Packit 6c4009
Packit 6c4009
  ret = pthread_mutex_init (&mutex, &mutexAttr);
Packit 6c4009
  CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_mutex_init");
Packit 6c4009
Packit 6c4009
  ret = pthread_cond_init (&cond, 0);
Packit 6c4009
  CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_cond_init");
Packit 6c4009
Packit 6c4009
  puts ("th: Init done, entering wait...");
Packit 6c4009
Packit 6c4009
  pthread_cleanup_push (clean, (void *) &mutex);
Packit 6c4009
  ret = pthread_mutex_lock (&mutex);
Packit 6c4009
  CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_mutex_lock");
Packit 6c4009
  while (1)
Packit 6c4009
    {
Packit 6c4009
      ret = pthread_cond_wait (&cond, &mutex);
Packit 6c4009
      CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_cond_wait");
Packit 6c4009
    }
Packit 6c4009
  pthread_cleanup_pop (1);
Packit 6c4009
Packit 6c4009
out:
Packit 6c4009
  return (void *) (uintptr_t) ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  pthread_t thread;
Packit 6c4009
  int ret = 0;
Packit 6c4009
  void *thr_ret = 0;
Packit 6c4009
  ret = pthread_create (&thread, 0, thr, &thr_ret);
Packit 6c4009
  CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_create");
Packit 6c4009
Packit 6c4009
  puts ("main: Thread created, waiting a bit...");
Packit 6c4009
  sleep (2);
Packit 6c4009
Packit 6c4009
  puts ("main: Cancelling thread...");
Packit 6c4009
  ret = pthread_cancel (thread);
Packit 6c4009
  CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_cancel");
Packit 6c4009
Packit 6c4009
  puts ("main: Joining th...");
Packit 6c4009
  ret = pthread_join (thread, NULL);
Packit 6c4009
  CHECK_RETURN_VAL_OR_FAIL (ret, "pthread_join");
Packit 6c4009
Packit 6c4009
  if (thr_ret != NULL)
Packit 6c4009
    return 1;
Packit 6c4009
Packit 6c4009
  puts ("main: Joined thread, done!");
Packit 6c4009
Packit 6c4009
out:
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#define TIMEOUT 5
Packit 6c4009
#include "../test-skeleton.c"