Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
4.5 KiB

  1. # IO::Poll.pm
  2. #
  3. # Copyright (c) 1997-8 Graham Barr <[email protected]>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6. package IO::Poll;
  7. use strict;
  8. use IO::Handle;
  9. use Exporter ();
  10. our(@ISA, @EXPORT_OK, @EXPORT, $VERSION);
  11. @ISA = qw(Exporter);
  12. $VERSION = "0.05";
  13. @EXPORT = qw( POLLIN
  14. POLLOUT
  15. POLLERR
  16. POLLHUP
  17. POLLNVAL
  18. );
  19. @EXPORT_OK = qw(
  20. POLLPRI
  21. POLLRDNORM
  22. POLLWRNORM
  23. POLLRDBAND
  24. POLLWRBAND
  25. POLLNORM
  26. );
  27. # [0] maps fd's to requested masks
  28. # [1] maps fd's to returned masks
  29. # [2] maps fd's to handles
  30. sub new {
  31. my $class = shift;
  32. my $self = bless [{},{},{}], $class;
  33. $self;
  34. }
  35. sub mask {
  36. my $self = shift;
  37. my $io = shift;
  38. my $fd = fileno($io);
  39. if (@_) {
  40. my $mask = shift;
  41. if($mask) {
  42. $self->[0]{$fd}{$io} = $mask; # the error events are always returned
  43. $self->[1]{$fd} = 0; # output mask
  44. $self->[2]{$io} = $io; # remember handle
  45. } else {
  46. delete $self->[0]{$fd}{$io};
  47. delete $self->[1]{$fd} unless %{$self->[0]{$fd}};
  48. delete $self->[2]{$io};
  49. }
  50. }
  51. return unless exists $self->[0]{$fd} and exists $self->[0]{$fd}{$io};
  52. return $self->[0]{$fd}{$io};
  53. }
  54. sub poll {
  55. my($self,$timeout) = @_;
  56. $self->[1] = {};
  57. my($fd,$mask,$iom);
  58. my @poll = ();
  59. while(($fd,$iom) = each %{$self->[0]}) {
  60. $mask = 0;
  61. $mask |= $_ for values(%$iom);
  62. push(@poll,$fd => $mask);
  63. }
  64. my $ret = @poll ? _poll(defined($timeout) ? $timeout * 1000 : -1,@poll) : 0;
  65. return $ret
  66. unless $ret > 0;
  67. while(@poll) {
  68. my($fd,$got) = splice(@poll,0,2);
  69. $self->[1]{$fd} = $got if $got;
  70. }
  71. return $ret;
  72. }
  73. sub events {
  74. my $self = shift;
  75. my $io = shift;
  76. my $fd = fileno($io);
  77. exists $self->[1]{$fd} and exists $self->[0]{$fd}{$io}
  78. ? $self->[1]{$fd} & ($self->[0]{$fd}{$io}|POLLHUP|POLLERR|POLLNVAL)
  79. : 0;
  80. }
  81. sub remove {
  82. my $self = shift;
  83. my $io = shift;
  84. $self->mask($io,0);
  85. }
  86. sub handles {
  87. my $self = shift;
  88. return values %{$self->[2]} unless @_;
  89. my $events = shift || 0;
  90. my($fd,$ev,$io,$mask);
  91. my @handles = ();
  92. while(($fd,$ev) = each %{$self->[1]}) {
  93. while (($io,$mask) = each %{$self->[0]{$fd}}) {
  94. $mask |= POLLHUP|POLLERR|POLLNVAL; # must allow these
  95. push @handles,$self->[2]{$io} if ($ev & $mask) & $events;
  96. }
  97. }
  98. return @handles;
  99. }
  100. 1;
  101. __END__
  102. =head1 NAME
  103. IO::Poll - Object interface to system poll call
  104. =head1 SYNOPSIS
  105. use IO::Poll qw(POLLRDNORM POLLWRNORM POLLIN POLLHUP);
  106. $poll = new IO::Poll;
  107. $poll->mask($input_handle => POLLIN);
  108. $poll->mask($output_handle => POLLOUT);
  109. $poll->poll($timeout);
  110. $ev = $poll->events($input);
  111. =head1 DESCRIPTION
  112. C<IO::Poll> is a simple interface to the system level poll routine.
  113. =head1 METHODS
  114. =over 4
  115. =item mask ( IO [, EVENT_MASK ] )
  116. If EVENT_MASK is given, then, if EVENT_MASK is non-zero, IO is added to the
  117. list of file descriptors and the next call to poll will check for
  118. any event specified in EVENT_MASK. If EVENT_MASK is zero then IO will be
  119. removed from the list of file descriptors.
  120. If EVENT_MASK is not given then the return value will be the current
  121. event mask value for IO.
  122. =item poll ( [ TIMEOUT ] )
  123. Call the system level poll routine. If TIMEOUT is not specified then the
  124. call will block. Returns the number of handles which had events
  125. happen, or -1 on error.
  126. =item events ( IO )
  127. Returns the event mask which represents the events that happend on IO
  128. during the last call to C<poll>.
  129. =item remove ( IO )
  130. Remove IO from the list of file descriptors for the next poll.
  131. =item handles( [ EVENT_MASK ] )
  132. Returns a list of handles. If EVENT_MASK is not given then a list of all
  133. handles known will be returned. If EVENT_MASK is given then a list
  134. of handles will be returned which had one of the events specified by
  135. EVENT_MASK happen during the last call ti C<poll>
  136. =back
  137. =head1 SEE ALSO
  138. L<poll(2)>, L<IO::Handle>, L<IO::Select>
  139. =head1 AUTHOR
  140. Graham Barr. Currently maintained by the Perl Porters. Please report all
  141. bugs to <[email protected]>.
  142. =head1 COPYRIGHT
  143. Copyright (c) 1997-8 Graham Barr <[email protected]>. All rights reserved.
  144. This program is free software; you can redistribute it and/or
  145. modify it under the same terms as Perl itself.
  146. =cut