Skip to content

Commit bd55b87

Browse files
authored
Merge pull request #1023 from haarg/seperate-sig-util
move try_sig_mask to separate file to avoid always loading POSIX
2 parents e790de8 + 11754ec commit bd55b87

File tree

3 files changed

+128
-51
lines changed

3 files changed

+128
-51
lines changed

lib/Test2/IPC/Driver/Files.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use Storable();
1414
use File::Spec();
1515
use POSIX();
1616

17-
use Test2::Util qw/try get_tid pkg_to_file IS_WIN32 ipc_separator do_rename do_unlink try_sig_mask/;
17+
use Test2::Util qw/try get_tid pkg_to_file IS_WIN32 ipc_separator do_rename do_unlink/;
18+
use Test2::Util::Sig qw/try_sig_mask/;
1819
use Test2::API qw/test2_ipc_set_pending/;
1920

2021
sub is_viable { 1 }

lib/Test2/Util.pm

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use warnings;
44

55
our $VERSION = '1.302209';
66

7-
use POSIX();
87
use Config qw/%Config/;
98
use Carp qw/croak/;
109

@@ -248,30 +247,10 @@ BEGIN {
248247
}
249248
}
250249

250+
#for backwards compatibility
251251
sub try_sig_mask(&) {
252-
my $code = shift;
253-
254-
my ($old, $blocked);
255-
unless(IS_WIN32) {
256-
my $to_block = POSIX::SigSet->new(
257-
POSIX::SIGINT(),
258-
POSIX::SIGALRM(),
259-
POSIX::SIGHUP(),
260-
POSIX::SIGTERM(),
261-
POSIX::SIGUSR1(),
262-
POSIX::SIGUSR2(),
263-
);
264-
$old = POSIX::SigSet->new;
265-
$blocked = POSIX::sigprocmask(POSIX::SIG_BLOCK(), $to_block, $old);
266-
# Silently go on if we failed to log signals, not much we can do.
267-
}
268-
269-
my ($ok, $err) = &try($code);
270-
271-
# If our block was successful we want to restore the old mask.
272-
POSIX::sigprocmask(POSIX::SIG_SETMASK(), $old, POSIX::SigSet->new()) if defined $blocked;
273-
274-
return ($ok, $err);
252+
require Test2::Util::Sig;
253+
goto &Test2::Util::Sig::try_sig_mask;
275254
}
276255

277256
1;
@@ -369,32 +348,6 @@ cross-platform when trying to rename files you recently altered.
369348
Unlink a file, this wraps C<unlink()> in a way that makes it more reliable
370349
cross-platform when trying to unlink files you recently altered.
371350
372-
=item ($ok, $err) = try_sig_mask { ... }
373-
374-
Complete an action with several signals masked, they will be unmasked at the
375-
end allowing any signals that were intercepted to get handled.
376-
377-
This is primarily used when you need to make several actions atomic (against
378-
some signals anyway).
379-
380-
Signals that are intercepted:
381-
382-
=over 4
383-
384-
=item SIGINT
385-
386-
=item SIGALRM
387-
388-
=item SIGHUP
389-
390-
=item SIGTERM
391-
392-
=item SIGUSR1
393-
394-
=item SIGUSR2
395-
396-
=back
397-
398351
=back
399352
400353
=head1 NOTES && CAVEATS

lib/Test2/Util/Sig.pm

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package Test2::Util::Sig;
2+
use strict;
3+
use warnings;
4+
5+
our $VERSION = '1.302205';
6+
7+
use POSIX();
8+
use Test2::Util qw/try IS_WIN32/;
9+
10+
our @EXPORT_OK = qw{
11+
try_sig_mask
12+
};
13+
BEGIN { require Exporter; our @ISA = qw(Exporter) }
14+
15+
sub try_sig_mask(&) {
16+
my $code = shift;
17+
18+
my ($old, $blocked);
19+
unless(IS_WIN32) {
20+
my $to_block = POSIX::SigSet->new(
21+
POSIX::SIGINT(),
22+
POSIX::SIGALRM(),
23+
POSIX::SIGHUP(),
24+
POSIX::SIGTERM(),
25+
POSIX::SIGUSR1(),
26+
POSIX::SIGUSR2(),
27+
);
28+
$old = POSIX::SigSet->new;
29+
$blocked = POSIX::sigprocmask(POSIX::SIG_BLOCK(), $to_block, $old);
30+
# Silently go on if we failed to log signals, not much we can do.
31+
}
32+
33+
my ($ok, $err) = &try($code);
34+
35+
# If our block was successful we want to restore the old mask.
36+
POSIX::sigprocmask(POSIX::SIG_SETMASK(), $old, POSIX::SigSet->new()) if defined $blocked;
37+
38+
return ($ok, $err);
39+
}
40+
41+
1;
42+
43+
__END__
44+
45+
=pod
46+
47+
=encoding UTF-8
48+
49+
=head1 NAME
50+
51+
Test2::Util::Sig - Signal tools used by Test2 and friends.
52+
53+
=head1 DESCRIPTION
54+
55+
Collection of signal tools used by L<Test2> and friends.
56+
57+
=head1 EXPORTS
58+
59+
All exports are optional. You must specify subs to import.
60+
61+
=over 4
62+
63+
=item ($ok, $err) = try_sig_mask { ... }
64+
65+
Complete an action with several signals masked, they will be unmasked at the
66+
end allowing any signals that were intercepted to get handled.
67+
68+
This is primarily used when you need to make several actions atomic (against
69+
some signals anyway).
70+
71+
Signals that are intercepted:
72+
73+
=over 4
74+
75+
=item SIGINT
76+
77+
=item SIGALRM
78+
79+
=item SIGHUP
80+
81+
=item SIGTERM
82+
83+
=item SIGUSR1
84+
85+
=item SIGUSR2
86+
87+
=back
88+
89+
=back
90+
91+
=head1 SOURCE
92+
93+
The source code repository for Test2 can be found at
94+
L<https://github.com/Test-More/test-more/>.
95+
96+
=head1 MAINTAINERS
97+
98+
=over 4
99+
100+
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
101+
102+
=back
103+
104+
=head1 AUTHORS
105+
106+
=over 4
107+
108+
=item Chad Granum E<lt>exodist@cpan.orgE<gt>
109+
110+
=item Kent Fredric E<lt>kentnl@cpan.orgE<gt>
111+
112+
=back
113+
114+
=head1 COPYRIGHT
115+
116+
Copyright Chad Granum E<lt>exodist@cpan.orgE<gt>.
117+
118+
This program is free software; you can redistribute it and/or
119+
modify it under the same terms as Perl itself.
120+
121+
See L<https://dev.perl.org/licenses/>
122+
123+
=cut

0 commit comments

Comments
 (0)