Skip to content

Commit 6666be0

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
1 parent 0f3f89d commit 6666be0

File tree

8 files changed

+44
-27
lines changed

8 files changed

+44
-27
lines changed

dist/threads/lib/threads.pm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use 5.008;
55
use strict;
66
use warnings;
77

8-
our $VERSION = '2.38'; # remember to update version in POD!
8+
our $VERSION = '2.39'; # remember to update version in POD!
99
my $XS_VERSION = $VERSION;
1010
$VERSION = eval $VERSION;
1111

@@ -134,7 +134,7 @@ threads - Perl interpreter-based threads
134134
135135
=head1 VERSION
136136
137-
This document describes threads version 2.38
137+
This document describes threads version 2.39
138138
139139
=head1 WARNING
140140

dist/threads/threads.xs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,6 @@ S_ithread_set(pTHX_ ithread *thread)
193193
{
194194
dMY_CXT;
195195
MY_CXT.context = thread;
196-
#ifdef PERL_SET_NON_tTHX_CONTEXT
197-
PERL_SET_NON_tTHX_CONTEXT(thread->interp);
198-
#endif
199196
}
200197

201198
STATIC ithread *

embed.fnc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6226,7 +6226,7 @@ Adhp |SSize_t|PerlIO_write |NULLOK PerlIO *f \
62266226
|Size_t count
62276227
#endif /* defined(USE_PERLIO) */
62286228
#if defined(USE_PERL_SWITCH_LOCALE_CONTEXT)
6229-
CTop |void |switch_locale_context
6229+
Cop |void |switch_locale_context
62306230
#endif
62316231
#if defined(USE_QUADMATH)
62326232
Tdp |bool |quadmath_format_needed \

locale.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8538,19 +8538,38 @@ S_my_setlocale_debug_string_i(pTHX_
85388538
#ifdef USE_PERL_SWITCH_LOCALE_CONTEXT
85398539

85408540
void
8541-
Perl_switch_locale_context()
8541+
Perl_switch_locale_context(pTHX)
85428542
{
85438543
/* libc keeps per-thread locale status information in some configurations.
85448544
* So, we can't just switch out aTHX to switch to a new thread. libc has
85458545
* to follow along. This routine does that based on per-interpreter
8546-
* variables we keep just for this purpose */
8547-
8548-
/* Can't use pTHX, because we may be called from a place where that
8549-
* isn't available */
8550-
dTHX;
8546+
* variables we keep just for this purpose.
8547+
*
8548+
* There are two implementations where this is an issue. For the other
8549+
* implementations, it doesn't matter because libc is using global values
8550+
* that all threads know about.
8551+
*
8552+
* The two implementations are where libc keeps thread-specific information
8553+
* on its own. These are
8554+
*
8555+
* POSIX 2008: The current locale is kept by libc as an object. We save
8556+
* a copy of that in the per-thread PL_cur_locale_obj, and so
8557+
* this routine uses that copy to tell the thread it should be
8558+
* operating with that object
8559+
* Windows thread-safe locales: A given thread in Windows can be being run
8560+
* with per-thread locales, or not. When the thread context
8561+
* changes, libc doesn't automatically know if the thread is
8562+
* using per-thread locales, nor does it know what the new
8563+
* thread's locale is. We keep that information in the
8564+
* per-thread variables:
8565+
* PL_controls_locale indicates if this thread is using
8566+
* per-thread locales or not
8567+
* PL_cur_LC_ALL indicates what the the locale
8568+
* should be if it is a per-thread
8569+
* locale.
8570+
*/
85518571

8552-
if (UNLIKELY( aTHX == NULL
8553-
|| PL_veto_switch_non_tTHX_context
8572+
if (UNLIKELY( PL_veto_switch_non_tTHX_context
85548573
|| PL_phase == PERL_PHASE_CONSTRUCT))
85558574
{
85568575
return;

perl.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6359,21 +6359,21 @@ EXTCONST U8 PL_deBruijn_bitpos_tab64[];
63596359
#ifdef USE_PERL_SWITCH_LOCALE_CONTEXT
63606360
# define PERL_SET_LOCALE_CONTEXT(i) \
63616361
STMT_START { \
6362-
if (UNLIKELY(PL_veto_switch_non_tTHX_context)) \
6363-
Perl_switch_locale_context(); \
6362+
if (LIKELY(! PL_veto_switch_non_tTHX_context)) \
6363+
Perl_switch_locale_context(i); \
63646364
} STMT_END
6365+
6366+
/* In some Configurations there may be per-thread information that is
6367+
* carried in a library instead of perl's tTHX structure. This macro is to
6368+
* be used to handle those when tTHX is changed. Only locale handling is
6369+
* currently known to be affected. */
6370+
# define PERL_SET_NON_tTHX_CONTEXT(i) \
6371+
STMT_START { if (i) PERL_SET_LOCALE_CONTEXT(i); } STMT_END
63656372
#else
6366-
# define PERL_SET_LOCALE_CONTEXT(i) NOOP
6373+
# define PERL_SET_LOCALE_CONTEXT(i) NOOP
6374+
# define PERL_SET_NON_tTHX_CONTEXT(i) NOOP
63676375
#endif
63686376

6369-
/* In some Configurations there may be per-thread information that is carried
6370-
* in a library instead of perl's tTHX structure. This macro is to be used to
6371-
* handle those when tTHX is changed. Only locale handling is currently known
6372-
* to be affected. */
6373-
#define PERL_SET_NON_tTHX_CONTEXT(i) \
6374-
STMT_START { PERL_SET_LOCALE_CONTEXT(i); } STMT_END
6375-
6376-
63776377
#ifndef PERL_GET_CONTEXT
63786378
# define PERL_GET_CONTEXT PERL_GET_INTERP
63796379
#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
@@ -3587,7 +3587,7 @@ Perl_set_context(void *t)
35873587
}
35883588
# endif
35893589

3590-
PERL_SET_NON_tTHX_CONTEXT(t);
3590+
PERL_SET_NON_tTHX_CONTEXT((PerlInterpreter *) t);
35913591

35923592
#else
35933593
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)