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

Comparing EV/EV.pm (file contents):
Revision 1.6 by root, Sat Oct 27 07:30:10 2007 UTC vs.
Revision 1.10 by root, Mon Oct 29 07:24:37 2007 UTC

25 }; 25 };
26 26
27 my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub { 27 my $w = EV::io \*STDIN, EV::READ | EV::PERSIST, sub {
28 my ($w, $events) = @_; # all callbacks get the watcher object and event mask 28 my ($w, $events) = @_; # all callbacks get the watcher object and event mask
29 if ($events & EV::TIMEOUT) { 29 if ($events & EV::TIMEOUT) {
30 warn "nothign received on stdin for 10 seconds, retrying"; 30 warn "nothing received on stdin for 10 seconds, retrying";
31 } else { 31 } else {
32 warn "stdin is readable, you entered: ", <STDIN>; 32 warn "stdin is readable, you entered: ", <STDIN>;
33 } 33 }
34 }; 34 };
35 $w->timeout (10); 35 $w->timeout (10);
55package EV; 55package EV;
56 56
57use strict; 57use strict;
58 58
59BEGIN { 59BEGIN {
60 our $VERSION = '0.01'; 60 our $VERSION = '0.02';
61 use XSLoader; 61 use XSLoader;
62 XSLoader::load "EV", $VERSION; 62 XSLoader::load "EV", $VERSION;
63} 63}
64 64
65=head1 FUNCTIONAL INTERFACE 65=head1 BASIC INTERFACE
66 66
67=over 4 67=over 4
68 68
69=item $EV::NPRI 69=item $EV::NPRI
70 70
71How many priority levels are available. 71How many priority levels are available.
72
73=item $EV::DIED
74
75Must contain a reference to a function that is called when a callback
76throws an exception (with $@ containing thr error). The default prints an
77informative message and continues.
78
79If this callback throws an exception it will be silently ignored.
72 80
73=item $time = EV::now 81=item $time = EV::now
74 82
75Returns the time in (fractional) seconds since the epoch. 83Returns the time in (fractional) seconds since the epoch.
76 84
101 109
102As long as the returned watcher object is alive, call the C<$callback> 110As long as the returned watcher object is alive, call the C<$callback>
103when the events specified in C<$eventmask> happen. Initially, the timeout 111when the events specified in C<$eventmask> happen. Initially, the timeout
104is disabled. 112is disabled.
105 113
106The C<io_ns> variant doesn't add/start the newly created watcher. 114You can additionall set a timeout to occur on the watcher, but note that
115this timeout will not be reset when you get an I/O event in the EV::PERSIST
116case, and reaching a timeout will always stop the watcher even in the
117EV::PERSIST case.
118
119If you want a timeout to occur only after a specific time of inactivity, set
120a repeating timeout and do NOT use EV::PERSIST.
107 121
108Eventmask can be one or more of these constants ORed together: 122Eventmask can be one or more of these constants ORed together:
109 123
110 EV::READ wait until read() wouldn't block anymore 124 EV::READ wait until read() wouldn't block anymore
111 EV::WRITE wait until write() wouldn't block anymore 125 EV::WRITE wait until write() wouldn't block anymore
112 EV::PERSIST stay active after an event occured 126 EV::PERSIST stay active after a (non-timeout) event occured
127
128The C<io_ns> variant doesn't add/start the newly created watcher.
129
130=item my $w = EV::timed_io $fileno_or_fh, $eventmask, $timeout, $callback
131
132=item my $w = EV::timed_io_ns $fileno_or_fh, $eventmask, $timeout, $callback
133
134Same as C<io> and C<io_ns>, but also specifies a timeout (as if there was
135a call to C<< $w->timeout ($timout, 1) >>. The persist flag is not allowed
136and will automatically be cleared. The watcher will be restarted after each event.
137
138If the timeout is zero or undef, no timeout will be set, and a normal
139watcher (with the persist flag set!) will be created.
140
141This has the effect of timing out after the specified period of inactivity
142has happened.
143
144Due to the design of libevent, this is also relatively inefficient, having
145one or two io watchers and a separate timeout watcher that you reset on
146activity (by calling its C<start> method) is usually more efficient.
113 147
114=item my $w = EV::timer $after, $repeat, $callback 148=item my $w = EV::timer $after, $repeat, $callback
115 149
116=item my $w = EV::timer_ns $after, $repeat, $callback 150=item my $w = EV::timer_ns $after, $repeat, $callback
117 151
138 172
139This can be used to schedule a callback to run at very regular intervals, 173This can be used to schedule a callback to run at very regular intervals,
140as long as the processing time is less then the interval (otherwise 174as long as the processing time is less then the interval (otherwise
141obviously events will be skipped). 175obviously events will be skipped).
142 176
177Another way to think about it (for the mathematically inclined) is that
178C<timer_abs> will try to tun the callback at the next possible time where
179C<$time = $at (mod $interval)>, regardless of any time jumps.
180
143The C<timer_abs_ns> variant doesn't add/start the newly created watcher. 181The C<timer_abs_ns> variant doesn't add/start the newly created watcher.
144 182
145=item my $w = EV::signal $signum, $callback 183=item my $w = EV::signal $signum, $callback
146 184
147=item my $w = EV::signal_ns $signum, $callback 185=item my $w = EV::signal_ns $signum, $callback
182 220
183=item $current_fh = $w->fh 221=item $current_fh = $w->fh
184 222
185=item $old_fh = $w->fh ($new_fh) 223=item $old_fh = $w->fh ($new_fh)
186 224
187Returns the previously set filehandle and optionally set a new one. 225Returns the previously set filehandle and optionally set a new one (also
226clears the EV::SIGNAL flag when setting a filehandle).
227
228=item $current_signal = $w->signal
229
230=item $old_signal = $w->signal ($new_signal)
231
232Returns the previously set signal number and optionally set a new one (also sets
233the EV::SIGNAL flag when setting a signal).
188 234
189=item $current_eventmask = $w->events 235=item $current_eventmask = $w->events
190 236
191=item $old_eventmask = $w->events ($new_eventmask) 237=item $old_eventmask = $w->events ($new_eventmask)
192 238
209=head1 BUGS 255=head1 BUGS
210 256
211Lots. Libevent itself isn't well tested and rather buggy, and this module 257Lots. Libevent itself isn't well tested and rather buggy, and this module
212is quite new at the moment. 258is quite new at the moment.
213 259
214Please note that the epoll method is not, in general, reliable in 260Please note that the epoll method is not, in general, reliable in programs
215programs that use fork (even if no libveent calls are being made in the 261that use fork (even if no libveent calls are being made in the forked
216forked process). Since this is such a common issue, this module will 262process). If your program behaves erratically, try setting the environment
217force the epoll method in EV to be off *unless* the global variable 263variable C<EVENT_NOEPOLL> first when running the program.
218$EV::ENABLE_EPOLL is set to 1 *before* loading this module for the first 264
219time. 265In general, if you fork, then you can only use the EV module in one of the
266children.
220 267
221=cut 268=cut
222 269
270our $DIED = sub {
271 warn "EV: error in callback (ignoring): $@";
272};
273
223our $NPRI = 4; 274our $NPRI = 4;
224our $BASE;
225our $ENABLE_EPOLL;
226
227{
228 local $ENV{EVENT_NOEPOLL};
229 $ENV{EVENT_NOEPOLL} = 1 unless $ENABLE_EPOLL;
230 $BASE = init; 275our $BASE = init;
231}
232
233priority_init $NPRI; 276priority_init $NPRI;
234 277
235push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"]; 278push @AnyEvent::REGISTRY, [EV => "EV::AnyEvent"];
236 279
2371; 2801;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines