Skip to content

Commit da149fe

Browse files
committed
switch_locale_context: Add aTHX
This fixes GH #21040 Instead of a dTHX, this passes aTHX automatically, and skips calling this function if there is no valid context. It moves that decision into the macro itself, avoiding some #ifdef directives. And it adds explanation f
1 parent f4e2609 commit da149fe

File tree

7 files changed

+45
-27
lines changed

7 files changed

+45
-27
lines changed

dist/threads/threads.xs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,7 @@ STATIC void
232232
S_ithread_set(pTHX_ ithread *thread)
233233
{
234234
dMY_CXT;
235-
DEBUG_U(PerlIO_printf(Perl_debug_log, "ithread_set about to set MY_CXT context to thread %p; tid=%ld\n", thread, thread->tid));
236235
MY_CXT.context = thread;
237-
#ifdef PERL_SET_NON_tTHX_CONTEXT
238-
PERL_SET_NON_tTHX_CONTEXT(thread->interp);
239-
DEBUG_U(PerlIO_printf(Perl_debug_log, "ithread_set just set MY_CXT context to thread\n"));
240-
#endif
241236
}
242237

243238
STATIC ithread *

embed.fnc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6179,7 +6179,7 @@ Adhp |SSize_t|PerlIO_write |NULLOK PerlIO *f \
61796179
|Size_t count
61806180
#endif /* defined(USE_PERLIO) */
61816181
#if defined(USE_PERL_SWITCH_LOCALE_CONTEXT)
6182-
CTop |void |switch_locale_context
6182+
Cop |void |switch_locale_context
61836183
#endif
61846184
#if defined(USE_QUADMATH)
61856185
Tdp |bool |quadmath_format_needed \

locale.c

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9907,19 +9907,41 @@ S_my_setlocale_debug_string_i(pTHX_
99079907
#ifdef USE_PERL_SWITCH_LOCALE_CONTEXT
99089908

99099909
void
9910-
Perl_switch_locale_context()
9910+
Perl_switch_locale_context(pTHX)
99119911
{
99129912
/* libc keeps per-thread locale status information in some configurations.
99139913
* So, we can't just switch out aTHX to switch to a new thread. libc has
99149914
* to follow along. This routine does that based on per-interpreter
9915-
* variables we keep just for this purpose */
9916-
9917-
/* Can't use pTHX, because we may be called from a place where that
9918-
* isn't available */
9919-
dTHX;
9915+
* variables we keep just for this purpose.
9916+
*
9917+
* There are two implementations where this is an issue. For the other
9918+
* implementations, it doesn't matter because libc is using global values
9919+
* that all threads know about. This is true even for the thread-safe
9920+
* emulation, as everything to libc is still a global, and we use
9921+
* PL_curlocales (for example) to know what the correct locale(s) should
9922+
* be, and this variable is under control of aTHX.
9923+
*
9924+
* The two implementations are where libc keeps thread-specific information
9925+
* on its own. These are
9926+
*
9927+
* POSIX 2008: The current locale is kept by libc as an object. We save
9928+
* a copy of that in the per-thread PL_cur_locale_obj, and so
9929+
* this routine uses that copy to tell the thread it should be
9930+
* operating with that object
9931+
* Windows thread-safe locales: A given thread in Windows can be being run
9932+
* with per-thread locales, or not. When the thread context
9933+
* changes, libc doesn't automatically know if the thread is
9934+
* using per-thread locales, nor does it know what the new
9935+
* thread's locale is. We keep that information in the
9936+
* per-thread variables:
9937+
* PL_controls_locale indicates if this thread is using
9938+
* per-thread locales or not
9939+
* PL_cur_LC_ALL indicates what the the locale
9940+
* should be if it is a per-thread
9941+
* locale.
9942+
*/
99209943

9921-
if (UNLIKELY( aTHX == NULL
9922-
|| PL_veto_switch_non_tTHX_context
9944+
if (UNLIKELY( PL_veto_switch_non_tTHX_context
99239945
|| PL_phase == PERL_PHASE_CONSTRUCT))
99249946
{
99259947
return;

perl.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6495,21 +6495,21 @@ EXTCONST U8 PL_deBruijn_bitpos_tab64[];
64956495
#ifdef USE_PERL_SWITCH_LOCALE_CONTEXT
64966496
# define PERL_SET_LOCALE_CONTEXT(i) \
64976497
STMT_START { \
6498-
if (UNLIKELY(PL_veto_switch_non_tTHX_context)) \
6499-
Perl_switch_locale_context(); \
6498+
if (LIKELY(! PL_veto_switch_non_tTHX_context)) \
6499+
Perl_switch_locale_context(i); \
65006500
} STMT_END
6501+
6502+
/* In some Configurations there may be per-thread information that is
6503+
* carried in a library instead of perl's tTHX structure. This macro is to
6504+
* be used to handle those when tTHX is changed. Only locale handling is
6505+
* currently known to be affected. */
6506+
# define PERL_SET_NON_tTHX_CONTEXT(i) \
6507+
STMT_START { if (i) PERL_SET_LOCALE_CONTEXT(i); } STMT_END
65016508
#else
6502-
# define PERL_SET_LOCALE_CONTEXT(i) NOOP
6509+
# define PERL_SET_LOCALE_CONTEXT(i) NOOP
6510+
# define PERL_SET_NON_tTHX_CONTEXT(i) NOOP
65036511
#endif
65046512

6505-
/* In some Configurations there may be per-thread information that is carried
6506-
* in a library instead of perl's tTHX structure. This macro is to be used to
6507-
* handle those when tTHX is changed. Only locale handling is currently known
6508-
* to be affected. */
6509-
#define PERL_SET_NON_tTHX_CONTEXT(i) \
6510-
STMT_START { PERL_SET_LOCALE_CONTEXT(i); } STMT_END
6511-
6512-
65136513
#ifndef PERL_GET_CONTEXT
65146514
# define PERL_GET_CONTEXT PERL_GET_INTERP
65156515
#endif

proto.h

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3591,7 +3591,7 @@ Perl_set_context(void *t)
35913591
}
35923592
# endif
35933593

3594-
PERL_SET_NON_tTHX_CONTEXT(t);
3594+
PERL_SET_NON_tTHX_CONTEXT((PerlInterpreter *) t);
35953595

35963596
#else
35973597
PERL_UNUSED_ARG(t);

win32/win32thread.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Perl_set_context(void *t)
1111
#if defined(USE_ITHREADS)
1212
# ifdef USE_DECLSPEC_THREAD
1313
Perl_current_context = t;
14+
PERL_SET_NON_tTHX_CONTEXT(t);
1415
# else
1516
DWORD err = GetLastError();
1617
TlsSetValue(PL_thr_key,t);

0 commit comments

Comments
 (0)