| 1 | package Test2::IPC; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | |||
| 5 | our $VERSION = '1.302073'; | ||
| 6 | |||
| 7 | |||
| 8 | use Test2::API::Instance; | ||
| 9 | use Test2::Util qw/get_tid/; | ||
| 10 | use Test2::API qw{ | ||
| 11 | test2_init_done | ||
| 12 | test2_ipc | ||
| 13 | test2_ipc_enable_polling | ||
| 14 | test2_pid | ||
| 15 | test2_stack | ||
| 16 | test2_tid | ||
| 17 | context | ||
| 18 | }; | ||
| 19 | |||
| 20 | use Carp qw/confess/; | ||
| 21 | |||
| 22 | our @EXPORT_OK = qw/cull/; | ||
| 23 | BEGIN { require Exporter; our @ISA = qw(Exporter) } | ||
| 24 | |||
| 25 | sub import { | ||
| 26 | goto &Exporter::import unless test2_init_done(); | ||
| 27 | |||
| 28 | confess "Cannot add IPC in a child process (" . test2_pid() . " vs $$)" if test2_pid() != $$; | ||
| 29 | confess "Cannot add IPC in a child thread (" . test2_tid() . " vs " . get_tid() . ")" if test2_tid() != get_tid(); | ||
| 30 | |||
| 31 | Test2::API::_set_ipc(_make_ipc()); | ||
| 32 | apply_ipc(test2_stack()); | ||
| 33 | |||
| 34 | goto &Exporter::import; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub _make_ipc { | ||
| 38 | # Find a driver | ||
| 39 | my ($driver) = Test2::API::test2_ipc_drivers(); | ||
| 40 | unless ($driver) { | ||
| 41 | require Test2::IPC::Driver::Files; | ||
| 42 | $driver = 'Test2::IPC::Driver::Files'; | ||
| 43 | } | ||
| 44 | |||
| 45 | return $driver->new(); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub apply_ipc { | ||
| 49 | my $stack = shift; | ||
| 50 | |||
| 51 | my ($root) = @$stack; | ||
| 52 | |||
| 53 | return unless $root; | ||
| 54 | |||
| 55 | confess "Cannot add IPC in a child process" if $root->pid != $$; | ||
| 56 | confess "Cannot add IPC in a child thread" if $root->tid != get_tid(); | ||
| 57 | |||
| 58 | my $ipc = $root->ipc || test2_ipc() || _make_ipc(); | ||
| 59 | |||
| 60 | # Add the IPC to all hubs | ||
| 61 | for my $hub (@$stack) { | ||
| 62 | my $has = $hub->ipc; | ||
| 63 | confess "IPC Mismatch!" if $has && $has != $ipc; | ||
| 64 | next if $has; | ||
| 65 | $hub->set_ipc($ipc); | ||
| 66 | $ipc->add_hub($hub->hid); | ||
| 67 | } | ||
| 68 | |||
| 69 | test2_ipc_enable_polling(); | ||
| 70 | |||
| 71 | return $ipc; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub cull { | ||
| 75 | my $ctx = context(); | ||
| 76 | $ctx->hub->cull; | ||
| 77 | $ctx->release; | ||
| 78 | } | ||
| 79 | |||
| 80 | 1; | ||
| 81 | |||
| 82 | __END__ |