bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: new module 'duplocale'


From: Bruno Haible
Subject: Re: new module 'duplocale'
Date: Tue, 24 Nov 2009 11:37:06 +0100
User-agent: KMail/1.9.9

Hi Ludovic,

> >       const char *base_name = nl_langinfo (_NL_LOCALE_NAME (LC_CTYPE));
> 
> This is not thread-safe but I guess there’s no other choice.

This code is only used on glibc systems, and nl_langinfo is multithread-safe
on glibc systems (by code inspection and due to the way it mmaps the locales).
This means, as long as no thread is calling setlocale, all threads can use
all locale functions in parallel without interference. If you call setlocale
while other threads are using locales, you are out for trouble anyway.

> Presumably an ‘nl_langinfo_l’ module could build on the ‘duplocale’
> module like this:
> 
> --8<---------------cut here---------------start------------->8---
> char *
> rpl_nl_langinfo_l (nl_item item, locale_t locale)
> {
>   char *result;
>   locale_t locale_copy;
> 
>   locale_copy = duplocale (locale);
>   if (locale_copy != (locale_t) 0)
>     {
>       result = nl_langinfo_l (item, locale_copy);
>       freelocale (locale);
>     }
>   else
>     result = NULL;
> 
>   return result;
> }
> --8<---------------cut here---------------end--------------->8---

Such code is better written to use uselocale(), which does normally not
cause memory allocation.

You can define such a function for yourself; I agree with Ulrich, Jakub, and
Eric that this functionality should not be in POSIX and hence also not
necessarily in gnulib.

Bruno




reply via email to

[Prev in Thread] Current Thread [Next in Thread]