Skip to content

Commit 5c459b6

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 84f8711 commit 5c459b6

File tree

7 files changed

+44
-25
lines changed

7 files changed

+44
-25
lines changed

dist/threads/threads.xs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ S_ithread_set(pTHX_ ithread *thread)
220220
dMY_CXT;
221221
DEBUG_U(PerlIO_printf(Perl_debug_log, "ithread_set about to set MY_CXT context to thread %p; tid=%ld\n", thread, thread->tid));
222222
MY_CXT.context = thread;
223-
#ifdef PERL_SET_NON_tTHX_CONTEXT
224223
PERL_SET_NON_tTHX_CONTEXT(thread->interp);
225-
DEBUG_U(PerlIO_printf(Perl_debug_log, "ithread_set just set MY_CXT context to thread\n"));
226-
#endif
227224
}
228225

229226
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: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9844,19 +9844,40 @@ S_my_setlocale_debug_string_i(pTHX_
98449844
#ifdef USE_PERL_SWITCH_LOCALE_CONTEXT
98459845

98469846
void
9847-
Perl_switch_locale_context()
9847+
Perl_switch_locale_context(pTHX)
98489848
{
98499849
/* libc keeps per-thread locale status information in some configurations.
98509850
* So, we can't just switch out aTHX to switch to a new thread. libc has
98519851
* to follow along. This routine does that based on per-interpreter
9852-
* variables we keep just for this purpose */
9853-
9854-
/* Can't use pTHX, because we may be called from a place where that
9855-
* isn't available */
9856-
dTHX;
9852+
* variables we keep just for this purpose.
9853+
*
9854+
* There are two implementations where this is an issue. For the other
9855+
* implementations, it doesn't matter because libc is using global values
9856+
* that all threads know about. This is true even for the thread-safe
9857+
* emulation, as everything to libc is still a global, and we use
9858+
* PL_curlocales (for example) to know what the correct locale(s) should
9859+
* be, and this variable is under control of aTHX.
9860+
*
9861+
* The two implementations are where libc keeps thread-specific information
9862+
* on its own. These are
9863+
*
9864+
* POSIX 2008: The current locale is kept by libc as an object. We save
9865+
* a copy of that in the per-thread PL_cur_locale_obj, and so
9866+
* this routine uses that copy to tell the thread it should be
9867+
* operating with that object
9868+
* Windows thread-safe locales: A given thread in Windows can be being run
9869+
* with per-thread locales, or not. When the thread context
9870+
* changes, libc doesn't automatically know if the thread is
9871+
* using per-thread locales, nor does it know what the new
9872+
* thread's locale is. We keep that information in the
9873+
* per-thread variables:
9874+
* PL_controls_locale for if this thread is using
9875+
* per-thread locales or not
9876+
* PL_cur_LC_ALL for what the the locale should be if
9877+
* it is a per-thread locale.
9878+
*/
98579879

9858-
if (UNLIKELY( aTHX == NULL
9859-
|| PL_veto_switch_non_tTHX_context
9880+
if (UNLIKELY( PL_veto_switch_non_tTHX_context
98609881
|| PL_phase == PERL_PHASE_CONSTRUCT))
98619882
{
98629883
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)