Start anew
[msysgit.git] / lib / perl5 / 5.6.1 / msys / IO / Poll.pm
blob70a3469edbb226f2a5ac7b50ef801fd9f213524b
2 # IO::Poll.pm
4 # Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
5 # This program is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
8 package IO::Poll;
10 use strict;
11 use IO::Handle;
12 use Exporter ();
13 our(@ISA, @EXPORT_OK, @EXPORT, $VERSION);
15 @ISA = qw(Exporter);
16 $VERSION = "0.05";
18 @EXPORT = qw( POLLIN
19 POLLOUT
20 POLLERR
21 POLLHUP
22 POLLNVAL
25 @EXPORT_OK = qw(
26 POLLPRI
27 POLLRDNORM
28 POLLWRNORM
29 POLLRDBAND
30 POLLWRBAND
31 POLLNORM
34 # [0] maps fd's to requested masks
35 # [1] maps fd's to returned masks
36 # [2] maps fd's to handles
37 sub new {
38 my $class = shift;
40 my $self = bless [{},{},{}], $class;
42 $self;
45 sub mask {
46 my $self = shift;
47 my $io = shift;
48 my $fd = fileno($io);
49 if (@_) {
50 my $mask = shift;
51 if($mask) {
52 $self->[0]{$fd}{$io} = $mask; # the error events are always returned
53 $self->[1]{$fd} = 0; # output mask
54 $self->[2]{$io} = $io; # remember handle
55 } else {
56 delete $self->[0]{$fd}{$io};
57 delete $self->[1]{$fd} unless %{$self->[0]{$fd}};
58 delete $self->[2]{$io};
62 return unless exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io};
63 return $self->[0]{$fd}{$io};
67 sub poll {
68 my($self,$timeout) = @_;
70 $self->[1] = {};
72 my($fd,$mask,$iom);
73 my @poll = ();
75 while(($fd,$iom) = each %{$self->[0]}) {
76 $mask = 0;
77 $mask |= $_ for values(%$iom);
78 push(@poll,$fd => $mask);
81 my $ret = @poll ? _poll(defined($timeout) ? $timeout * 1000 : -1,@poll) : 0;
83 return $ret
84 unless $ret > 0;
86 while(@poll) {
87 my($fd,$got) = splice(@poll,0,2);
88 $self->[1]{$fd} = $got if $got;
91 return $ret;
94 sub events {
95 my $self = shift;
96 my $io = shift;
97 my $fd = fileno($io);
98 exists $self->[1]{$fd} and exists $self->[0]{$fd}{$io}
99 ? $self->[1]{$fd} & ($self->[0]{$fd}{$io}|POLLHUP|POLLERR|POLLNVAL)
100 : 0;
103 sub remove {
104 my $self = shift;
105 my $io = shift;
106 $self->mask($io,0);
109 sub handles {
110 my $self = shift;
111 return values %{$self->[2]} unless @_;
113 my $events = shift || 0;
114 my($fd,$ev,$io,$mask);
115 my @handles = ();
117 while(($fd,$ev) = each %{$self->[1]}) {
118 while (($io,$mask) = each %{$self->[0]{$fd}}) {
119 $mask |= POLLHUP|POLLERR|POLLNVAL; # must allow these
120 push @handles,$self->[2]{$io} if ($ev & $mask) & $events;
123 return @handles;
128 __END__
130 =head1 NAME
132 IO::Poll - Object interface to system poll call
134 =head1 SYNOPSIS
136 use IO::Poll qw(POLLRDNORM POLLWRNORM POLLIN POLLHUP);
138 $poll = new IO::Poll;
140 $poll->mask($input_handle => POLLIN);
141 $poll->mask($output_handle => POLLOUT);
143 $poll->poll($timeout);
145 $ev = $poll->events($input);
147 =head1 DESCRIPTION
149 C<IO::Poll> is a simple interface to the system level poll routine.
151 =head1 METHODS
153 =over 4
155 =item mask ( IO [, EVENT_MASK ] )
157 If EVENT_MASK is given, then, if EVENT_MASK is non-zero, IO is added to the
158 list of file descriptors and the next call to poll will check for
159 any event specified in EVENT_MASK. If EVENT_MASK is zero then IO will be
160 removed from the list of file descriptors.
162 If EVENT_MASK is not given then the return value will be the current
163 event mask value for IO.
165 =item poll ( [ TIMEOUT ] )
167 Call the system level poll routine. If TIMEOUT is not specified then the
168 call will block. Returns the number of handles which had events
169 happen, or -1 on error.
171 =item events ( IO )
173 Returns the event mask which represents the events that happend on IO
174 during the last call to C<poll>.
176 =item remove ( IO )
178 Remove IO from the list of file descriptors for the next poll.
180 =item handles( [ EVENT_MASK ] )
182 Returns a list of handles. If EVENT_MASK is not given then a list of all
183 handles known will be returned. If EVENT_MASK is given then a list
184 of handles will be returned which had one of the events specified by
185 EVENT_MASK happen during the last call ti C<poll>
187 =back
189 =head1 SEE ALSO
191 L<poll(2)>, L<IO::Handle>, L<IO::Select>
193 =head1 AUTHOR
195 Graham Barr. Currently maintained by the Perl Porters. Please report all
196 bugs to <perl5-porters@perl.org>.
198 =head1 COPYRIGHT
200 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
201 This program is free software; you can redistribute it and/or
202 modify it under the same terms as Perl itself.
204 =cut