From ed225df3ad9cbac3c22ec3f0fbbed1f9c61d1c54 Mon Sep 17 00:00:00 2001 From: Martin Sebor Date: Wed, 1 Jul 2015 14:05:27 -0600 Subject: [PATCH] The C++ 2011 std::call_once function is specified to allow the initialization routine to exit by throwing an exception. Such an execution, termed exceptional, requires call_once to propagate the exception to its caller. A program may contain any number of exceptional executions but only one returning execution (which, if it exists, must be the last execution with the same once flag). On POSIX systems such as Linux, std::call_once is implemented in terms of pthread_once. However, as discussed in libstdc++ bug 66146 - "call_once not C++11-compliant on ppc64le," GLIBC's pthread_once hangs when the initialization function exits by throwing an exception on at least arm and ppc64 (though apparently not on x86_64). This effectively prevents call_once from conforming to the C++ requirements since there doesn't appear to be a thread-safe way to work around this problem in libstdc++. This patch changes pthread_once to handle gracefully init functions that exit by throwing exceptions. It was successfully tested on ppc64, ppc64le, and x86_64. [BZ #18435] * nptl/Makefile: Add tst-once5.cc. * nptl/pthreadP.h (pthread_cleanup_push, pthread_cleanup_pop): Remove macro redefinitions. * nptl/tst-once5.cc: New test. --- ChangeLog | 8 ++++++ NEWS | 10 +++---- nptl/Makefile | 3 ++- nptl/pthreadP.h | 7 ----- nptl/tst-once5.cc | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 nptl/tst-once5.cc diff --git a/ChangeLog b/ChangeLog index 0cd6ca3ea8..a2fd213951 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2015-07-01 Martin Sebor + + [BZ #18435] + * nptl/Makefile: Add tst-once5.cc. + * nptl/pthreadP.h (pthread_cleanup_push, pthread_cleanup_pop): + Remove macro redefinitions. + * nptl/tst-once5.cc: New test. + 2015-07-01 Joseph Myers * sysdeps/powerpc/nofpu/libm-test-ulps: Regenerated. diff --git a/NEWS b/NEWS index 69d9f73932..91320dd429 100644 --- a/NEWS +++ b/NEWS @@ -21,11 +21,11 @@ Version 2.22 18111, 18116, 18125, 18128, 18138, 18185, 18196, 18197, 18206, 18210, 18211, 18217, 18219, 18220, 18221, 18234, 18244, 18245, 18247, 18287, 18319, 18324, 18333, 18346, 18371, 18397, 18409, 18410, 18412, 18418, - 18422, 18434, 18444, 18468, 18469, 18470, 18479, 18483, 18495, 18496, - 18497, 18498, 18502, 18507, 18512, 18513, 18519, 18520, 18522, 18527, - 18528, 18529, 18530, 18532, 18533, 18534, 18536, 18539, 18540, 18542, - 18544, 18545, 18546, 18547, 18549, 18553, 18558, 18569, 18583, 18585, - 18586, 18593, 18594, 18602, 18612, 18613. + 18422, 18434, 18435, 18444, 18468, 18469, 18470, 18479, 18483, 18495, + 18496, 18497, 18498, 18502, 18507, 18512, 18513, 18519, 18520, 18522, + 18527, 18528, 18529, 18530, 18532, 18533, 18534, 18536, 18539, 18540, + 18542, 18544, 18545, 18546, 18547, 18549, 18553, 18558, 18569, 18583, + 18585, 18586, 18593, 18594, 18602, 18612, 18613. * Cache information can be queried via sysconf() function on s390 e.g. with _SC_LEVEL1_ICACHE_SIZE as argument. diff --git a/nptl/Makefile b/nptl/Makefile index 530d14b9d4..4544aa2947 100644 --- a/nptl/Makefile +++ b/nptl/Makefile @@ -211,6 +211,7 @@ CFLAGS-recvfrom.c = -fexceptions -fasynchronous-unwind-tables CFLAGS-pt-system.c = -fexceptions +LDLIBS-tst-once5 = -lstdc++ tests = tst-typesizes \ tst-attr1 tst-attr2 tst-attr3 tst-default-attr \ @@ -233,7 +234,7 @@ tests = tst-typesizes \ tst-rwlock5 tst-rwlock6 tst-rwlock7 tst-rwlock8 tst-rwlock9 \ tst-rwlock10 tst-rwlock11 tst-rwlock12 tst-rwlock13 tst-rwlock14 \ tst-rwlock15 tst-rwlock16 \ - tst-once1 tst-once2 tst-once3 tst-once4 \ + tst-once1 tst-once2 tst-once3 tst-once4 tst-once5 \ tst-key1 tst-key2 tst-key3 tst-key4 \ tst-sem1 tst-sem2 tst-sem3 tst-sem4 tst-sem5 tst-sem6 tst-sem7 \ tst-sem8 tst-sem9 tst-sem10 tst-sem11 tst-sem12 tst-sem13 tst-sem14 \ diff --git a/nptl/pthreadP.h b/nptl/pthreadP.h index 84a7105848..72d3e23b95 100644 --- a/nptl/pthreadP.h +++ b/nptl/pthreadP.h @@ -536,16 +536,9 @@ extern void __librt_disable_asynccancel (int oldtype) extern void __pthread_cleanup_push (struct _pthread_cleanup_buffer *buffer, void (*routine) (void *), void *arg) attribute_hidden; -# undef pthread_cleanup_push -# define pthread_cleanup_push(routine,arg) \ - { struct _pthread_cleanup_buffer _buffer; \ - __pthread_cleanup_push (&_buffer, (routine), (arg)); extern void __pthread_cleanup_pop (struct _pthread_cleanup_buffer *buffer, int execute) attribute_hidden; -# undef pthread_cleanup_pop -# define pthread_cleanup_pop(execute) \ - __pthread_cleanup_pop (&_buffer, (execute)); } #endif extern void __pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *buffer, diff --git a/nptl/tst-once5.cc b/nptl/tst-once5.cc new file mode 100644 index 0000000000..60bc78a64a --- /dev/null +++ b/nptl/tst-once5.cc @@ -0,0 +1,80 @@ +/* Copyright (C) 2015 Free Software Foundation, Inc. + This file is part of the GNU C Library. + Contributed by Ulrich Drepper , 2002. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + + +static pthread_once_t once = PTHREAD_ONCE_INIT; + +// Exception type thrown from the pthread_once init routine. +struct OnceException { }; + +// Test iteration counter. +static int niter; + +static void +init_routine (void) +{ + if (niter < 2) + throw OnceException (); +} + +// Verify that an exception thrown from the pthread_once init routine +// is propagated to the pthread_once caller and that the function can +// be subsequently invoked to attempt the initialization again. +static int +do_test (void) +{ + int result = 1; + + // Repeat three times, having the init routine throw the first two + // times and succeed on the final attempt. + for (niter = 0; niter != 3; ++niter) { + + try { + int rc = pthread_once (&once, init_routine); + if (rc) + fprintf (stderr, "pthread_once failed: %i (%s)\n", + rc, strerror (rc)); + + if (niter < 2) + fputs ("pthread_once unexpectedly returned without" + " throwing an exception", stderr); + } + catch (OnceException) { + if (1 < niter) + fputs ("pthread_once unexpectedly threw", stderr); + result = 0; + } + catch (...) { + fputs ("pthread_once threw an unknown exception", stderr); + } + + // Abort the test on the first failure. + if (result) + break; + } + + return result; +} + +#define TEST_FUNCTION do_test () +#include "../test-skeleton.c" -- 2.11.4.GIT