X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=t%2Flib%2Fautovivification%2FTestThreads.pm;fp=t%2Flib%2Fautovivification%2FTestThreads.pm;h=2fe58e02635523f44f4a082c0e7447924781e908;hb=bff16fe7ea455b013bb0e681f2852f6c11d72636;hp=0000000000000000000000000000000000000000;hpb=e1d2632ef0b4e3ee3e418c12903f81b5d7b55b1a;p=perl%2Fmodules%2Fautovivification.git diff --git a/t/lib/autovivification/TestThreads.pm b/t/lib/autovivification/TestThreads.pm new file mode 100644 index 0000000..2fe58e0 --- /dev/null +++ b/t/lib/autovivification/TestThreads.pm @@ -0,0 +1,68 @@ +package autovivification::TestThreads; + +use strict; +use warnings; + +use Config qw<%Config>; + +sub skipall { + my ($msg) = @_; + require Test::Leaner; + Test::Leaner::plan(skip_all => $msg); +} + +sub diag { + require Test::Leaner; + Test::Leaner::diag(@_); +} + +sub import { + shift; + + require autovivification; + + skipall 'This autovivification isn\'t thread safe' + unless autovivification::A_THREADSAFE(); + + my $force = $ENV{PERL_AUTOVIVIFICATION_TEST_THREADS} ? 1 : !1; + skipall 'This perl wasn\'t built to support threads' + unless $Config{useithreads}; + skipall 'perl 5.13.4 required to test thread safety' + unless $force or "$]" >= 5.013004; + + my $t_v = $force ? '0' : '1.67'; + my $has_threads = do { + local $@; + eval "use threads $t_v; 1"; + }; + skipall "threads $t_v required to test thread safety" unless $has_threads; + + defined and diag "Using threads $_" for $threads::VERSION; + + my %exports = ( + spawn => \&spawn, + ); + + my $pkg = caller; + while (my ($name, $code) = each %exports) { + no strict 'refs'; + *{$pkg.'::'.$name} = $code; + } +} + +sub spawn { + local $@; + my @diag; + my $thread = eval { + local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" }; + threads->create(@_); + }; + push @diag, "Thread creation error: $@" if $@; + if (@diag) { + require Test::Leaner; + Test::Leaner::diag($_) for @diag; + } + return $thread ? $thread : (); +} + +1;