ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Select.pm
Revision: 1.28
Committed: Mon Nov 24 07:55:28 2008 UTC (15 years, 6 months ago) by root
Branch: MAIN
CVS Tags: rel-5_1, rel-5_11
Changes since 1.27: +1 -2 lines
Log Message:
5.1

File Contents

# Content
1 =head1 NAME
2
3 Coro::Select - a (slow but coro-aware) replacement for CORE::select
4
5 =head1 SYNOPSIS
6
7 use Coro::Select; # replace select globally
8 use Core::Select 'select'; # only in this module
9 use Coro::Select (); # use Coro::Select::select
10
11 =head1 DESCRIPTION
12
13 This module tries to create a fully working replacement for perl's
14 C<select> built-in, using C<AnyEvent> watchers to do the job, so other
15 coroutines can run in parallel to any select user. As many libraries that
16 only have a blocking API do not use global variables and often use select
17 (or IO::Select), this effectively makes most such libraries "somewhat"
18 non-blocking w.r.t. other coroutines.
19
20 To be effective globally, this module must be C<use>'d before any other
21 module that uses C<select>, so it should generally be the first module
22 C<use>'d in the main program.
23
24 You can also invoke it from the commandline as C<perl -MCoro::Select>.
25
26 Performance naturally isn't great, but unless you need very high select
27 performance you normally won't notice the difference.
28
29 =over 4
30
31 =cut
32
33 package Coro::Select;
34
35 use strict;
36
37 use Coro ();
38 use AnyEvent ();
39 use Coro::AnyEvent ();
40
41 use base Exporter::;
42
43 our $VERSION = 5.1;
44 our @EXPORT_OK = "select";
45
46 sub import {
47 my $pkg = shift;
48 if (@_) {
49 $pkg->export (scalar caller 0, @_);
50 } else {
51 $pkg->export ("CORE::GLOBAL", "select");
52 }
53 }
54
55 sub select(;*$$$) { # not the correct prototype, but well... :()
56 if (@_ == 0) {
57 return CORE::select
58 } elsif (@_ == 1) {
59 return CORE::select $_[0]
60 } elsif (defined $_[3] && !$_[3]) {
61 return CORE::select $_[0], $_[1], $_[2], $_[3]
62 } else {
63 my $nfound = 0;
64 my @w;
65 my $wakeup = Coro::rouse_cb;
66
67 # AnyEvent does not do 'e', so replace it by 'r'
68 for ([0, 'r', '<'], [1, 'w', '>'], [2, 'r', '<']) {
69 my ($i, $poll, $mode) = @$_;
70 if (defined (my $vec = $_[$i])) {
71 my $rvec = \$_[$i];
72 for my $b (0 .. (8 * length $vec)) {
73 if (vec $vec, $b, 1) {
74 (vec $$rvec, $b, 1) = 0;
75 open my $fh, "$mode&=$b"
76 or die "Coro::Select::fd2fh($b): $!";
77 push @w,
78 AnyEvent->io (fh => $fh, poll => $poll, cb => sub {
79 (vec $$rvec, $b, 1) = 1;
80 $nfound++;
81 $wakeup->();
82 });
83 }
84 }
85 }
86 }
87
88 push @w,
89 AnyEvent->timer (after => $_[3], cb => $wakeup)
90 if defined $_[3];
91
92 Coro::rouse_wait;
93
94 return $nfound
95 }
96 }
97
98 1;
99
100 =back
101
102 =head1 SEE ALSO
103
104 L<Coro::LWP>.
105
106 =head1 AUTHOR
107
108 Marc Lehmann <schmorp@schmorp.de>
109 http://home.schmorp.de/
110
111 =cut
112
113