use strict;
use warnings;
-use Config qw<%Config>;
-
-BEGIN {
- my $has_threads = do {
- local $@;
- $Config{useithreads} and eval "use threads; 1";
- };
- # Load Test::More after threads
- require Test::More;
- Test::More->import;
- if ($has_threads) {
- plan(tests => 4 * 10);
- } else {
- plan(skip_all => 'This perl wasn\'t built to support threads');
- }
-}
+use lib 't/lib';
+use Linux::SysInfo::TestThreads;
+
+use Test::More 'no_plan';
use Linux::SysInfo qw<sysinfo>;
}
}
-my @t = map { threads->create(\&try, $_) } 1 .. 10;
-$_->join for @t;
+my @threads = map spawn(\&try, $_), 1 .. 10;
+
+$_->join for @threads;
+
+pass 'done';
--- /dev/null
+package Linux::SysInfo::TestThreads;
+
+use strict;
+use warnings;
+
+use Config qw<%Config>;
+
+use VPIT::TestHelpers;
+
+sub diag {
+ require Test::More;
+ Test::More::diag($_) for @_;
+}
+
+sub import {
+ shift;
+
+ my $force = $ENV{PERL_LINUX_SYSINFO_TEST_THREADS} ? 1 : !1;
+ skip_all 'This perl wasn\'t built to support threads'
+ unless $Config{useithreads};
+ skip_all 'perl 5.13.4 required to test thread safety'
+ unless $force or "$]" >= 5.013_004;
+
+ load_or_skip_all('threads', $force ? '0' : '1.67', [ ]);
+
+ 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 $@;
+ diag(@diag) if @diag;
+ return $thread ? $thread : ();
+}
+
+1;