ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/AIO.pm
Revision: 1.1
Committed: Mon Dec 26 18:48:11 2005 UTC (19 years, 1 month ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro::AIO - truely asynchronous file access
4
5 =head1 SYNOPSIS
6
7 use Coro::AIO;
8
9 # can now use any of:
10 # aio_sendfile aio_read aio_write aio_open aio_close
11 # aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink aio_fsync
12 # aio_fdatasync aio_readahead
13
14 # read 1MB of /etc/passwd, without blocking other coroutines
15 my $fh = aio_open "/etc/passwd", O_RDONLY, 0
16 or die "/etc/passwd: $!";
17 aio_read $fh, 0, 1_000_000, my $buf, 0
18 or die "aio_read: $!";
19 aio_close $fh;
20
21 =head1 DESCRIPTION
22
23 This module implements a thin wrapper around L<IO::AIO|IO::AIO>. All of
24 the functions (except C<aio_lstat> and C<aio_stat>) that expect a callback
25 are being wrapped by this module.
26
27 The API is exactly the same as that of the corresponding IO::AIO routines,
28 except that you have to specify I<all> arguments I<except> the callback
29 argument. Instead the routines return the values normally passed to the
30 callback. They also all have a prototype of C<@> currently, but that might
31 change.
32
33 You can mix calls to C<IO::AIO> functions with calls to this module. You
34 also can, but do not need to, call C<IO::AIO::poll_cb>, as this module
35 automatically installs an event watcher for the C<IO::AIO> file
36 descriptor. It uses the L<AnyEvent|AnyEvent> module for this, so please
37 refer to its documentation on how it selects an appropriate Event module.
38
39 For your convienience, here are the changed function signatures, for
40 documentation of these functions please have a look at L<IO::AIO|the
41 IO::AIO manual>.
42
43 =over 4
44
45 =cut
46
47 package Coro::AIO;
48
49 use Coro ();
50 use AnyEvent;
51 use IO::AIO ();
52
53 use base Exporter::;
54
55 our $FH; open $FH, "<&=" . IO::AIO::poll_fileno;
56 our $WATCHER = AnyEvent->io (fh => $FH, poll => 'r', cb => sub { IO::AIO::poll_cb });
57
58 our @EXPORT;
59
60 sub wrap($) {
61 my ($sub) = @_;
62
63 push @EXPORT, $sub;
64
65 my $iosub = "IO::AIO::$sub";
66
67 *$sub = sub {
68 my $current = $Coro::current;
69 my $errno;
70 my @res;
71
72 $iosub->(@_, sub {
73 $errno = $!;
74 @res = @_;
75 $current->ready;
76 undef $current;
77 });
78
79 Coro::schedule while $current;
80
81 $! = $errno;
82 wantarray ? @res : $res[0]
83 };
84 }
85
86 wrap $_ for qw(aio_sendfile aio_read aio_write aio_open aio_close
87 aio_unlink aio_rmdir aio_readdir aio_scandir aio_symlink
88 aio_fsync aio_fdatasync aio_readahead);
89
90 =item $fh = aio_open $pathname, $flags, $mode
91
92 =item $status = aio_close $fh
93
94 =item $retval = aio_read $fh,$offset,$length, $data,$dataoffset
95
96 =item $retval = aio_write $fh,$offset,$length, $data,$dataoffset
97
98 =item $retval = aio_sendfile $out_fh, $in_fh, $in_offset, $length
99
100 =item $retval = aio_readahead $fh,$offset,$length
101
102 =item $status = aio_unlink $pathname
103
104 =item $status = aio_rmdir $pathname
105
106 =item $entries = aio_readdir $pathname $callback->($entries)
107
108 =item ($dirs, $nondirs) = aio_scandir $path, $maxreq
109
110 =item $status = aio_fsync $fh
111
112 =item $status = aio_fdatasync $fh
113
114 =back
115
116 =head1 AUTHOR
117
118 Marc Lehmann <schmorp@schmorp.de>
119 http://home.schmorp.de/
120
121 =cut
122
123 1