ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Select.pm
Revision: 1.13
Committed: Sun Nov 5 19:16:09 2006 UTC (18 years ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.12: +0 -0 lines
State: FILE REMOVED
Log Message:
*** empty log message ***

File Contents

# Content
1 =head1 NAME
2
3 Coro::Select - a (slow but event-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<Event> watchers to do the job, so other
15 coroutines can run in parallel.
16
17 To be effective globally, this module must be C<use>'d before any other
18 module that uses C<select>, so it should generally be the first module
19 C<use>'d in the main program.
20
21 You can also invoke it from the commandline as C<perl -MCoro::Select>.
22
23 =over 4
24
25 =cut
26
27 package Coro::Select;
28
29 use strict;
30
31 use Event;
32
33 use Coro;
34 use Coro::Event;
35
36 use base Exporter::;
37
38 our $VERSION = 2.0;
39 our @EXPORT_OK = "select";
40
41 sub import {
42 my $pkg = shift;
43 if (@_) {
44 $pkg->export (scalar caller 0, @_);
45 } else {
46 $pkg->export ("CORE::GLOBAL", "select");
47 }
48 }
49
50 sub select(;*$$$) { # not the correct prototype, but well... :()
51 if (@_ == 0) {
52 return CORE::select
53 } elsif (@_ == 1) {
54 return CORE::select $_[0]
55 } elsif (defined $_[3] && !$_[3]) {
56 return CORE::select $_[0], $_[1], $_[2], $_[3]
57 } else {
58 my $current = $Coro::current;
59 my $nfound = 0;
60 my @w;
61 for ([0, 'r'], [1, 'w'], [2, 'e']) {
62 my ($i, $poll) = @$_;
63 if (defined (my $vec = $_[$i])) {
64 my $rvec = \$_[$i];
65 for my $b (0 .. (8 * length $vec)) {
66 if (vec $vec, $b, 1) {
67 (vec $$rvec, $b, 1) = 0;
68 push @w,
69 Event->io (fd => $b, poll => $poll, cb => sub {
70 (vec $$rvec, $b, 1) = 1;
71 $nfound++;
72 $current->ready;
73 });
74 }
75 }
76 }
77 }
78
79 push @w,
80 Event->timer (after => $_[3], cb => sub {
81 $current->ready;
82 });
83
84 Coro::schedule;
85 # wait here
86
87 $_->cancel for @w;
88 return $nfound
89 }
90 }
91
92 1;
93
94 =back
95
96 =head1 AUTHOR
97
98 Marc Lehmann <schmorp@schmorp.de>
99 http://home.schmorp.de/
100
101 =cut
102
103