Subject: | threads->list() in END block blocks |
Currently, PAR does not work with threads. I'd love to fix it on the PAR
side of things, but I'm stuck. Along the way, I think I found a bug in
threads:
PAR::Packer has some cleanup-code in an END{} block which must be run by
the last remaining thread only. We can't do anything about detached
threads, I suppose, but I thought checking that threads->list() == 1
would be sufficient for the usual case (no detached threads). So I
rebuilt the situation in PAR as simple as possible. See code below.
Turns out the following code gets into some locking problem or similar
during the threads->list() call:
#!/usr/bin/perl
use strict; use warnings;
use threads;
sub start_thread {
eval "use End;";
print "I am a thread.\n";
#END{eval"threads->list()"}
}
threads->create("start_thread")->join();
print "I am not a thread \n";
Whereas End.pm contains:
package End;
use strict; use warnings;
END {
print "CLEANUP!\n";
my $tid = threads->list();
if (not defined $tid or $tid == 0) {
print "MAIN THREAD!\n";
} else {
print "CHILD THREAD: $tid!\n";
}
}
1;
When I run the script, I get:
I am a thread.
CLEANUP!
and then, it just spins. (0% CPU use, so it's not a loop.)
This doesn't happen if I run the threads->list() in the main program,
i.e. if I comment out the "eval"use End;"" and comment in the
END{eval"threads->list()"}. Also, in reality, I'd do eval
"threads->list()" in End (aka PAR) because that degrades nicely in case
the user isn't even using threads. That doesn't make a difference, though.
Best regards,
Steffen
P.S.: I tested this with a very recent 5.10-devel checkout (see below)
as well as a Suse Linux 5.8.8. Both were using the newest threads (1.69)
and both showed the behaviour described. I also tested the 5.8.8 with
various threads releases. It seems that threads 1.57 is the first
release with the locking problem.
P.P.S: If you have any suggestions how to fix the original problem I'm
trying to work around, I'm all ears!
This is perl, v5.10.0 DEVEL33787 built for i686-linux-thread-multi
(with 1 registered patch, see perl -V for more detail)
Copyright 1987-2007, Larry Wall
Perl may be copied only under the terms of either the Artistic License
or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl". If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
OKG72|smueller@iklx107:/userdata2/smueller/par> perl -V
Summary of my perl5 (revision 5 version 10 subversion 0 patch 33787)
configuration:
Platform:
osname=linux, osvers=2.6.5-7.257-smp, archname=i686-linux-thread-multi
uname='linux iklx107 2.6.5-7.257-smp #1 smp mon may 15 14:14:14 utc
2006 i686 athlon i386 gnulinux '
config_args='-Dusedevel
-Dprefix=/userdata2/smueller/bleadperl/install5.10.107 -Dusethreads
-Dsiteprefix=/userdata2/smueller/bleadperl/install5.10.107'
hint=recommended, useposix=true, d_sigaction=define
useithreads=define, usemultiplicity=define
useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
use64bitint=undef, use64bitall=undef, uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='ccache cc -m32', ccflags ='-D_REENTRANT -D_GNU_SOURCE
-fno-strict-aliasing -pipe -I/usr/local/include -D_LARGEFILE_SOURCE
-D_FILE_OFFSET_BITS=64',
optimize='-O2',
cppflags='-D_REENTRANT -D_GNU_SOURCE -fno-strict-aliasing -pipe
-I/usr/local/include'
ccversion='', gccversion='3.3.3 (SuSE Linux)', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t',
lseeksize=8
alignbytes=4, prototype=define
Linker and Libraries:
ld='ccache cc -m32', ldflags =' -L/usr/local/lib'
libpth=/usr/local/lib /lib /usr/lib
libs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc
perllibs=-lnsl -ldl -lm -lcrypt -lutil -lpthread -lc
libc=, so=so, useshrplib=false, libperl=libperl.a
gnulibc_version='2.3.3'
Dynamic Linking:
dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
cccdlflags='-fPIC', lddlflags='-shared -O2 -L/usr/local/lib'
Characteristics of this binary (from libperl):
Compile-time options: MULTIPLICITY PERL_DONT_CREATE_GVSV
PERL_IMPLICIT_CONTEXT PERL_MALLOC_WRAP USE_ITHREADS
USE_LARGE_FILES USE_PERLIO USE_REENTRANT_API
Locally applied patches:
MAINT33535
Built under linux
Compiled at May 6 2008 13:59:36
%ENV:
PERL5LIB="/users/ik3al1/smueller/local/perl5lib:/users/ik3al1/smueller/perl/lib/perl5/site_perl/5.10.0:/users/ik3al1/smueller/perl/lib/perl5/5.10.0:/users/ik3al1/smueller/perl/lib/5.10.0:/users/ik3al1/smueller/perl/lib/site_perl/5.10.0"
PERLVERSION="5.10.0"
@INC:
/users/ik3al1/smueller/local/perl5lib
/users/ik3al1/smueller/perl/lib/perl5/site_perl/5.10.0/i686-linux-thread-multi
/users/ik3al1/smueller/perl/lib/perl5/site_perl/5.10.0
/users/ik3al1/smueller/perl/lib/perl5/5.10.0/i686-linux-thread-multi
/users/ik3al1/smueller/perl/lib/perl5/5.10.0
/users/ik3al1/smueller/perl/lib/5.10.0/i686-linux-thread-multi
/users/ik3al1/smueller/perl/lib/5.10.0
/users/ik3al1/smueller/perl/lib/site_perl/5.10.0/i686-linux-thread-multi
/users/ik3al1/smueller/perl/lib/site_perl/5.10.0
/userdata2/smueller/bleadperl/install5.10.107/lib/5.10.0/i686-linux-thread-multi
/userdata2/smueller/bleadperl/install5.10.107/lib/5.10.0
/userdata2/smueller/bleadperl/install5.10.107/lib/site_perl/5.10.0/i686-linux-thread-multi
/userdata2/smueller/bleadperl/install5.10.107/lib/site_perl/5.10.0
~smueller/local/perl5lib
.