Browse files

Further eliminate POSIX-emulation under LinuxThreads

Under POSIX threads the getpid() and getppid() functions return the
same values across multiple threads, i.e. threads don't have their own
PID's. This is not the case under the obsolete LinuxThreads where each
thread has a different PID, so getpid() and getppid() will return
different values across threads.

Ever since the first perl 5.0 we've returned POSIX-consistent
semantics for $$, until v5.14.0-251-g0e21945 when the getpid() cache
was removed. In 5.8.1 Rafael added further explicit POSIX emulation in
perl-5.8.0-133-g4d76a34 [1] by explicitly caching getppid(), so that
multiple threads would always return the same value.

I don't think all this effort to emulate POSIX sematics is worth it. I
think $$ and getppid() are OS-level functions that should always
return the same as their C equivalents. I shouldn't have to use a
module like Linux::Pid to get the OS version of the return values.

This is pretty much a complete non-issue in practice these days,
LinuxThreads was a Linux 2.4 thread implementation that nobody
maintains anymore[2], all modern Linux distros use NPTL threads which
don't suffer from this discrepancy. Debian GNU/kFreeBSD does use
LinuxThreads in the 6.0 release, but they too will be moving away from
it in future releases, and really, nobody uses Debian GNU/kFreeBSD

This caching makes it unnecessarily tedious to fork an embedded Perl
interpreter. When someone that constructs an embedded perl interpreter
and forks their application, the fork(2) system call isn't going to
run Perl_pp_fork(), and thus the return value of $$ and getppid()
doesn't reflect the current process. See [3] for a bug in uWSGI
related to this, and Perl::AfterFork on the CPAN for XS code that you
need to run after forking a PerlInterpreter unbeknownst to perl.

We've already been failing the tests in t/op/getpid.t on these Linux
systems that nobody apparently uses, the Debian GNU/kFreeBSD users did
notice and filed #96270, this patch fixes that failure by changing the
tests to test for different behavior under LinuxThreads, I've tested
that this works on my Debian GNU/kFreeBSD 6.0.4 virtual machine.

If this change is found to be unacceptable (i.e. we want to continue
to emulate POSIX thread semantics for the sake of LinuxThreads) we
also need to revert v5.14.0-251-g0e21945, because currently we're only
emulating POSIX semantics for getppid(), not getpid(). But I don't
think we should do that, both v5.14.0-251-g0e21945 and this commit are

This commit includes a change to embedvar.h made by "make

  • Loading branch information...
Ævar Arnfjörð Bjarmason
Ævar Arnfjörð Bjarmason committed Feb 11, 2012
1 parent 804b24f commit d7c042c9f3475e045bbea4f4880efff973b7049d
Showing with 50 additions and 53 deletions.
  1. +0 −1 embedvar.h
  2. +1 −7 hints/
  3. +0 −5 intrpvar.h
  4. +0 −4
  5. +0 −3 perl.c
  6. +0 −3 perl.h
  7. +14 −0 pod/perldelta.pod
  8. +5 −6 pod/perlfunc.pod
  9. +17 −5 pod/perlvar.pod
  10. +0 −10 pp_sys.c
  11. +0 −4 sv.c
  12. +13 −2 t/op/getpid.t
  13. +0 −3 util.c
@@ -265,7 +265,6 @@
#define PL_perlio (vTHX->Iperlio)
#define PL_phase (vTHX->Iphase)
#define PL_pidstatus (vTHX->Ipidstatus)
-#define PL_ppid (vTHX->Ippid)
#define PL_preambleav (vTHX->Ipreambleav)
#define PL_profiledata (vTHX->Iprofiledata)
#define PL_psig_name (vTHX->Ipsig_name)
@@ -365,15 +365,9 @@ fi
# This script UU/usethreads.cbu will get 'called-back' by Configure
# after it has prompted the user for whether to use threads.
cat > UU/usethreads.cbu <<'EOCBU'
-if getconf GNU_LIBPTHREAD_VERSION | grep NPTL >/dev/null 2>/dev/null
- threadshavepids=""
- threadshavepids="-DTHREADS_HAVE_PIDS"
case "$usethreads" in
- ccflags="-D_REENTRANT -D_GNU_SOURCE $threadshavepids $ccflags"
+ ccflags="-D_REENTRANT -D_GNU_SOURCE $ccflags"
if echo $libswanted | grep -v pthread >/dev/null
set `echo X "$libswanted "| sed -e 's/ c / pthread c /'`
@@ -801,11 +801,6 @@ PERLVAR(I, memory_debug_header, struct perl_memory_debug_header)
PERLVARI(I, dumper_fd, int, -1)
-/* Stores the PPID */
-PERLVARI(I, ppid, IV, 0)
#ifdef PERL_MAD
PERLVARI(I, madskills, bool, FALSE) /* preserve all syntactic info */
/* (MAD = Misc Attribute Decoration) */
@@ -422,10 +422,6 @@ sub readvar {
-unless ($define{'THREADS_HAVE_PIDS'}) {
- ++$skip{PL_ppid};
unless ($define{'PERL_NEED_APPCTX'}) {
3 perl.c
@@ -4207,9 +4207,6 @@ S_init_postdump_symbols(pTHX_ register int argc, register char **argv, register
#endif /* !PERL_MICRO */
- PL_ppid = (IV)getppid();
/* touch @F array to prevent spurious warnings 20020415 MJD */
if (PL_minus_a) {
3 perl.h
@@ -4693,9 +4693,6 @@ EXTCONST char PL_bincompat_options[] =
# endif
-# endif
# ifdef USE_64_BIT_ALL
# endif
@@ -212,6 +212,20 @@ and the API will not be considered stable until v5.16.
See L<Unicode::UCD/prop_invmap()> for details on the new interface.
+=head2 C<$$> and C<getppid()> no longer emulate POSIX semantics under LinuxThreads
+The POSIX emulation of C<$$> and C<getppid()> under the obsolete
+LinuxThreads implementation has been removed (the C<$$> emulation was
+actually removed in v5.15.0). This only impacts users of Linux 2.4 and
+users of Debian GNU/kFreeBSD up to and including 6.0, not the vast
+majority of Linux installations that use NPTL threads.
+This means that C<getppid()> like C<$$> is now always guaranteed to
+return the OS's idea of the current state of the process, not perl's
+cached version of it.
+See the documentation for L<$$|perlvar/$$> for details.
=head1 Deprecations
XXX Any deprecated features, syntax, modules etc. should be listed here.
@@ -2352,12 +2352,11 @@ X<getppid> X<parent> X<pid>
Returns the process id of the parent process.
-Note for Linux users: on Linux, the C functions C<getpid()> and
-C<getppid()> return different values from different threads. In order to
-be portable, this behavior is not reflected by the Perl-level function
-C<getppid()>, that returns a consistent value across threads. If you want
-to call the underlying C<getppid()>, you may use the CPAN module
+Note for Linux users: Between v5.8.1 and v5.16.0 Perl would work
+around non-POSIX thread semantics the minority of Linux systems (and
+Debian GNU/kFreeBSD systems) that used LinuxThreads, this emulation
+has since been removed. See the documentation for L<$$|perlvar/$$> for
Portability issues: L<perlport/getppid>.
@@ -191,11 +191,23 @@ this variable, doing so is generally discouraged, although it can be
invaluable for some testing purposes. It will be reset automatically
across C<fork()> calls.
-Note for Linux users: on Linux, the C functions C<getpid()> and
-C<getppid()> return different values from different threads. In order to
-be portable, this behavior is not reflected by C<$$>, whose value remains
-consistent across threads. If you want to call the underlying C<getpid()>,
-you may use the CPAN module C<Linux::Pid>.
+Note for Linux and Debian GNU/kFreeBSD users: Before Perl v5.16.0 perl
+would emulate POSIX semantics on Linux systems using LinuxThreads, a
+partial implementation of POSIX Threads that has since been superseded
+by the Native POSIX Thread Library (NPTL).
+LinuxThreads is now obsolete on Linux, and and caching C<getpid()>
+like this made embedding perl unnecessarily complex (since you'd have
+to manually update the value of $$), so now C<$$> and C<getppid()>
+will always return the same values as the underlying C library.
+Debian GNU/kFreeBSD systems also used LinuxThreads up until and
+including the 6.0 release, but after that moved to FreeBSD thread
+semantics, which are POSIX-like.
+To see if your system is affected by this discrepancy check if
+C<getconf GNU_LIBPTHREAD_VERSION | grep -q NPTL> returns a false
+value. NTPL threads preserve the POSIX semantics.
Mnemonic: same as shells.
@@ -4043,9 +4043,6 @@ PP(pp_fork)
if (childpid < 0)
if (!childpid) {
- PL_ppid = (IV)getppid();
hv_clear(PL_pidstatus); /* no kids, so don't wait for 'em */
@@ -4336,14 +4333,7 @@ PP(pp_getppid)
- if (PL_ppid != 1 && getppid() == 1)
- /* maybe the parent process has died. Refresh ppid cache */
- PL_ppid = 1;
- XPUSHi( PL_ppid );
-# else
XPUSHi( getppid() );
-# endif
DIE(aTHX_ PL_no_func, "getppid");
4 sv.c
@@ -13083,10 +13083,6 @@ perl_clone_using(PerlInterpreter *proto_perl, UV flags,
PL_globhook = proto_perl->Iglobhook;
- PL_ppid = proto_perl->Ippid;
/* swatch cache */
PL_last_swash_hv = NULL; /* reinits on demand */
PL_last_swash_klen = 0;
@@ -30,5 +30,16 @@ my $ppid2 : shared = 0;
new threads( sub { ($pid2, $ppid2) = ($$, getppid()); } ) -> join();
-is($pid, $pid2, 'pids');
-is($ppid, $ppid2, 'ppids');
+# If this breaks you're either running under LinuxThreads (and we
+# haven't detected it) or your system doesn't have POSIX thread
+# semantics.
+if ($^O =~ /^(?:gnukfreebsd|linux)$/ and
+ (my $linuxthreads = qx[getconf GNU_LIBPTHREAD_VERSION 2>&1]) =~ /linuxthreads/) {
+ chomp $linuxthreads;
+ diag "We're running under $^O with linuxthreads <$linuxthreads>";
+ isnt($pid, $pid2, "getpid() in a thread is different from the parent on this non-POSIX system");
+ isnt($ppid, $ppid2, "getppid() in a thread is different from the parent on this non-POSIX system");
+} else {
+ is($pid, $pid2, 'getpid() in a thread is the same as in the parent');
+ is($ppid, $ppid2, 'getppid() in a thread is the same as in the parent');
3 util.c
@@ -2857,9 +2857,6 @@ Perl_my_popen(pTHX_ const char *cmd, const char *mode)
default, binary, low-level mode; see PerlIOBuf_open(). */
PerlLIO_setmode((*mode == 'r'), O_BINARY);
- PL_ppid = (IV)getppid();
PL_forkprocess = 0;
hv_clear(PL_pidstatus); /* we have no children */

0 comments on commit d7c042c

Please sign in to comment.