Skip to content

Commit 7aaa36b

Browse files
committed
locale.c: Use strerror_l if platform has it
strerror_l makes the my_strerror function trivial, as it doesn't have to worry about critical sections, etc. Even on unthreaded perls, it avoids having to change the current locale, and then change it back.
1 parent 5277094 commit 7aaa36b

File tree

1 file changed

+26
-7
lines changed

1 file changed

+26
-7
lines changed

locale.c

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2728,9 +2728,27 @@ Perl_my_strerror(pTHX_ const int errnum)
27282728

27292729
const bool within_locale_scope = IN_LC(LC_MESSAGES);
27302730

2731-
# ifdef USE_POSIX_2008_LOCALE
2731+
# if defined(HAS_POSIX_2008_LOCALE) && defined(HAS_STRERROR_L)
2732+
2733+
/* This function is trivial if we have strerror_l() */
2734+
2735+
if (within_locale_scope) {
2736+
errstr = strerror(errnum);
2737+
}
2738+
else {
2739+
errstr = strerror_l(errnum, PL_C_locale_obj);
2740+
}
2741+
2742+
errstr = savepv(errstr);
2743+
2744+
# else /* Doesn't have strerror_l(). */
2745+
2746+
# ifdef USE_POSIX_2008_LOCALE
2747+
27322748
locale_t save_locale = NULL;
2733-
# else
2749+
2750+
# else
2751+
27342752
char * save_locale = NULL;
27352753
bool locale_is_C = FALSE;
27362754

@@ -2739,7 +2757,7 @@ Perl_my_strerror(pTHX_ const int errnum)
27392757
* setlocale() ) */
27402758
LOCALE_LOCK;
27412759

2742-
# endif
2760+
# endif
27432761

27442762
DEBUG_Lv(PerlIO_printf(Perl_debug_log,
27452763
"my_strerror called with errnum %d\n", errnum));
@@ -2761,7 +2779,7 @@ Perl_my_strerror(pTHX_ const int errnum)
27612779
"uselocale returned 0x%p\n", save_locale));
27622780
}
27632781

2764-
# else /* Not thread-safe build */
2782+
# else /* Not thread-safe build */
27652783

27662784
save_locale = setlocale(LC_MESSAGES, NULL);
27672785
if (! save_locale) {
@@ -2781,7 +2799,7 @@ Perl_my_strerror(pTHX_ const int errnum)
27812799
}
27822800
}
27832801

2784-
# endif
2802+
# endif
27852803

27862804
} /* end of ! within_locale_scope */
27872805
else {
@@ -2807,7 +2825,7 @@ Perl_my_strerror(pTHX_ const int errnum)
28072825
}
28082826
}
28092827

2810-
# else
2828+
# else
28112829

28122830
if (save_locale && ! locale_is_C) {
28132831
if (! setlocale(LC_MESSAGES, save_locale)) {
@@ -2820,7 +2838,8 @@ Perl_my_strerror(pTHX_ const int errnum)
28202838

28212839
LOCALE_UNLOCK;
28222840

2823-
# endif
2841+
# endif
2842+
# endif /* End of doesn't have strerror_l */
28242843
#endif /* End of does have locale messages */
28252844

28262845
#ifdef DEBUGGING

0 commit comments

Comments
 (0)