I have a need for a type of watcher for which events are only generated
manually. I want this for event types along the lines of "redraw
required": it's generated as a result of internal program logic, but
I want it to be subject to priority queuing. I'm surprised that there
isn't already a "non-specific" watcher class, one that never automatically
generates an event. However, the "now" method is there, and I think I
can roll my own.
The first thing I've tried is a stopped watcher:
#!/usr/bin/perl
use warnings;
use strict;
use Event;
my $my_watcher = Event->var(parked => 1, cb => sub {
print "my_watcher event\n";
});
Event->io(fd => \*STDIN, cb => sub {
scalar <STDIN>;
print "got input\n";
$my_watcher->now;
});
Event::loop();
This works as I expected. The my_watcher events are generated by the
STDIN handler, and the my_watcher handler gets called.
(Digression: a bug report:
Note that "now" works perfectly well on a stopped watcher. There's a
related bug: if the stopped event has no callback set, then a SEGV
results. Try that out by changing the setup of $my_watcher above to
my $my_watcher = Event->var(parked => 1);
I'm not sure what the semantics of "now" on a stopped watcher ought to be.
The two obvious candidates are: (a) act as now, but balk if no callback
is set; (b) silently do nothing if the watcher is stopped. Option (b)
would be convenient for the usage I discuss below, but I don't know how
"now" is being used by other people. Maybe both versions are useful.
End digression.)
There's a problem with the stopped-watcher approach: I want to obey
normal watcher semantics, namely to not generate events if the watcher
is stopped. I want the recipient of these events to be able to use the
watcher's "active" flag to control whether it receives events. This means
I need an *active* watcher that never automatically generates events.
I might have lots of these active at once, for different types of event,
so it's important that their overhead be minimal when they're quiescent.
What's the lowest-overhead watcher that will never generate an event
while active?
-zefram