5 our $VERSION = '1.302071';
8 use Config qw/%Config/;
24 BEGIN { require Exporter; our @ISA = qw(Exporter) }
27 *IS_WIN32 = ($^O eq 'MSWin32') ? sub() { 1 } : sub() { 0 };
31 return 0 unless $] >= 5.008001;
32 return 0 unless $Config{'useithreads'};
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);
40 # Change to a version check if this ever changes
41 return 0 if $INC{'Devel/Cover.pm'};
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/;
56 *CAN_THREAD = _can_thread() ? sub() { 1 } : sub() { 0 };
62 $can_fork = !!_can_fork();
63 no warnings 'redefine';
64 *CAN_FORK = $can_fork ? sub() { 1 } : sub() { 0 };
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 };
77 sub _manual_try(&;@) {
82 my $die = delete $SIG{__DIE__};
84 eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n";
86 $die ? $SIG{__DIE__} = $die : delete $SIG{__DIE__};
88 return (!defined($err), $err);
98 eval { $code->(@$args); 1 } or $err = $@ || "Error was squashed!\n";
100 return (!defined($err), $err);
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.
107 if (IS_WIN32 && $] < 5.020002) {
108 *try = \&_manual_try;
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() };
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 };
130 # No threads, not now, not ever!
131 *USE_THREADS = sub() { 0 };
132 *get_tid = sub() { 0 };
139 $file =~ s{(::|')}{/}g;
144 sub ipc_separator() { "~" }
156 Test2::Util - Tools used by Test2 and friends.
160 Collection of tools used by L<Test2> and friends.
164 All exports are optional. You must specify subs to import.
168 =item ($success, $error) = try { ... }
170 Eval the codeblock, return success or failure, and the error message. This code
171 protects $@ and $!, they will be restored by the end of the run. This code also
172 temporarily blocks $SIG{DIE} handlers.
174 =item protect { ... }
176 Similar to try, except that it does not catch exceptions. The idea here is to
177 protect $@ and $! from changes. $@ and $! will be restored to whatever they
178 were before the run so long as it is successful. If the run fails $! will still
179 be restored, but $@ will contain the exception being thrown.
183 True if this system is capable of true or pseudo-fork.
185 =item CAN_REALLY_FORK
187 True if the system can really fork. This will be false for systems where fork
192 True if this system is capable of using threads.
196 Returns true if threads are enabled, false if they are not.
200 This will return the id of the current thread when threads are enabled,
201 otherwise it returns 0.
203 =item my $file = pkg_to_file($package)
205 Convert a package name to a filename.
209 =head1 NOTES && CAVEATS
215 Perl 5.10.0 has a bug when compiled with newer gcc versions. This bug causes a
216 segfault whenever a new thread is launched. Test2 will attempt to detect
217 this, and note that the system is not capable of forking when it is detected.
221 Devel::Cover does not support threads. CAN_THREAD will return false if
222 Devel::Cover is loaded before the check is first run.
228 The source code repository for Test2 can be found at
229 F<http://github.com/Test-More/test-more/>.
235 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
243 =item Chad Granum E<lt>exodist@cpan.orgE<gt>
245 =item Kent Fredric E<lt>kentnl@cpan.orgE<gt>
251 Copyright 2016 Chad Granum E<lt>exodist@cpan.orgE<gt>.
253 This program is free software; you can redistribute it and/or
254 modify it under the same terms as Perl itself.
256 See F<http://dev.perl.org/licenses/>