… | |
… | |
26 | package Coro::Select; |
26 | package Coro::Select; |
27 | |
27 | |
28 | use base Exporter; |
28 | use base Exporter; |
29 | |
29 | |
30 | use Coro; |
30 | use Coro; |
|
|
31 | use Coro::Event; |
31 | use Event; |
32 | use Event; |
32 | |
33 | |
33 | $VERSION = 1.31; |
34 | $VERSION = 1.31; |
34 | |
35 | |
35 | BEGIN { |
36 | BEGIN { |
… | |
… | |
37 | } |
38 | } |
38 | |
39 | |
39 | sub import { |
40 | sub import { |
40 | my $pkg = shift; |
41 | my $pkg = shift; |
41 | if (@_) { |
42 | if (@_) { |
42 | $pkg->export(caller(0), @_); |
43 | $pkg->export (caller (0), @_); |
43 | } else { |
44 | } else { |
44 | $pkg->export("CORE::GLOBAL", "select"); |
45 | $pkg->export ("CORE::GLOBAL", "select"); |
45 | } |
46 | } |
46 | } |
47 | } |
47 | |
48 | |
48 | sub select(;*$$$) { # not the correct prototype, but well... :() |
49 | sub select(;*$$$) { # not the correct prototype, but well... :() |
49 | if (@_ == 0) { |
50 | if (@_ == 0) { |
50 | return CORE::select; |
51 | return CORE::select; |
51 | } elsif (@_ == 1) { |
52 | } elsif (@_ == 1) { |
52 | return CORE::select $_[0]; |
53 | return CORE::select $_[0]; |
53 | } elsif (defined $_[3] && !$_[3]) { |
54 | } elsif (defined $_[3] && !$_[3]) { |
54 | return CORE::select(@_); |
55 | return CORE::select (@_); |
55 | } else { |
56 | } else { |
56 | my $current = $Coro::current; |
57 | my $current = $Coro::current; |
57 | my $nfound = 0; |
58 | my $nfound = 0; |
58 | my @w; |
59 | my @w; |
59 | for ([0, 'r'], [1, 'w'], [2, 'e']) { |
60 | for ([0, 'r'], [1, 'w'], [2, 'e']) { |
… | |
… | |
62 | my $rvec = \$_[$i]; |
63 | my $rvec = \$_[$i]; |
63 | for my $b (0 .. (8 * length $vec)) { |
64 | for my $b (0 .. (8 * length $vec)) { |
64 | if (vec $vec, $b, 1) { |
65 | if (vec $vec, $b, 1) { |
65 | (vec $$rvec, $b, 1) = 0; |
66 | (vec $$rvec, $b, 1) = 0; |
66 | push @w, |
67 | push @w, |
67 | Event->io(fd => $b, poll => $poll, cb => sub { |
68 | Event->io (fd => $b, poll => $poll, cb => sub { |
68 | (vec $$rvec, $b, 1) = 1; |
69 | (vec $$rvec, $b, 1) = 1; |
69 | $nfound++; |
70 | $nfound++; |
70 | $current->ready; |
71 | $current->ready; |
71 | }); |
72 | }); |
72 | } |
73 | } |
73 | } |
74 | } |
74 | } |
75 | } |
75 | } |
76 | } |
76 | |
77 | |
77 | push @w, |
78 | push @w, |
78 | Event->timer(after => $_[3], cb => sub { |
79 | Event->timer (after => $_[3], cb => sub { |
79 | $current->ready; |
80 | $current->ready; |
80 | }); |
81 | }); |
81 | |
82 | |
82 | Coro::schedule; |
83 | Coro::schedule; |
83 | # wait here |
84 | # wait here |