| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Test2::IPC; | 
| 2 | 26 |  |  | 26 |  | 12098 | use strict; | 
|  | 26 |  |  |  |  | 135 |  | 
|  | 26 |  |  |  |  | 739 |  | 
| 3 | 26 |  |  | 26 |  | 196 | use warnings; | 
|  | 26 |  |  |  |  | 44 |  | 
|  | 26 |  |  |  |  | 1068 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | our $VERSION = '1.302181'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 26 |  |  | 26 |  | 13499 | use Test2::API::Instance; | 
|  | 26 |  |  |  |  | 68 |  | 
|  | 26 |  |  |  |  | 140 |  | 
| 9 | 26 |  |  | 26 |  | 172 | use Test2::Util qw/get_tid/; | 
|  | 26 |  |  |  |  | 48 |  | 
|  | 26 |  |  |  |  | 1421 |  | 
| 10 | 26 |  |  |  |  | 2874 | use Test2::API qw{ | 
| 11 |  |  |  |  |  |  | test2_in_preload | 
| 12 |  |  |  |  |  |  | test2_init_done | 
| 13 |  |  |  |  |  |  | test2_ipc | 
| 14 |  |  |  |  |  |  | test2_has_ipc | 
| 15 |  |  |  |  |  |  | test2_ipc_enable_polling | 
| 16 |  |  |  |  |  |  | test2_pid | 
| 17 |  |  |  |  |  |  | test2_stack | 
| 18 |  |  |  |  |  |  | test2_tid | 
| 19 |  |  |  |  |  |  | context | 
| 20 | 26 |  |  | 26 |  | 11887 | }; | 
|  | 26 |  |  |  |  | 64 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # Make sure stuff is finalized before anyone tried to fork or start a new thread. | 
| 23 |  |  |  |  |  |  | { | 
| 24 |  |  |  |  |  |  | # Avoid warnings if things are loaded at run-time | 
| 25 | 26 |  |  | 26 |  | 191 | no warnings 'void'; | 
|  | 26 |  |  |  |  | 62 |  | 
|  | 26 |  |  |  |  | 1132 |  | 
| 26 |  |  |  |  |  |  | INIT { | 
| 27 | 26 |  |  | 26 |  | 151 | use warnings 'void'; | 
|  | 26 |  |  |  |  | 58 |  | 
|  | 26 |  |  |  |  | 2145 |  | 
| 28 | 26 | 50 |  | 26 |  | 151 | context()->release() unless test2_in_preload(); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | } | 
| 31 |  |  |  |  |  |  |  | 
| 32 | 26 |  |  | 26 |  | 176 | use Carp qw/confess/; | 
|  | 26 |  |  |  |  | 47 |  | 
|  | 26 |  |  |  |  | 1845 |  | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | our @EXPORT_OK = qw/cull/; | 
| 35 | 26 |  |  | 26 |  | 153 | BEGIN { require Exporter; our @ISA = qw(Exporter) } | 
|  | 26 |  |  |  |  | 11831 |  | 
| 36 |  |  |  |  |  |  |  | 
| 37 | 1 |  |  | 1 |  | 11 | sub unimport { Test2::API::test2_ipc_disable() } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub import { | 
| 40 | 26 | 50 | 66 | 26 |  | 314 | goto &Exporter::import if test2_has_ipc || !test2_init_done(); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 0 | 0 |  |  |  |  | confess "IPC is disabled" if Test2::API::test2_ipc_disabled(); | 
| 43 | 0 | 0 |  |  |  |  | confess "Cannot add IPC in a child process (" . test2_pid() . " vs $$)" if test2_pid() != $$; | 
| 44 | 0 | 0 |  |  |  |  | confess "Cannot add IPC in a child thread (" . test2_tid() . " vs " . get_tid() . ")"  if test2_tid() != get_tid(); | 
| 45 |  |  |  |  |  |  |  | 
| 46 | 0 |  |  |  |  |  | Test2::API::_set_ipc(_make_ipc()); | 
| 47 | 0 |  |  |  |  |  | apply_ipc(test2_stack()); | 
| 48 |  |  |  |  |  |  |  | 
| 49 | 0 |  |  |  |  |  | goto &Exporter::import; | 
| 50 |  |  |  |  |  |  | } | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | sub _make_ipc { | 
| 53 |  |  |  |  |  |  | # Find a driver | 
| 54 | 0 |  |  | 0 |  |  | my ($driver) = Test2::API::test2_ipc_drivers(); | 
| 55 | 0 | 0 |  |  |  |  | unless ($driver) { | 
| 56 | 0 |  |  |  |  |  | require Test2::IPC::Driver::Files; | 
| 57 | 0 |  |  |  |  |  | $driver = 'Test2::IPC::Driver::Files'; | 
| 58 |  |  |  |  |  |  | } | 
| 59 |  |  |  |  |  |  |  | 
| 60 | 0 |  |  |  |  |  | return $driver->new(); | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub apply_ipc { | 
| 64 | 0 |  |  | 0 | 0 |  | my $stack = shift; | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 0 |  |  |  |  |  | my ($root) = @$stack; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 | 0 |  |  |  |  | return unless $root; | 
| 69 |  |  |  |  |  |  |  | 
| 70 | 0 | 0 |  |  |  |  | confess "Cannot add IPC in a child process" if $root->pid != $$; | 
| 71 | 0 | 0 |  |  |  |  | confess "Cannot add IPC in a child thread"  if $root->tid != get_tid(); | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 0 |  | 0 |  |  |  | my $ipc = $root->ipc || test2_ipc() || _make_ipc(); | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | # Add the IPC to all hubs | 
| 76 | 0 |  |  |  |  |  | for my $hub (@$stack) { | 
| 77 | 0 |  |  |  |  |  | my $has = $hub->ipc; | 
| 78 | 0 | 0 | 0 |  |  |  | confess "IPC Mismatch!" if $has && $has != $ipc; | 
| 79 | 0 | 0 |  |  |  |  | next if $has; | 
| 80 | 0 |  |  |  |  |  | $hub->set_ipc($ipc); | 
| 81 | 0 |  |  |  |  |  | $ipc->add_hub($hub->hid); | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 | 0 |  |  |  |  |  | test2_ipc_enable_polling(); | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 0 |  |  |  |  |  | return $ipc; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | sub cull { | 
| 90 | 0 |  |  | 0 | 1 |  | my $ctx = context(); | 
| 91 | 0 |  |  |  |  |  | $ctx->hub->cull; | 
| 92 | 0 |  |  |  |  |  | $ctx->release; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | 1; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | __END__ | 
| 98 |  |  |  |  |  |  |  | 
| 99 |  |  |  |  |  |  | =pod | 
| 100 |  |  |  |  |  |  |  | 
| 101 |  |  |  |  |  |  | =encoding UTF-8 | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | =head1 NAME | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | Test2::IPC - Turn on IPC for threading or forking support. | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | You should C<use Test2::IPC;> as early as possible in your test file. If you | 
| 110 |  |  |  |  |  |  | import this module after API initialization it will attempt to retrofit IPC | 
| 111 |  |  |  |  |  |  | onto the existing hubs. | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | =head2 DISABLING IT | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | You can use C<no Test2::IPC;> to disable IPC for good. You can also use the | 
| 116 |  |  |  |  |  |  | T2_NO_IPC env var. | 
| 117 |  |  |  |  |  |  |  | 
| 118 |  |  |  |  |  |  | =head1 EXPORTS | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | All exports are optional. | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =over 4 | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =item cull() | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | Cull allows you to collect results from other processes or threads on demand. | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =back | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | =head1 SOURCE | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | The source code repository for Test2 can be found at | 
| 133 |  |  |  |  |  |  | F<http://github.com/Test-More/test-more/>. | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | =head1 MAINTAINERS | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | =over 4 | 
| 138 |  |  |  |  |  |  |  | 
| 139 |  |  |  |  |  |  | =item Chad Granum E<lt>exodist@cpan.orgE<gt> | 
| 140 |  |  |  |  |  |  |  | 
| 141 |  |  |  |  |  |  | =back | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =head1 AUTHORS | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | =over 4 | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | =item Chad Granum E<lt>exodist@cpan.orgE<gt> | 
| 148 |  |  |  |  |  |  |  | 
| 149 |  |  |  |  |  |  | =back | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | =head1 COPYRIGHT | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  | Copyright 2020 Chad Granum E<lt>exodist@cpan.orgE<gt>. | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or | 
| 156 |  |  |  |  |  |  | modify it under the same terms as Perl itself. | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | See F<http://dev.perl.org/licenses/> | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =cut |