Skip to content

Commit

Permalink
For C++ extensions, use the pthreads definition of PERL_GET_CONTEXT
Browse files Browse the repository at this point in the history
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 on some
platforms the C++ compiler can use different syntax for thread local storage.
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
pthread_setspecific().
  • Loading branch information
nwc10 committed Jan 4, 2022
1 parent 676e7ef commit ec238e5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions thread.h
Expand Up @@ -372,8 +372,16 @@
# define PTHREAD_GETSPECIFIC(key) pthread_getspecific(key)
#endif

#if defined(PERL_THREAD_LOCAL) && !defined(PERL_GET_CONTEXT) && !defined(PERL_SET_CONTEXT)
/* Use C11 thread-local storage, where possible: */
#if defined(PERL_THREAD_LOCAL) && !defined(PERL_GET_CONTEXT) && !defined(PERL_SET_CONTEXT) && !defined(__cplusplus)
/* Use C11 thread-local storage, where possible.
* Frustratingly we can't (reliably) use it for C++ extensions, as some

This comment has been minimized.

Copy link
@tonycoz

tonycoz Jan 4, 2022

Contributor

I think saying it's a problem with some C++ compilers is incorrect - it's a basic incompatibility between the languages.

Your commit message points out that the commit is minimal because we already set the context using pthreads, updating the comment:

/* Set our thread-specific value anyway, in case code is reading it directly. */

to indicate that the value is actually being used might help anyone reading the code in the future.

* compilers on some platforms use one token for it for C, and a different token
* for it for C++, meaning that the working token that Configure probed for C
* turns out to be a compiler error on C++. Great.
* 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++.
* Of course, if C++ XS extensions really want to avoid *all* this overhead,
* they should #define PERL_NO_GET_CONTEXT and pass aTHX/aTHX_ explicitly) */
# define PERL_USE_THREAD_LOCAL
extern PERL_THREAD_LOCAL void *PL_current_context;

Expand Down

4 comments on commit ec238e5

@mkothar2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are getting the below error with perl 5.36.0 :

bash-4.4$ CC -mt -xildoff -zdefs -g -L lib -L /scratch/mkothari/perl-5.36.0/12.0/solaris/lib/5.36.0/sun4-solaris-thread-multi-64int-ld/CORE -G -z nocompstrtab -o pcmif.so -L . pcmif.o -lportal -lpinsys -lpcmext -lCrun -lperl -lnsl -lsocket -lposix4 -lthread -lCrun -lm -lc || ( rm -f pcmif.so; exit 1 )

ld: warning: symbol 'PL_current_context' has differing types:

    (file pcmif.o type=OBJT; file /scratch/mkothari/perl-5.36.0/12.0/solaris/lib/5.36.0/sun4-solaris-thread-multi-64int-ld/CORE/libperl.so type=TLS );

ld: fatal: relocation error: R_SPARC_GOTDATA_OP_HIX22: file pcmif.o: symbol PL_current_context: relocation illegal for TLS symbol

Command Used :
sh Configure -des -Dcc="cc" -Dusethreads -Dusemorebits -Dprefix=/scratch/mkothari/perl/perl-5.36.0/12.0/solaris && make && make test && make install

@mkothar2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get some help on this ?

@tonycoz
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please open a ticket with the details of your perl build (the perl -V output) and whatever you're trying to build against.

@mkothar2
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.