ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/AIO.pm
Revision: 1.20
Committed: Sat Sep 27 12:08:02 2008 UTC (15 years, 8 months ago) by root
Branch: MAIN
Changes since 1.19: +2 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro::AIO - truly asynchronous file and directory I/O
4
5 =head1 SYNOPSIS
6
7 use Coro::AIO;
8
9 # can now use any of the aio requests your IO::AIO module supports.
10
11 # read 1MB of /etc/passwd, without blocking other coroutines
12 my $fh = aio_open "/etc/passwd", O_RDONLY, 0
13 or die "/etc/passwd: $!";
14 aio_read $fh, 0, 1_000_000, my $buf, 0
15 or die "aio_read: $!";
16 aio_close $fh;
17
18 =head1 DESCRIPTION
19
20 This module is an L<AnyEvent> user, you need to make sure that you use and
21 run a supported event loop.
22
23 This module implements a thin wrapper around L<IO::AIO>. All of
24 the functions that expect a callback are being wrapped by this module.
25
26 The API is exactly the same as that of the corresponding IO::AIO routines,
27 except that you have to specify I<all> arguments I<except> the callback
28 argument. Instead the routines return the values normally passed to the
29 callback. Everything else, including C<$!> and perls stat cache, are set
30 as expected after these functions return.
31
32 You can mix calls to C<IO::AIO> functions with calls to this module. You
33 I<must not>, however, call these routines from within IO::AIO callbacks,
34 as this causes a deadlock. Start a coro inside the callback instead.
35
36 This module also loads L<AnyEvent::AIO> to integrate into the event loop
37 in use, so please refer to its (and L<AnyEvent>'s) documentation on how it
38 selects an appropriate event module.
39
40 All other functions exported by default by IO::AIO (e.g. C<aioreq_pri>)
41 will be exported by default by Coro::AIO, too.
42
43 Functions that can be optionally imported from IO::AIO can be imported
44 from Coro::AIO or can be called directly, e.g. C<Coro::AIO::nreqs>.
45
46 You cannot specify priorities with C<aioreq_pri>, as this module
47 overwrites the request priority with the current coroutine priority at all
48 times.
49
50 For your convienience, here are the changed function signatures for most
51 of the requests, for documentation of these functions please have a look
52 at L<IO::AIO|the IO::AIO manual>. Note that requests added by newer
53 versions of L<IO::AIO> will be automatically wrapped as well.
54
55 =over 4
56
57 =cut
58
59 package Coro::AIO;
60
61 use strict qw(subs vars);
62
63 use Coro ();
64 use AnyEvent::AIO ();
65
66 use base Exporter::;
67
68 our $VERSION = 4.748;
69
70 our @EXPORT = @IO::AIO::EXPORT;
71 our @EXPORT_OK = @IO::AIO::EXPORT_OK;
72 our $AUTOLOAD;
73
74 {
75 my @reqs = @IO::AIO::AIO_REQ ? @IO::AIO::AIO_REQ : @EXPORT;
76 my %reqs = map +($_ => 1), @reqs;
77
78 eval
79 join "",
80 map "sub $_(" . (prototype "IO::AIO::$_") . ");",
81 grep !$reqs{$_},
82 @EXPORT, @EXPORT_OK;
83
84 for my $sub (@reqs) {
85 push @EXPORT, $sub;
86
87 my $iosub = "IO::AIO::$sub";
88 my $proto = prototype $iosub;
89
90 $proto =~ s/;?\$$// or die "$iosub: unable to remove callback slot from prototype";
91
92 eval qq{
93 #line 1 "Coro::AIO::$sub($proto)"
94 sub $sub($proto) {
95 my \$current = \$Coro::current;
96 my \$state;
97 my \@res;
98
99 push \@_, sub {
100 \$state = _get_state;
101 \@res = \@_;
102 \$current->ready;
103 };
104
105 aioreq_pri \$Coro::current->prio;
106 &$iosub;
107
108 &Coro::schedule;
109 &Coro::schedule while !\$state;
110
111 _set_state \$state;
112 wantarray ? \@res : \$res[0]
113 }
114 };
115 die if $@;
116 }
117 }
118
119 sub AUTOLOAD {
120 (my $func = $AUTOLOAD) =~ s/^.*:://;
121 *$AUTOLOAD = \&{"IO::AIO::$func"};
122 goto &$AUTOLOAD;
123 }
124
125 =item $fh = aio_open $pathname, $flags, $mode
126
127 =item $status = aio_close $fh
128
129 =item $retval = aio_read $fh,$offset,$length, $data,$dataoffset
130
131 =item $retval = aio_write $fh,$offset,$length, $data,$dataoffset
132
133 =item $retval = aio_sendfile $out_fh, $in_fh, $in_offset, $length
134
135 =item $retval = aio_readahead $fh,$offset,$length
136
137 =item $status = aio_stat $fh_or_path
138
139 =item $status = aio_lstat $fh
140
141 =item $status = aio_unlink $pathname
142
143 =item $status = aio_rmdir $pathname
144
145 =item $entries = aio_readdir $pathname
146
147 =item ($dirs, $nondirs) = aio_scandir $path, $maxreq
148
149 =item $status = aio_fsync $fh
150
151 =item $status = aio_fdatasync $fh
152
153 =item ... = aio_xxx ...
154
155 Any additional aio requests follow the same scheme: same parameters except
156 you must not specify a callback but instead get the callback arguments as
157 return values.
158
159 =back
160
161 =head1 SEE ALSO
162
163 L<Coro::Socket> and L<Coro::Handle> for non-blocking socket operation.
164
165 =head1 AUTHOR
166
167 Marc Lehmann <schmorp@schmorp.de>
168 http://home.schmorp.de/
169
170 =cut
171
172 1