setlocale is unfortunately not safe to use in a multi-threaded application.
How
We can avoid using it altogether by pulling the locale from the environment. Both glibc and FreeBSD's libc simply pull this out of the environment using the first set variable of:
LC_ALL
LC_{{category}}
LANG
Since we are just using LC_CTYPE when libc has many different locales, we can get the same locale information by just checking three environment variables. glibc does a lot of other operations around including locale alias substitution but those don't end up affecting the name it hands back out of the setlocale call.
Drawbacks
libc/glibc do a little extra validation on the locale string and won't return a locale string if it's invalid or if matching locale data can't be loaded. With these changes, we are a slight bit less defensive for invalid locales.
we need to remove no_std on linux/bsd to query the environment directly (instead of letting libc do it for us)
Why
setlocale
is unfortunately not safe to use in a multi-threaded application.How
We can avoid using it altogether by pulling the locale from the environment. Both glibc and FreeBSD's libc simply pull this out of the environment using the first set variable of:
LC_ALL
LC_{{category}}
LANG
Since we are just using
LC_CTYPE
when libc has many different locales, we can get the same locale information by just checking three environment variables. glibc does a lot of other operations around including locale alias substitution but those don't end up affecting the name it hands back out of thesetlocale
call.Drawbacks