This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

RE: [PATCH] Threadsafe locale initialization


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
+#endif
+      {
+	if (!_S_initialized)
+	  _S_init();
+	_S_initialized = true;
+      }
+    return _S_c_locale;
+  }

to:

+   __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);
  else
    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
(!_S_initialized)
to
(!_S_c_locale)
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.

Petur


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]