hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame nptl/tst-once5.cc

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
#include <errno.h>
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static pthread_once_t once = PTHREAD_ONCE_INIT;
Packit 6c4009
Packit 6c4009
// Exception type thrown from the pthread_once init routine.
Packit 6c4009
struct OnceException { };
Packit 6c4009
Packit 6c4009
// Test iteration counter.
Packit 6c4009
static int niter;
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
init_routine (void)
Packit 6c4009
{
Packit 6c4009
  if (niter < 2)
Packit 6c4009
    throw OnceException ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
// Verify that an exception thrown from the pthread_once init routine
Packit 6c4009
// is propagated to the pthread_once caller and that the function can
Packit 6c4009
// be subsequently invoked to attempt the initialization again.
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int result = 1;
Packit 6c4009
Packit 6c4009
  // Repeat three times, having the init routine throw the first two
Packit 6c4009
  // times and succeed on the final attempt.
Packit 6c4009
  for (niter = 0; niter != 3; ++niter) {
Packit 6c4009
Packit 6c4009
    try {
Packit 6c4009
      int rc = pthread_once (&once, init_routine);
Packit 6c4009
      if (rc)
Packit 6c4009
        fprintf (stderr, "pthread_once failed: %i (%s)\n",
Packit 6c4009
                 rc, strerror (rc));
Packit 6c4009
Packit 6c4009
      if (niter < 2)
Packit 6c4009
        fputs ("pthread_once unexpectedly returned without"
Packit 6c4009
               " throwing an exception", stderr);
Packit 6c4009
    }
Packit 6c4009
    catch (OnceException) {
Packit 6c4009
      if (1 < niter)
Packit 6c4009
        fputs ("pthread_once unexpectedly threw", stderr);
Packit 6c4009
      result = 0;
Packit 6c4009
    }
Packit 6c4009
    catch (...) {
Packit 6c4009
      fputs ("pthread_once threw an unknown exception", stderr);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
    // Abort the test on the first failure.
Packit 6c4009
    if (result)
Packit 6c4009
      break;
Packit 6c4009
  }
Packit 6c4009
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
// The test currently hangs and is XFAILed.  Reduce the timeout.
Packit 6c4009
#define TIMEOUT 1
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"