… | |
… | |
33 | You also can, but do not need to, call C<IO::AIO::poll_cb>, as this |
33 | You also can, but do not need to, call C<IO::AIO::poll_cb>, as this |
34 | module automatically installs an event watcher for the C<IO::AIO> file |
34 | module automatically installs an event watcher for the C<IO::AIO> file |
35 | descriptor. It uses the L<AnyEvent|AnyEvent> module for this, so please |
35 | descriptor. It uses the L<AnyEvent|AnyEvent> module for this, so please |
36 | refer to its documentation on how it selects an appropriate Event module. |
36 | refer to its documentation on how it selects an appropriate Event module. |
37 | |
37 | |
|
|
38 | All other functions exported by default by IO::AIO (e.g. C<aioreq_pri>) |
|
|
39 | will be exported by default by Coro::AIO, too. |
|
|
40 | |
|
|
41 | Functions that can be optionally imported from IO::AIO can be imported |
|
|
42 | from Coro::AIO or can be called directly, e.g. C<Coro::AIO::nreqs>. |
|
|
43 | |
38 | For your convienience, here are the changed function signatures, for |
44 | For your convienience, here are the changed function signatures for most |
39 | documentation of these functions please have a look at L<IO::AIO|the |
45 | of the requests, for documentation of these functions please have a look |
40 | IO::AIO manual>. |
46 | at L<IO::AIO|the IO::AIO manual>. |
41 | |
47 | |
42 | =over 4 |
48 | =over 4 |
43 | |
49 | |
44 | =cut |
50 | =cut |
45 | |
51 | |
46 | package Coro::AIO; |
52 | package Coro::AIO; |
47 | |
53 | |
48 | use strict; |
54 | use strict 'subs'; |
49 | |
55 | |
50 | use Coro (); |
56 | use Coro (); |
51 | use AnyEvent; |
57 | use AnyEvent; |
52 | use IO::AIO (); |
58 | use IO::AIO (); |
53 | |
59 | |
54 | use base Exporter::; |
60 | use base Exporter::; |
55 | |
61 | |
56 | our $FH; open $FH, "<&=" . IO::AIO::poll_fileno; |
62 | our $FH; open $FH, "<&=" . IO::AIO::poll_fileno; |
57 | our $WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => sub { IO::AIO::poll_cb }); |
63 | our $WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => sub { IO::AIO::poll_cb }); |
58 | |
64 | |
59 | our @EXPORT; |
65 | our @EXPORT = @IO::AIO::EXPORT; |
|
|
66 | our @EXPORT_OK = @IO::AIO::EXPORT_OK; |
|
|
67 | our $AUTOLOAD; |
60 | |
68 | |
61 | sub wrap($) { |
69 | { |
62 | my ($sub) = @_; |
70 | my @reqs = @IO::AIO::AIO_REQ ? @IO::AIO::AIO_REQ : @EXPORT; |
|
|
71 | my %reqs = map +($_ => 1), @reqs; |
63 | |
72 | |
64 | no strict 'refs'; |
73 | eval |
65 | |
74 | join "", |
66 | push @EXPORT, $sub; |
75 | map "sub $_(" . (prototype "IO::AIO::$_") . ");", |
67 | |
76 | grep !$reqs{$_}, |
68 | my $iosub = "IO::AIO::$sub"; |
77 | @EXPORT, @EXPORT_OK; |
69 | my $proto = prototype $iosub; |
|
|
70 | |
78 | |
71 | $proto =~ s/;?\$$// or die "$iosub: unable to remove callback slot from prototype"; |
79 | for my $sub (@reqs) { |
|
|
80 | push @EXPORT, $sub; |
|
|
81 | |
|
|
82 | my $iosub = "IO::AIO::$sub"; |
|
|
83 | my $proto = prototype $iosub; |
72 | |
84 | |
|
|
85 | $proto =~ s/;?\$$// or die "$iosub: unable to remove callback slot from prototype"; |
|
|
86 | |
73 | eval qq{ |
87 | eval qq{ |
74 | #line 1 "Coro::AIO::$sub($proto)" |
88 | #line 1 "Coro::AIO::$sub($proto)" |
75 | sub $sub($proto) { |
89 | sub $sub($proto) { |
76 | my \$current = \$Coro::current; |
90 | my \$current = \$Coro::current; |
77 | my \$stat; |
91 | my \$stat; |
78 | my \@res; |
92 | my \@res; |
79 | |
93 | |
80 | push \@_, sub { |
94 | push \@_, sub { |
81 | \$stat = Coro::_aio_get_state; |
95 | \$stat = Coro::_aio_get_state; |
82 | \@res = \@_; |
96 | \@res = \@_; |
83 | \$current->ready; |
97 | \$current->ready; |
84 | undef \$current; |
98 | undef \$current; |
85 | }; |
99 | }; |
86 | |
100 | |
87 | &$iosub; |
101 | &$iosub; |
88 | |
102 | |
89 | Coro::schedule while \$current; |
103 | Coro::schedule while \$current; |
90 | |
104 | |
91 | Coro::_aio_set_state \$stat; |
105 | Coro::_aio_set_state \$stat; |
92 | wantarray ? \@res : \$res[0] |
106 | wantarray ? \@res : \$res[0] |
|
|
107 | } |
93 | } |
108 | }; |
94 | }; |
|
|
95 | die if $@; |
109 | die if $@; |
|
|
110 | } |
96 | } |
111 | } |
97 | |
112 | |
98 | wrap $_ for @IO::AIO::AIO_REQ |
113 | sub AUTOLOAD { |
99 | ? @IO::AIO::AIO_REQ |
114 | (my $func = $AUTOLOAD) =~ s/^.*:://; |
100 | : @IO::AIO::EXPORT; |
115 | *$AUTOLOAD = \&{"IO::AIO::$func"}; |
|
|
116 | goto &$AUTOLOAD; |
|
|
117 | } |
101 | |
118 | |
102 | =item $fh = aio_open $pathname, $flags, $mode |
119 | =item $fh = aio_open $pathname, $flags, $mode |
103 | |
120 | |
104 | =item $status = aio_close $fh |
121 | =item $status = aio_close $fh |
105 | |
122 | |