This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
bump IO::* version to 1.55
[perl5.git] / dist / IO / lib / IO / Poll.pm
CommitLineData
c8aac497 1
cf7fe8a2
GS
2# IO::Poll.pm
3#
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.
7
8package IO::Poll;
9
10use strict;
11use IO::Handle;
12use Exporter ();
cf7fe8a2 13
07f4731b 14our @ISA = qw(Exporter);
67fb9414 15our $VERSION = "1.55";
cf7fe8a2 16
07f4731b 17our @EXPORT = qw( POLLIN
c8aac497
GS
18 POLLOUT
19 POLLERR
20 POLLHUP
21 POLLNVAL
22 );
cf7fe8a2 23
07f4731b
TR
24our @EXPORT_OK = qw(
25 POLLPRI
cf7fe8a2
GS
26 POLLRDNORM
27 POLLWRNORM
28 POLLRDBAND
29 POLLWRBAND
07f4731b 30 POLLNORM
c8aac497 31 );
cf7fe8a2 32
c8aac497
GS
33# [0] maps fd's to requested masks
34# [1] maps fd's to returned masks
35# [2] maps fd's to handles
cf7fe8a2
GS
36sub new {
37 my $class = shift;
38
c8aac497 39 my $self = bless [{},{},{}], $class;
cf7fe8a2
GS
40
41 $self;
42}
43
44sub mask {
45 my $self = shift;
46 my $io = shift;
47 my $fd = fileno($io);
06cd8c16 48 return unless defined $fd;
c8aac497 49 if (@_) {
cf7fe8a2 50 my $mask = shift;
cf7fe8a2 51 if($mask) {
c8aac497
GS
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 {
5284b67e
MS
56 delete $self->[0]{$fd}{$io};
57 unless(%{$self->[0]{$fd}}) {
58 # We no longer have any handles for this FD
59 delete $self->[1]{$fd};
60 delete $self->[0]{$fd};
61 }
62 delete $self->[2]{$io};
cf7fe8a2
GS
63 }
64 }
c8aac497
GS
65
66 return unless exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io};
334f17bd 67 return $self->[0]{$fd}{$io};
cf7fe8a2
GS
68}
69
70
71sub poll {
72 my($self,$timeout) = @_;
73
74 $self->[1] = {};
75
c8aac497 76 my($fd,$mask,$iom);
cf7fe8a2
GS
77 my @poll = ();
78
c8aac497
GS
79 while(($fd,$iom) = each %{$self->[0]}) {
80 $mask = 0;
81 $mask |= $_ for values(%$iom);
82 push(@poll,$fd => $mask);
cf7fe8a2
GS
83 }
84
03a80229 85 my $ret = _poll(defined($timeout) ? $timeout * 1000 : -1,@poll);
cf7fe8a2
GS
86
87 return $ret
88 unless $ret > 0;
89
90 while(@poll) {
91 my($fd,$got) = splice(@poll,0,2);
c8aac497 92 $self->[1]{$fd} = $got if $got;
cf7fe8a2
GS
93 }
94
95 return $ret;
96}
97
98sub events {
99 my $self = shift;
100 my $io = shift;
101 my $fd = fileno($io);
c8aac497
GS
102 exists $self->[1]{$fd} and exists $self->[0]{$fd}{$io}
103 ? $self->[1]{$fd} & ($self->[0]{$fd}{$io}|POLLHUP|POLLERR|POLLNVAL)
334f17bd 104 : 0;
cf7fe8a2
GS
105}
106
107sub remove {
108 my $self = shift;
109 my $io = shift;
110 $self->mask($io,0);
111}
112
113sub handles {
114 my $self = shift;
c8aac497 115 return values %{$self->[2]} unless @_;
cf7fe8a2
GS
116
117 my $events = shift || 0;
118 my($fd,$ev,$io,$mask);
119 my @handles = ();
120
121 while(($fd,$ev) = each %{$self->[1]}) {
c8aac497
GS
122 while (($io,$mask) = each %{$self->[0]{$fd}}) {
123 $mask |= POLLHUP|POLLERR|POLLNVAL; # must allow these
124 push @handles,$self->[2]{$io} if ($ev & $mask) & $events;
334f17bd 125 }
cf7fe8a2
GS
126 }
127 return @handles;
128}
129
1301;
131
132__END__
133
134=head1 NAME
135
136IO::Poll - Object interface to system poll call
137
138=head1 SYNOPSIS
139
140 use IO::Poll qw(POLLRDNORM POLLWRNORM POLLIN POLLHUP);
141
f00d3350 142 $poll = IO::Poll->new();
cf7fe8a2 143
c8aac497
GS
144 $poll->mask($input_handle => POLLIN);
145 $poll->mask($output_handle => POLLOUT);
cf7fe8a2
GS
146
147 $poll->poll($timeout);
148
149 $ev = $poll->events($input);
150
151=head1 DESCRIPTION
152
153C<IO::Poll> is a simple interface to the system level poll routine.
154
155=head1 METHODS
156
157=over 4
158
159=item mask ( IO [, EVENT_MASK ] )
160
161If EVENT_MASK is given, then, if EVENT_MASK is non-zero, IO is added to the
162list of file descriptors and the next call to poll will check for
163any event specified in EVENT_MASK. If EVENT_MASK is zero then IO will be
164removed from the list of file descriptors.
165
166If EVENT_MASK is not given then the return value will be the current
167event mask value for IO.
168
169=item poll ( [ TIMEOUT ] )
170
171Call the system level poll routine. If TIMEOUT is not specified then the
172call will block. Returns the number of handles which had events
173happen, or -1 on error.
174
175=item events ( IO )
176
3c4b39be 177Returns the event mask which represents the events that happened on IO
cf7fe8a2
GS
178during the last call to C<poll>.
179
180=item remove ( IO )
181
182Remove IO from the list of file descriptors for the next poll.
183
184=item handles( [ EVENT_MASK ] )
185
186Returns a list of handles. If EVENT_MASK is not given then a list of all
187handles known will be returned. If EVENT_MASK is given then a list
188of handles will be returned which had one of the events specified by
189EVENT_MASK happen during the last call ti C<poll>
190
191=back
192
193=head1 SEE ALSO
194
195L<poll(2)>, L<IO::Handle>, L<IO::Select>
196
197=head1 AUTHOR
198
854822f1 199Graham Barr. Currently maintained by the Perl Porters. Please report all
503e0d1c 200bugs at L<https://github.com/Perl/perl5/issues>.
cf7fe8a2
GS
201
202=head1 COPYRIGHT
203
204Copyright (c) 1997-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
205This program is free software; you can redistribute it and/or
206modify it under the same terms as Perl itself.
207
208=cut