On Mon, 6 Nov 2023 at 12:16, Jakub Jelinek <ja...@redhat.com> wrote: > > On Mon, Nov 06, 2023 at 11:52:08AM +0000, Richard Biener wrote: > > The following makes the C++98 locale init path follow the way the > > C++11 performs initialization. This way we deal with pthread_once > > failing, falling back to non-threadsafe initialization which, given we > > initialize from the library, should be serialized by the dynamic > > loader already. > > > > Bootstrapped and tested on x86_64-unknown-linux-gnu, OK for trunk? > > And GCC 13 branch? > > > > Thanks, > > Richard. > > > > PR libstdc++/112351 > > libstdc++-v3/ > > * src/c++98/locale.cc (locale::facet::_S_get_c_locale): > > Always perform non-threadsafe init when threadsafe init > > failed. > > --- > > libstdc++-v3/src/c++98/locale.cc | 7 ++----- > > 1 file changed, 2 insertions(+), 5 deletions(-) > > > > diff --git a/libstdc++-v3/src/c++98/locale.cc > > b/libstdc++-v3/src/c++98/locale.cc > > index d308140bab7..e9bec1db3b6 100644 > > --- a/libstdc++-v3/src/c++98/locale.cc > > +++ b/libstdc++-v3/src/c++98/locale.cc > > @@ -216,12 +216,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > > #ifdef __GTHREADS > > if (__gthread_active_p()) > > __gthread_once(&_S_once, _S_initialize_once); > > - else > > #endif > > - { > > - if (!_S_c_locale) > > - _S_initialize_once(); > > - } > > + if (__builtin_expect (!_S_c_locale, 0)) > > + _S_initialize_once(); > > return _S_c_locale; > > Wouldn't it be better to just test __gthread_once return value > #ifdef __THREADS > if ((!__gthread_active_p() > || __gthread_once(&_S_once, _S_initialize_once))) > #endif > if (!_S_c_locale) > _S_initialize_once(); > ?
This still has the problem of calling the function twice, once because __gthread_once fails and one because it succeeds.