ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent/lib/AnyEvent/Strict.pm
(Generate patch)

Comparing AnyEvent/lib/AnyEvent/Strict.pm (file contents):
Revision 1.30 by root, Thu Aug 18 19:35:15 2011 UTC vs.
Revision 1.35 by root, Fri Sep 2 04:35:03 2011 UTC

23=cut 23=cut
24 24
25package AnyEvent::Strict; 25package AnyEvent::Strict;
26 26
27use Carp qw(croak); 27use Carp qw(croak);
28use Errno ();
29use POSIX ();
28 30
29use AnyEvent (); BEGIN { AnyEvent::common_sense } 31use AnyEvent (); BEGIN { AnyEvent::common_sense }
30 32
31AnyEvent::_isa_hook 0 => "AnyEvent::Strict", 1; 33AnyEvent::_isa_hook 1 => "AnyEvent::Strict", 1;
32 34
33BEGIN { 35BEGIN {
34 if (defined &Internals::SvREADONLY) { 36 if (defined &Internals::SvREADONLY) {
35 # readonly available (at least 5.8.9+, working better in 5.10.1+) 37 # readonly available (at least 5.8.9+, working better in 5.10.1+)
36 *wrap = sub { 38 *wrap = sub {
61 } 63 }
62 }; 64 };
63 } 65 }
64} 66}
65 67
68our (@FD_INUSE, $FD_I);
69our $FD_CHECK_W = AE::timer 4, 4, sub {
70 my $cnt = (@FD_INUSE < 100 * 10 ? int @FD_INUSE * 0.1 : 100) || 10;
71
72 if ($FD_I <= 0) {
73 #pop @FD_INUSE while @FD_INUSE && !$FD_INUSE[-1];
74 ($FD_I = @FD_INUSE) >= 0
75 or return; # empty
76 }
77
78 $cnt = $FD_I if $cnt > $FD_I;
79
80 eval {
81 do {
82 !$FD_INUSE[--$FD_I]
83 or (POSIX::lseek $FD_I, 0, 1) != -1
84 or $! != Errno::EBADF
85 or die;
86 } while --$cnt;
87 1
88 } or AE::log crit => "file descriptor $FD_I registered with AnyEvent but prematurely closed, event loop might malfunction.\n";
89};
90
66sub io { 91sub io {
67 my $class = shift; 92 my $class = shift;
68 my %arg = @_; 93 my (%arg, $fh, $cb, $fd) = @_;
69 94
70 ref $arg{cb} 95 ref $arg{cb}
71 or croak "AnyEvent->io called with illegal cb argument '$arg{cb}'"; 96 or croak "AnyEvent->io called with illegal cb argument '$arg{cb}'";
72 my $cb = wrap delete $arg{cb}; 97 $cb = wrap delete $arg{cb};
73 98
74 $arg{poll} =~ /^[rw]$/ 99 $arg{poll} =~ /^[rw]$/
75 or croak "AnyEvent->io called with illegal poll argument '$arg{poll}'"; 100 or croak "AnyEvent->io called with illegal poll argument '$arg{poll}'";
76 101
102 $fh = delete $arg{fh};
103
77 if ($arg{fh} =~ /^\s*\d+\s*$/) { 104 if ($fh =~ /^\s*\d+\s*$/) {
105 $fd = $fh;
78 $arg{fh} = AnyEvent::_dupfh $arg{poll}, $arg{fh}; 106 $fh = AnyEvent::_dupfh $arg{poll}, $fh;
79 } else { 107 } else {
80 defined eval { fileno $arg{fh} } 108 defined eval { $fd = fileno $fh }
81 or croak "AnyEvent->io called with illegal fh argument '$arg{fh}'"; 109 or croak "AnyEvent->io called with illegal fh argument '$fh'";
82 } 110 }
83 111
84 -f $arg{fh} 112 -f $fh
85 and croak "AnyEvent->io called with fh argument pointing to a file"; 113 and croak "AnyEvent->io called with fh argument pointing to a file";
86 114
87 delete $arg{poll}; 115 delete $arg{poll};
88 delete $arg{fh};
89 116
90 croak "AnyEvent->io called with unsupported parameter(s) " . join ", ", keys %arg 117 croak "AnyEvent->io called with unsupported parameter(s) " . join ", ", keys %arg
91 if keys %arg; 118 if keys %arg;
92 119
120 ++$FD_INUSE[$fd];
121
122 bless [
123 $fd,
93 $class->SUPER::io (@_, cb => $cb) 124 $class->SUPER::io (@_, cb => $cb)
125 ], "AnyEvent::Strict::io";
126}
127
128sub AnyEvent::Strict::io::DESTROY {
129 --$FD_INUSE[$_[0][0]];
94} 130}
95 131
96sub timer { 132sub timer {
97 my $class = shift; 133 my $class = shift;
98 my %arg = @_; 134 my %arg = @_;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines