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.19 by root, Wed Jul 29 13:15:55 2009 UTC vs.
Revision 1.35 by root, Fri Sep 2 04:35:03 2011 UTC

11 11
12This module implements AnyEvent's strict mode. 12This module implements AnyEvent's strict mode.
13 13
14Loading it makes AnyEvent check all arguments to AnyEvent-methods, at the 14Loading it makes AnyEvent check all arguments to AnyEvent-methods, at the
15expense of being slower (often the argument checking takes longer than the 15expense of being slower (often the argument checking takes longer than the
16actual function). 16actual function). It also wraps all callbacks to check for modifications
17of C<$_>, which indicates a programming bug inside the watcher callback.
17 18
18Normally, you don't load this module yourself but instead use it 19Normally, you don't load this module yourself but instead use it
19indirectly via the C<PERL_ANYEVENT_STRICT> environment variable (see 20indirectly via the C<PERL_ANYEVENT_STRICT> environment variable (see
20L<AnyEvent>). However, this module can be loaded at any time. 21L<AnyEvent>). However, this module can be loaded manually at any time.
21 22
22=cut 23=cut
23 24
24package AnyEvent::Strict; 25package AnyEvent::Strict;
25 26
26use Carp qw(croak); 27use Carp qw(croak);
27use Fcntl (); 28use Errno ();
29use POSIX ();
28 30
29use AnyEvent (); BEGIN { AnyEvent::common_sense } 31use AnyEvent (); BEGIN { AnyEvent::common_sense }
30 32
31our @ISA; 33AnyEvent::_isa_hook 1 => "AnyEvent::Strict", 1;
32 34
33AnyEvent::post_detect { 35BEGIN {
34 # assume the first ISA member is the implementation 36 if (defined &Internals::SvREADONLY) {
35 # # and link us in before it in the chain. 37 # readonly available (at least 5.8.9+, working better in 5.10.1+)
36 my $MODEL = shift @AnyEvent::ISA; 38 *wrap = sub {
37 unshift @ISA, $MODEL; 39 my $cb = shift;
38 unshift @AnyEvent::ISA, AnyEvent::Strict:: 40
41 sub {
42 Internals::SvREADONLY $_, 1;
43 &$cb;
44 Internals::SvREADONLY $_, 0;
45 }
46 };
47 } else {
48 # or not :/
49 my $magic = []; # a unique magic value
50
51 *wrap = sub {
52 my $cb = shift;
53
54 sub {
55 local $_ = $magic;
56
57 &$cb;
58
59 if (!ref $_ || $_ != $magic) {
60 require AnyEvent::Debug;
61 die "callback $cb (" . AnyEvent::Debug::cb2str ($cb) . ") modified \$_ without restoring it.\n";
62 }
63 }
64 };
65 }
66}
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";
39}; 89};
40 90
41sub io { 91sub io {
42 my $class = shift; 92 my $class = shift;
43 my %arg = @_; 93 my (%arg, $fh, $cb, $fd) = @_;
44 94
45 ref $arg{cb} 95 ref $arg{cb}
46 or croak "AnyEvent->io called with illegal cb argument '$arg{cb}'"; 96 or croak "AnyEvent->io called with illegal cb argument '$arg{cb}'";
47 delete $arg{cb}; 97 $cb = wrap delete $arg{cb};
48 98
49 $arg{poll} =~ /^[rw]$/ 99 $arg{poll} =~ /^[rw]$/
50 or croak "AnyEvent->io called with illegal poll argument '$arg{poll}'"; 100 or croak "AnyEvent->io called with illegal poll argument '$arg{poll}'";
51 101
52 if (defined fileno $arg{fh} or ref $arg{fh} or $arg{fh} !~ /^\s*\d+\s*$/) { 102 $fh = delete $arg{fh};
53 defined fileno $arg{fh} 103
54 or croak "AnyEvent->io called with illegal fh argument '$arg{fh}'"; 104 if ($fh =~ /^\s*\d+\s*$/) {
105 $fd = $fh;
106 $fh = AnyEvent::_dupfh $arg{poll}, $fh;
55 } else { 107 } else {
56 $arg{fh} = AnyEvent::_dupfh $arg{poll}, $arg{fh}; 108 defined eval { $fd = fileno $fh }
109 or croak "AnyEvent->io called with illegal fh argument '$fh'";
57 } 110 }
58 111
59 -f $arg{fh} 112 -f $fh
60 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";
61 114
62 delete $arg{poll}; 115 delete $arg{poll};
63 delete $arg{fh};
64 116
65 croak "AnyEvent->io called with unsupported parameter(s) " . join ", ", keys %arg 117 croak "AnyEvent->io called with unsupported parameter(s) " . join ", ", keys %arg
66 if keys %arg; 118 if keys %arg;
67 119
120 ++$FD_INUSE[$fd];
121
122 bless [
123 $fd,
68 $class->SUPER::io (@_) 124 $class->SUPER::io (@_, cb => $cb)
125 ], "AnyEvent::Strict::io";
126}
127
128sub AnyEvent::Strict::io::DESTROY {
129 --$FD_INUSE[$_[0][0]];
69} 130}
70 131
71sub timer { 132sub timer {
72 my $class = shift; 133 my $class = shift;
73 my %arg = @_; 134 my %arg = @_;
74 135
75 ref $arg{cb} 136 ref $arg{cb}
76 or croak "AnyEvent->timer called with illegal cb argument '$arg{cb}'"; 137 or croak "AnyEvent->timer called with illegal cb argument '$arg{cb}'";
77 delete $arg{cb}; 138 my $cb = wrap delete $arg{cb};
78 139
79 exists $arg{after} 140 exists $arg{after}
80 or croak "AnyEvent->timer called without mandatory 'after' parameter"; 141 or croak "AnyEvent->timer called without mandatory 'after' parameter";
81 delete $arg{after}; 142 delete $arg{after};
82 143
85 delete $arg{interval}; 146 delete $arg{interval};
86 147
87 croak "AnyEvent->timer called with unsupported parameter(s) " . join ", ", keys %arg 148 croak "AnyEvent->timer called with unsupported parameter(s) " . join ", ", keys %arg
88 if keys %arg; 149 if keys %arg;
89 150
90 $class->SUPER::timer (@_) 151 $class->SUPER::timer (@_, cb => $cb)
91} 152}
92 153
93sub signal { 154sub signal {
94 my $class = shift; 155 my $class = shift;
95 my %arg = @_; 156 my %arg = @_;
96 157
97 ref $arg{cb} 158 ref $arg{cb}
98 or croak "AnyEvent->signal called with illegal cb argument '$arg{cb}'"; 159 or croak "AnyEvent->signal called with illegal cb argument '$arg{cb}'";
99 delete $arg{cb}; 160 my $cb = wrap delete $arg{cb};
100 161
101 defined AnyEvent::Base::sig2num $arg{signal} and $arg{signal} == 0 162 defined AnyEvent::Base::sig2num $arg{signal} and $arg{signal} == 0
102 or croak "AnyEvent->signal called with illegal signal name '$arg{signal}'"; 163 or croak "AnyEvent->signal called with illegal signal name '$arg{signal}'";
103 delete $arg{signal}; 164 delete $arg{signal};
104 165
105 croak "AnyEvent->signal called with unsupported parameter(s) " . join ", ", keys %arg 166 croak "AnyEvent->signal called with unsupported parameter(s) " . join ", ", keys %arg
106 if keys %arg; 167 if keys %arg;
107 168
108 $class->SUPER::signal (@_) 169 $class->SUPER::signal (@_, cb => $cb)
109} 170}
110 171
111sub child { 172sub child {
112 my $class = shift; 173 my $class = shift;
113 my %arg = @_; 174 my %arg = @_;
114 175
115 ref $arg{cb} 176 ref $arg{cb}
116 or croak "AnyEvent->child called with illegal cb argument '$arg{cb}'"; 177 or croak "AnyEvent->child called with illegal cb argument '$arg{cb}'";
117 delete $arg{cb}; 178 my $cb = wrap delete $arg{cb};
118 179
119 $arg{pid} =~ /^-?\d+$/ 180 $arg{pid} =~ /^-?\d+$/
120 or croak "AnyEvent->child called with malformed pid value '$arg{pid}'"; 181 or croak "AnyEvent->child called with malformed pid value '$arg{pid}'";
121 delete $arg{pid}; 182 delete $arg{pid};
122 183
123 croak "AnyEvent->child called with unsupported parameter(s) " . join ", ", keys %arg 184 croak "AnyEvent->child called with unsupported parameter(s) " . join ", ", keys %arg
124 if keys %arg; 185 if keys %arg;
125 186
126 $class->SUPER::child (@_) 187 $class->SUPER::child (@_, cb => $cb)
127} 188}
128 189
129sub idle { 190sub idle {
130 my $class = shift; 191 my $class = shift;
131 my %arg = @_; 192 my %arg = @_;
132 193
133 ref $arg{cb} 194 ref $arg{cb}
134 or croak "AnyEvent->idle called with illegal cb argument '$arg{cb}'"; 195 or croak "AnyEvent->idle called with illegal cb argument '$arg{cb}'";
135 delete $arg{cb}; 196 my $cb = wrap delete $arg{cb};
136 197
137 croak "AnyEvent->idle called with unsupported parameter(s) " . join ", ", keys %arg 198 croak "AnyEvent->idle called with unsupported parameter(s) " . join ", ", keys %arg
138 if keys %arg; 199 if keys %arg;
139 200
140 $class->SUPER::idle (@_) 201 $class->SUPER::idle (@_, cb => $cb)
141} 202}
142 203
143sub condvar { 204sub condvar {
144 my $class = shift; 205 my $class = shift;
145 my %arg = @_; 206 my %arg = @_;
146 207
147 !exists $arg{cb} or ref $arg{cb} 208 !exists $arg{cb} or ref $arg{cb}
148 or croak "AnyEvent->condvar called with illegal cb argument '$arg{cb}'"; 209 or croak "AnyEvent->condvar called with illegal cb argument '$arg{cb}'";
149 delete $arg{cb}; 210 my @cb = exists $arg{cb} ? (cb => wrap delete $arg{cb}) : ();
150 211
151 croak "AnyEvent->condvar called with unsupported parameter(s) " . join ", ", keys %arg 212 croak "AnyEvent->condvar called with unsupported parameter(s) " . join ", ", keys %arg
152 if keys %arg; 213 if keys %arg;
153 214
154 $class->SUPER::condvar (@_) 215 $class->SUPER::condvar (@cb);
155} 216}
156 217
157sub time { 218sub time {
158 my $class = shift; 219 my $class = shift;
159 220

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines