| 1 | package Test2::Util; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '1.302073'; | ||
| 6 | |||
| 7 | |||
| 8 | use Config qw/%Config/; | ||
| 9 | |||
| 10 | our @EXPORT_OK = qw{ | ||
| 11 | try | ||
| 12 | |||
| 13 | pkg_to_file | ||
| 14 | |||
| 15 | get_tid USE_THREADS | ||
| 16 | CAN_THREAD | ||
| 17 | CAN_REALLY_FORK | ||
| 18 | CAN_FORK | ||
| 19 | |||
| 20 | IS_WIN32 | ||
| 21 | |||
| 22 | ipc_separator | ||
| 23 | }; | ||
| 24 | BEGIN { require Exporter; our @ISA = qw(Exporter) } | ||
| 25 | |||
| 26 | BEGIN { | ||
| 27 | *IS_WIN32 = ($^O eq 'MSWin32') ? sub() { 1 } : sub() { 0 }; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub _can_thread { | ||
| 31 | return 0 unless $] >= 5.008001; | ||
| 32 | return 0 unless $Config{'useithreads'}; | ||
| 33 | |||
| 34 | # Threads are broken on perl 5.10.0 built with gcc 4.8+ | ||
| 35 | if ($] == 5.010000 && $Config{'ccname'} eq 'gcc' && $Config{'gccversion'}) { | ||
| 36 | my @parts = split /\./, $Config{'gccversion'}; | ||
| 37 | return 0 if $parts[0] > 4 || ($parts[0] == 4 && $parts[1] >= 8); | ||
| 38 | } | ||
| 39 | |||
| 40 | # Change to a version check if this ever changes | ||
| 41 | return 0 if $INC{'Devel/Cover.pm'}; | ||
| 42 | return 1; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _can_fork { | ||
| 46 | return 1 if $Config{d_fork}; | ||
| 47 | return 0 unless IS_WIN32 || $^O eq 'NetWare'; | ||
| 48 | return 0 unless $Config{useithreads}; | ||
| 49 | return 0 unless $Config{ccflags} =~ /-DPERL_IMPLICIT_SYS/; | ||
| 50 | |||
| 51 | return _can_thread(); | ||
| 52 | } | ||
| 53 | |||
| 54 | BEGIN { | ||
| 55 | no warnings 'once'; | ||
| 56 | *CAN_THREAD = _can_thread() ? sub() { 1 } : sub() { 0 }; | ||
| 57 | } | ||
| 58 | my $can_fork; | ||
| 59 | sub CAN_FORK () { | ||
| 60 | return $can_fork | ||
| 61 | if defined $can_fork; | ||
| 62 | $can_fork = !!_can_fork(); | ||
| 63 | no warnings 'redefine'; | ||
| 64 | *CAN_FORK = $can_fork ? sub() { 1 } : sub() { 0 }; | ||
| 65 | $can_fork; | ||
| 66 | } | ||
| 67 | my $can_really_fork; | ||
| 68 | sub CAN_REALLY_FORK () { | ||
| 69 | return $can_really_fork | ||
| 70 | if defined $can_really_fork; | ||
| 71 | $can_really_fork = !!$Config{d_fork}; | ||
| 72 | no warnings 'redefine'; | ||
| 73 | *CAN_REALLY_FORK = $can_really_fork ? sub() { 1 } : sub() { 0 }; | ||
| 74 | $can_really_fork; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub _manual_try(&;@) { | ||
| 78 | my $code = shift; | ||
| 79 | my $args = \@_; | ||
| 80 | my $err; | ||
| 81 | |||
| 82 | my $die = delete $SIG{__DIE__}; | ||
| 83 | |||
| 84 | eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n"; | ||
| 85 | |||
| 86 | $die ? $SIG{__DIE__} = $die : delete $SIG{__DIE__}; | ||
| 87 | |||
| 88 | return (!defined($err), $err); | ||
| 89 | } | ||
| 90 | |||
| 91 | sub _local_try(&;@) { | ||
| 92 | my $code = shift; | ||
| 93 | my $args = \@_; | ||
| 94 | my $err; | ||
| 95 | |||
| 96 | no warnings; | ||
| 97 | local $SIG{__DIE__}; | ||
| 98 | eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n"; | ||
| 99 | |||
| 100 | return (!defined($err), $err); | ||
| 101 | } | ||
| 102 | |||
| 103 | # Older versions of perl have a nasty bug on win32 when localizing a variable | ||
| 104 | # before forking or starting a new thread. So for those systems we use the | ||
| 105 | # non-local form. When possible though we use the faster 'local' form. | ||
| 106 | BEGIN { | ||
| 107 | if (IS_WIN32 && $] < 5.020002) { | ||
| 108 | *try = \&_manual_try; | ||
| 109 | } | ||
| 110 | else { | ||
| 111 | *try = \&_local_try; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | |||
| 115 | BEGIN { | ||
| 116 | if (CAN_THREAD) { | ||
| 117 | if ($INC{'threads.pm'}) { | ||
| 118 | # Threads are already loaded, so we do not need to check if they | ||
| 119 | # are loaded each time | ||
| 120 | *USE_THREADS = sub() { 1 }; | ||
| 121 | *get_tid = sub() { threads->tid() }; | ||
| 122 | } | ||
| 123 | else { | ||
| 124 | # :-( Need to check each time to see if they have been loaded. | ||
| 125 | *USE_THREADS = sub() { $INC{'threads.pm'} ? 1 : 0 }; | ||
| 126 | *get_tid = sub() { $INC{'threads.pm'} ? threads->tid() : 0 }; | ||
| 127 | } | ||
| 128 | } | ||
| 129 | else { | ||
| 130 | # No threads, not now, not ever! | ||
| 131 | *USE_THREADS = sub() { 0 }; | ||
| 132 | *get_tid = sub() { 0 }; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | sub pkg_to_file { | ||
| 137 | my $pkg = shift; | ||
| 138 | my $file = $pkg; | ||
| 139 | $file =~ s{(::|')}{/}g; | ||
| 140 | $file .= '.pm'; | ||
| 141 | return $file; | ||
| 142 | } | ||
| 143 | |||
| 144 | sub ipc_separator() { "~" } | ||
| 145 | |||
| 146 | 1; | ||
| 147 | |||
| 148 | __END__ |