]> git.vpit.fr Git - perl/modules/Thread-Cleanup.git/blob - t/lib/Thread/Cleanup/TestThreads.pm
Skip threads tests unless perl version is 5.13.4 or greater
[perl/modules/Thread-Cleanup.git] / t / lib / Thread / Cleanup / TestThreads.pm
1 package Thread::Cleanup::TestThreads;
2
3 use strict;
4 use warnings;
5
6 use Config qw<%Config>;
7
8 use VPIT::TestHelpers;
9
10 sub diag {
11  require Test::More;
12  Test::More::diag($_) for @_;
13 }
14
15 sub import {
16  shift;
17
18  my $force = $ENV{PERL_THREAD_CLEANUP_TEST_THREADS} ? 1 : !1;
19  skip_all 'This perl wasn\'t built to support threads'
20                                                     unless $Config{useithreads};
21  skip_all 'perl 5.13.4 required to test thread safety'
22                                              unless $force or "$]" >= 5.013_004;
23
24  load_or_skip_all('threads',         $force ? '0' : '1.67', [ ]);
25  load_or_skip_all('threads::shared', $force ? '0' : '1.14', [ ]);
26
27  my %exports = (
28   spawn => \&spawn,
29  );
30
31  my $pkg = caller;
32  while (my ($name, $code) = each %exports) {
33   no strict 'refs';
34   *{$pkg.'::'.$name} = $code;
35  }
36 }
37
38 sub spawn {
39  local $@;
40  my @diag;
41  my $thread = eval {
42   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
43   threads->create(@_);
44  };
45  push @diag, "Thread creation error: $@" if $@;
46  diag(@diag) if @diag;
47  return $thread ? $thread : ();
48 }
49
50 1;