develooper Front page | perl.perl5.changes | Postings from January 2022

[Perl/perl5] c4b685: For C++ extensions, use the pthreads definitionof...

Nicholas Clark via perl5-changes
January 13, 2022 07:14
[Perl/perl5] c4b685: For C++ extensions, use the pthreads definitionof...
Message ID:
  Branch: refs/heads/blead
  Commit: c4b6855dd57cd9a2ac4647c33c5636ed7d15d64a
  Author: Nicholas Clark <>
  Date:   2022-01-12 (Wed, 12 Jan 2022)

  Changed paths:
    M thread.h

  Log Message:
  For C++ extensions, use the pthreads definition of PERL_GET_CONTEXT

Configure probes the C compiler to find out whether it supports C11 thread
local storage, and if found uses this for PERL_SET_CONTEXT/PERL_GET_CONTEXT,
in preference to the pthread_setspecific()/pthread_getspecific() approach.

However, we can come unstuck with XS extensions written in C++, as C++ and C
disagree on the syntax used for thread local storage, meaning that the
working token that Configure probed for C turns out to be a compiler error
on C++.

As Configure doesn't have a way to probe for C++ dialects, we just take the
safe option and do the same as 5.34.0 and earlier - use pthreads on C++.

This commit is minimal because the implementation of PERL_SET_CONTEXT for
C11 thread local storage was already defensively written to *also* call

  Commit: 34e35871651eac526a753c06710cac081848d75f
  Author: Nicholas Clark <>
  Date:   2022-01-12 (Wed, 12 Jan 2022)

  Changed paths:
    M thread.h
    M util.c

  Log Message:
  PERL_SET_CONTEXT(t) needs to set PL_current_context from C++ too

We want to use C11 thread local storage where possible, as its read overhead
is lower. However C++ uses incompatible syntax for TLS, so we have to hide
the variable from C++ extensions, and have the read code "fall back" to the
pthread_getspecific() approach.

However, C++ extensions also need to be able to call PERL_SET_CONTEXT(t) and
have everything still work the same as C extensions. Hence they *also* need
to set PL_current_context - a variable that has to be hidden from them.
Hence change the version of the PERL_SET_CONTEXT macro used by C++
extensions from inline code to a call to Perl_set_context(), and have that
set PL_current_context (when used).

Technically this commit also fixes a bug in that Perl_set_context() is API
and should have been setting PL_current_context since that variable was
first introduced. In practice, likely nothing uses this API, as there is no
code on CPAN that calls it, and the macro PERL_SET_CONTEXT() is much more

Compare: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About