]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - t/lib/Lexical/Types/TestThreads.pm
83242bcd39e0dec5cdf863d0ddf9cb86f0188692
[perl/modules/Lexical-Types.git] / t / lib / Lexical / Types / TestThreads.pm
1 package Lexical::Types::TestThreads;
2
3 use strict;
4 use warnings;
5
6 use Config qw<%Config>;
7
8 use VPIT::TestHelpers;
9
10 sub import {
11  shift;
12
13  require Lexical::Types;
14
15  skip_all 'This Lexical::Types isn\'t thread safe'
16                                          unless Lexical::Types::LT_THREADSAFE();
17
18  my $force = $ENV{PERL_LEXICAL_TYPES_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.013004;
23
24  load_or_skip('threads', $force ? '0' : '1.67', [ ],
25               'required to test thread safety');
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  if (@diag) {
47   require Test::Leaner;
48   Test::Leaner::diag($_) for @diag;
49  }
50  return $thread ? $thread : ();
51 }
52
53 1;