]> git.vpit.fr Git - perl/modules/Linux-SysInfo.git/blob - t/lib/Linux/SysInfo/TestThreads.pm
37a88c99463f56f1bd193bf38b7b0b9f91de93b8
[perl/modules/Linux-SysInfo.git] / t / lib / Linux / SysInfo / TestThreads.pm
1 package Linux::SysInfo::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_LINUX_SYSINFO_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
26  my %exports = (
27   spawn => \&spawn,
28  );
29
30  my $pkg = caller;
31  while (my ($name, $code) = each %exports) {
32   no strict 'refs';
33   *{$pkg.'::'.$name} = $code;
34  }
35 }
36
37 sub spawn {
38  local $@;
39  my @diag;
40  my $thread = eval {
41   local $SIG{__WARN__} = sub { push @diag, "Thread creation warning: @_" };
42   threads->create(@_);
43  };
44  push @diag, "Thread creation error: $@" if $@;
45  diag(@diag) if @diag;
46  return $thread ? $thread : ();
47 }
48
49 1;