@@ -2195,7 +2195,7 @@ To summarize, here's what to expect and how to handle locales in XS code:
2195
2195
=item Non-locale-aware XS code
2196
2196
2197
2197
Keep in mind that even if you think your code is not locale-aware, it
2198
- may call a C library function that is. Hopefully the man page for such
2198
+ may call a library function that is. Hopefully the man page for such
2199
2199
a function will indicate that dependency, but the documentation is
2200
2200
imperfect.
2201
2201
@@ -2231,9 +2231,107 @@ L<perlapi/STORE_LC_NUMERIC_FORCE_TO_UNDERLYING>, and
2231
2231
L<perlapi/RESTORE_LC_NUMERIC> should be used to affect any needed
2232
2232
change.
2233
2233
2234
- However, some alien libraries that may be called do set it, such as
2235
- C<Gtk>. This can cause problems for the perl core and other modules.
2236
- Starting in v5.20.1, calling the function
2234
+ But, starting with Perl v5.28, locales are thread-safe on platforms that
2235
+ support this functionality. Windows has this starting with Visual
2236
+ Studio 2005. Many other modern platforms support the thread-safe POSIX
2237
+ 2008 functions. The C C<#define> C<USE_THREAD_SAFE_LOCALE> will be
2238
+ defined iff this build is using these. From Perl-space, the read-only
2239
+ variable C<${SAFE_LOCALES}> is 1 if either the build is not threaded, or
2240
+ if C<USE_THREAD_SAFE_LOCALE> is defined; otherwise it is 0.
2241
+
2242
+ The way this works under-the-hood is that every thread has a choice of
2243
+ using a locale specific to it (this is the Windows and POSIX 2008
2244
+ functionality), or the global locale that is accessible to all threads
2245
+ (this is the functionality that has always been there). The
2246
+ implementations for Windows and POSIX are completely different. On
2247
+ Windows, the runtime can be set up so that the standard
2248
+ L<C<setlocale(3)>> function either only knows about the global locale or
2249
+ the locale for this thread. On POSIX, C<setlocale> always deals with
2250
+ the global locale, and other functions have been created to handle
2251
+ per-thread locales. Perl makes this transparent to perl-space code. It
2252
+ continues to use C<POSIX::setlocale()>, and the interpreter translates
2253
+ that into the per-thread functions.
2254
+
2255
+ All other locale-senstive functions automatically use the per-thread
2256
+ locale, if that is turned on, and failing that, the global locale. Thus
2257
+ calls to C<setlocale> are ineffective on POSIX systems for the current
2258
+ thread if that thread is using a per-thread locale. If perl is compiled
2259
+ for single-thread operation, it does not use the per-thread functions,
2260
+ so C<setlocale> does work as expected.
2261
+
2262
+ If you have loaded the L<C<POSIX>> module you can use the methods given
2263
+ in L<perlcall> to call L<C<POSIX::setlocale>|POSIX/setlocale> to safely
2264
+ change or query the locale (on systems where it is safe to do so), or
2265
+ you can use the new 5.28 function L<perlapi/Perl_setlocale> instead,
2266
+ which is a drop-in replacement for the system L<C<setlocale(3)>>, and
2267
+ handles single-threaded and multi-threaded applications transparently.
2268
+
2269
+ There are some locale-related library calls that still aren't
2270
+ thread-safe because they return data in a buffer global to all threads.
2271
+ In the past, these didn't matter as locales weren't thread-safe at all.
2272
+ But now you have to be aware of them in case your module is called in a
2273
+ multi-threaded application. The known ones are
2274
+
2275
+ asctime()
2276
+ ctime()
2277
+ gcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
2278
+ getdate()
2279
+ wcrtomb() if its final argument is NULL
2280
+ wcsrtombs() if its final argument is NULL
2281
+ wcstombs()
2282
+ wctomb()
2283
+
2284
+ Some of these shouldn't really be called in a Perl application, and for
2285
+ others there are thread-safe versions of these already implemented:
2286
+
2287
+ asctime_r()
2288
+ ctime_r()
2289
+ Perl_langinfo()
2290
+
2291
+ The C<_r> forms are automatically used, starting in Perl 5.28, if you
2292
+ compile your code, with
2293
+
2294
+ #define PERL_REENTRANT
2295
+
2296
+ See also L<perlapi/Perl_langinfo>.
2297
+ You can use the methods given in L<perlcall>, to get the best available
2298
+ locale-safe versions of these
2299
+
2300
+ POSIX::localeconv()
2301
+ POSIX::wcstombs()
2302
+ POSIX::wctomb()
2303
+
2304
+ And note, that some items returned by C<Localeconv> are available
2305
+ through L<perlapi/Perl_langinfo>.
2306
+
2307
+ The others shouldn't be used in a threaded application.
2308
+
2309
+ Some modules may call a non-perl library that is locale-aware. This is
2310
+ fine as long as it doesn't try to query or change the locale using the
2311
+ system C<setlocale>. But if these do call the system C<setlocale>,
2312
+ those calls may be ineffective. Instead,
2313
+ L<C<Perl_setlocale>|perlapi/Perl_setlocale> works in all circumstances.
2314
+ Plain setlocale is ineffective on multi-threaded POSIX 2008 systems. It
2315
+ operates only on the global locale, whereas each thread has its own
2316
+ locale, paying no attention to the global one. Since converting
2317
+ these non-Perl libraries to C<Perl_setlocale> is out of the question,
2318
+ there is a new function in v5.28
2319
+ C<switch_to_global_locale> that will
2320
+ switch the thread it is called from so that any system C<setlocale>
2321
+ calls will have their desired effect. The function
2322
+ L<C<sync_locale>|perlapi/sync_locale> must be called before returning to
2323
+ perl.
2324
+
2325
+ This thread can change the locale all it wants and it won't affect any
2326
+ other thread, except any that also have been switched to the global
2327
+ locale. This means that a multi-threaded application can have a single
2328
+ thread using an alien library without a problem; but no more than a
2329
+ single thread can be so-occupied. Bad results likely will happen.
2330
+
2331
+ In perls without multi-thread locale support, some alien libraries,
2332
+ such as C<Gtk> change locales. This can cause problems for the Perl
2333
+ core and other modules. For these, before control is returned to
2334
+ perl, starting in v5.20.1, calling the function
2237
2335
L<sync_locale()|perlapi/sync_locale> from XS should be sufficient to
2238
2336
avoid most of these problems. Prior to this, you need a pure Perl
2239
2337
statement that does this:
0 commit comments