ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro/Select.pm
Revision: 1.99
Committed: Mon Mar 16 11:12:52 2020 UTC (4 years, 2 months ago) by root
Branch: MAIN
CVS Tags: rel-6_57, HEAD
Changes since 1.98: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 =head1 NAME
2    
3     Coro::Select - a (slow but coro-aware) replacement for CORE::select
4    
5     =head1 SYNOPSIS
6    
7 root 1.32 use Coro::Select; # replace select globally (be careful, see below)
8 root 1.1 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 root 1.36 threads can run in parallel to any select user. As many libraries that
16 root 1.2 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 root 1.36 non-blocking w.r.t. other threads.
19 root 1.1
20 root 1.40 This implementation works fastest when only very few bits are set in the
21     fd set(s).
22    
23 root 1.1 To be effective globally, this module must be C<use>'d before any other
24     module that uses C<select>, so it should generally be the first module
25 root 1.32 C<use>'d in the main program. Note that overriding C<select> globally
26     might actually cause problems, as some C<AnyEvent> backends use C<select>
27     themselves, and asking AnyEvent to use Coro::Select, which in turn asks
28     AnyEvent will not quite work.
29 root 1.1
30     You can also invoke it from the commandline as C<perl -MCoro::Select>.
31    
32 root 1.32 To override select only for a single module (e.g. C<Net::DBus::Reactor>),
33     use a code fragment like this to load it:
34    
35     {
36     package Net::DBus::Reactor;
37     use Coro::Select qw(select);
38     use Net::DBus::Reactor;
39     }
40    
41 root 1.40 Some modules (notably L<POE::Loop::Select>) directly call
42     C<CORE::select>. For these modules, we need to patch the opcode table by
43     sandwiching it between calls to C<Coro::Select::patch_pp_sselect> and
44     C<Coro::Select::unpatch_pp_sselect>:
45    
46     BEGIN {
47     use Coro::Select ();
48     Coro::Select::patch_pp_sselect;
49     require evil_poe_module_using_CORE::SELECT;
50     Coro::Select::unpatch_pp_sselect;
51     }
52 root 1.2
53 root 1.1 =over 4
54    
55     =cut
56    
57     package Coro::Select;
58    
59 root 1.45 use common::sense;
60 root 1.1
61 root 1.31 use Errno;
62    
63 root 1.25 use Coro ();
64 root 1.40 use Coro::State ();
65 root 1.39 use AnyEvent 4.800001 ();
66 root 1.25 use Coro::AnyEvent ();
67 root 1.1
68     use base Exporter::;
69    
70 root 1.99 our $VERSION = 6.57;
71 root 1.1 our @EXPORT_OK = "select";
72    
73     sub import {
74     my $pkg = shift;
75     if (@_) {
76     $pkg->export (scalar caller 0, @_);
77     } else {
78     $pkg->export ("CORE::GLOBAL", "select");
79     }
80     }
81    
82 root 1.96 sub select(;*$$$) {
83 root 1.1 if (@_ == 0) {
84     return CORE::select
85     } elsif (@_ == 1) {
86     return CORE::select $_[0]
87     } elsif (defined $_[3] && !$_[3]) {
88     return CORE::select $_[0], $_[1], $_[2], $_[3]
89     } else {
90     my $nfound = 0;
91     my @w;
92 root 1.25 my $wakeup = Coro::rouse_cb;
93    
94 root 1.1 # AnyEvent does not do 'e', so replace it by 'r'
95 root 1.44 for ([0, 0], [1, 1], [2, 0]) {
96 root 1.39 my ($i, $poll) = @$_;
97 root 1.31 if (defined $_[$i]) {
98 root 1.1 my $rvec = \$_[$i];
99 root 1.31
100     # we parse the bitmask by first expanding it into
101     # a string of bits
102     for (unpack "b*", $$rvec) {
103     # and then repeatedly matching a regex against it
104     while (/1/g) {
105     my $fd = (pos) - 1;
106    
107 root 1.1 push @w,
108 root 1.44 AE::io $fd, $poll, sub {
109 root 1.31 (vec $$rvec, $fd, 1) = 1;
110     ++$nfound;
111 root 1.25 $wakeup->();
112 root 1.44 };
113 root 1.1 }
114     }
115 root 1.39
116     $$rvec ^= $$rvec; # clear all bits
117 root 1.1 }
118     }
119    
120     push @w,
121 root 1.44 AE::timer $_[3], 0, $wakeup
122 root 1.25 if defined $_[3];
123    
124     Coro::rouse_wait;
125 root 1.1
126     return $nfound
127     }
128     }
129    
130     1;
131    
132     =back
133    
134 root 1.39 =head1 BUGS
135    
136     For performance reasons, Coro::Select's select function might not
137     properly detect bad file descriptors (but relying on EBADF is inherently
138     non-portable).
139    
140 root 1.2 =head1 SEE ALSO
141    
142     L<Coro::LWP>.
143    
144 root 1.79 =head1 AUTHOR/SUPPORT/CONTACT
145 root 1.1
146 root 1.79 Marc A. Lehmann <schmorp@schmorp.de>
147     http://software.schmorp.de/pkg/Coro.html
148 root 1.1
149     =cut
150    
151