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;
49 return unless defined $fd;
53 $self->[0]{$fd}{$io} = $mask; # the error events are always returned
54 $self->[1]{$fd} = 0; # output mask
55 $self->[2]{$io} = $io; # remember handle
57 delete $self->[0]{$fd}{$io};
58 unless(%{$self->[0]{$fd}}) {
59 # We no longer have any handles for this FD
60 delete $self->[1]{$fd};
61 delete $self->[0]{$fd};
63 delete $self->[2]{$io};
67 return unless exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io};
68 return $self->[0]{$fd}{$io};
73 my($self,$timeout) = @_;
80 while(($fd,$iom) = each %{$self->[0]}) {
82 $mask |= $_ for values(%$iom);
83 push(@poll,$fd => $mask);
86 my $ret = @poll ? _poll(defined($timeout) ? $timeout * 1000 : -1,@poll) : 0;
92 my($fd,$got) = splice(@poll,0,2);
93 $self->[1]{$fd} = $got if $got;
102 my $fd = fileno($io);
103 exists $self->[1]{$fd} and exists $self->[0]{$fd}{$io}
104 ? $self->[1]{$fd} & ($self->[0]{$fd}{$io}|POLLHUP|POLLERR|POLLNVAL)
116 return values %{$self->[2]} unless @_;
118 my $events = shift || 0;
119 my($fd,$ev,$io,$mask);
122 while(($fd,$ev) = each %{$self->[1]}) {
123 while (($io,$mask) = each %{$self->[0]{$fd}}) {
124 $mask |= POLLHUP|POLLERR|POLLNVAL; # must allow these
125 push @handles,$self->[2]{$io} if ($ev & $mask) & $events;
137 IO::Poll - Object interface to system poll call
141 use IO::Poll qw(POLLRDNORM POLLWRNORM POLLIN POLLHUP);
143 $poll = new IO::Poll;
145 $poll->mask($input_handle => POLLIN);
146 $poll->mask($output_handle => POLLOUT);
148 $poll->poll($timeout);
150 $ev = $poll->events($input);
154 C<IO::Poll> is a simple interface to the system level poll routine.
160 =item mask ( IO [, EVENT_MASK ] )
162 If EVENT_MASK is given, then, if EVENT_MASK is non-zero, IO is added to the
163 list of file descriptors and the next call to poll will check for
164 any event specified in EVENT_MASK. If EVENT_MASK is zero then IO will be
165 removed from the list of file descriptors.
167 If EVENT_MASK is not given then the return value will be the current
168 event mask value for IO.
170 =item poll ( [ TIMEOUT ] )
172 Call the system level poll routine. If TIMEOUT is not specified then the
173 call will block. Returns the number of handles which had events
174 happen, or -1 on error.
178 Returns the event mask which represents the events that happened on IO
179 during the last call to C<poll>.
183 Remove IO from the list of file descriptors for the next poll.
185 =item handles( [ EVENT_MASK ] )
187 Returns a list of handles. If EVENT_MASK is not given then a list of all
188 handles known will be returned. If EVENT_MASK is given then a list
189 of handles will be returned which had one of the events specified by
190 EVENT_MASK happen during the last call ti C<poll>
196 L<poll(2)>, L<IO::Handle>, L<IO::Select>
200 Graham Barr. Currently maintained by the Perl Porters. Please report all
201 bugs to <perl5-porters@perl.org>.
205 Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
206 This program is free software; you can redistribute it and/or
207 modify it under the same terms as Perl itself.