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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines