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.
13 our(@ISA, @EXPORT_OK, @EXPORT, $VERSION);
34 # [0] maps fd's to requested masks
35 # [1] maps fd's to returned masks
36 # [2] maps fd's to handles
40 my $self = bless [{},{},{}], $class;
52 $self->[0]{$fd} = $mask;
53 $self->[1]{$fd} = 0; # no returned mask until poll() called
54 $self->[2]{$fd} = $io;
56 delete $self->[0]{$fd};
57 delete $self->[1]{$fd};
58 delete $self->[2]{$fd};
62 return unless exists $self->[1]{$fd};
63 return $self->[1]{$fd};
68 my($self,$timeout) = @_;
75 while(($fd,$mask) = each %{$self->[0]}) {
76 push(@poll,$fd => $mask);
79 my $ret = @poll ? _poll(defined($timeout) ? $timeout * 1000 : -1,@poll) : 0;
85 my($fd,$got) = splice(@poll,0,2);
86 $self->[1]{$fd} = $got if $got;
96 exists $self->[1]{$fd} ? $self->[1]{$fd} : 0;
107 return values %{$self->[2]} unless @_;
109 my $events = shift || 0;
110 my($fd,$ev,$io,$mask);
113 while(($fd,$ev) = each %{$self->[1]}) {
114 push @handles,$self->[2]{$fd} if $ev & $events;
125 IO::Poll - Object interface to system poll call
129 use IO::Poll qw(POLLRDNORM POLLWRNORM POLLIN POLLHUP);
131 $poll = new IO::Poll;
133 $poll->mask($input_handle => POLLIN);
134 $poll->mask($output_handle => POLLOUT);
136 $poll->poll($timeout);
138 $ev = $poll->events($input);
142 C<IO::Poll> is a simple interface to the system level poll routine.
148 =item mask ( IO [, EVENT_MASK ] )
150 If EVENT_MASK is given, then, if EVENT_MASK is non-zero, IO is added to the
151 list of file descriptors and the next call to poll will check for
152 any event specified in EVENT_MASK. If EVENT_MASK is zero then IO will be
153 removed from the list of file descriptors.
155 If EVENT_MASK is not given then the return value will be the current
156 event mask value for IO.
158 =item poll ( [ TIMEOUT ] )
160 Call the system level poll routine. If TIMEOUT is not specified then the
161 call will block. Returns the number of handles which had events
162 happen, or -1 on error.
166 Returns the event mask which represents the events that happend on IO
167 during the last call to C<poll>.
171 Remove IO from the list of file descriptors for the next poll.
173 =item handles( [ EVENT_MASK ] )
175 Returns a list of handles. If EVENT_MASK is not given then a list of all
176 handles known will be returned. If EVENT_MASK is given then a list
177 of handles will be returned which had one of the events specified by
178 EVENT_MASK happen during the last call ti C<poll>
184 L<poll(2)>, L<IO::Handle>, L<IO::Select>
188 Graham Barr. Currently maintained by the Perl Porters. Please report all
189 bugs to <perl5-porters@perl.org>.
193 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
194 This program is free software; you can redistribute it and/or
195 modify it under the same terms as Perl itself.