[PATCH] Threadsafe locale initialization

Pétur Runólfsson peturr02@ru.is
Thu Oct 2 13:02:00 GMT 2003

Benjamin Kosnik wrote: 
> I like this patch very much, and consider this a necessary first step
> for the detangling of the object files.

Thanks :-)

> Here's a slightly modified version of this patch.

I see you have changed:

+  __c_locale
+  locale::facet::_S_get_c_locale()
+  {
+#ifdef __GHTREADS
+    if (__gthread_active_p())
+      __gthread_once(&_S_once_mt, _S_init);
+    else
+      {
+	if (!_S_initialized)
+	  _S_init();
+	_S_initialized = true;
+      }
+    return _S_c_locale;
+  }


+   __c_locale
+   locale::facet::_S_get_c_locale()
+   {
+ #ifdef __GHTREADS
+     __gthread_once(&_S_once, _S_initialize_once);
+ #else
+     if (!_S_c_locale)
+       _S_initialize_once();
+ #endif
+     return _S_c_locale;
+   }

I copied the __gthread_active_p() check from __mt_alloc::allocate
(in include/ext/mt_allocator.h). The implementation of
__gthread_once in gthr-posix.h is:

static inline int
__gthread_once (__gthread_once_t *once, void (*func) (void))
  if (__gthread_active_p ())
    return pthread_once (once, func);
    return -1;

so it seems that _S_initialize_once() must be called manually if
__GTHREADS is defined but __gthread_active_p() returns false.
Am I missing something obvious here?

The other change, from
should be safe enough. It seems that _S_c_locale is always null
in the generic locale, so I put in a separate check to make sure
_S_initialize_once() would only be called once. However,
_S_initialize_once() doesn't really do anything in the generic
locale, so calling it more than once is safe.


More information about the Libstdc++ mailing list