ev_periodic like functionality for time intervals

Thilo Schulz thilo at tjps.eu
Wed May 20 23:57:46 CEST 2015

On Wednesday 20 May 2015 18:25:28 you wrote:
> The most compatible/conceptually simple solution would be to simply have
> a one-minute timer or so and check for the interval yourself, which might
> also simplify the interval check when it is in local time. One minute is
> roughly the accuracy with which time jumps are detected.

Hmm, time jumps will only be the exception that I want to catch if they 
happen. I still want to trigger on entering the interval as soon as possible, 
so to have a second watcher, a timer watcher in addition to the periodic is a 
bit ugly in my opinion.

> A more advanced technique would be to run a periodic watcher in manual
> reschedule mode. In that mode, a callback will be invoked whenever a time
> jump is detected (teh callback is very limited in what it can do, check
> the docs).

Oh. I didn't realize the reschedule callback for periodics is invoked every 
time libev() detects a time jump, forwards or backwards. This was not 
immediately clear to me from the documentation, which only says:

  "The current reschedule callback, or 0, if this functionality is switched
  off. Can be changed any time, but changes only take effect when the periodic
  timer fires or ev_periodic_again is being called."

As a user I was oblivious to the fact that rescheduling in this context means 
libev internal rescheduling of when to invoke the periodic watcher; as opposed 
to a "user triggered" rescheduling like via
or when the periodic itself fires.

> Some experimenting might be required, as the reschedule callback will
> likely get invoked before your callback is called (its not really
> documented when it is invoked), so you might have to cope with multiple
> invocations (or too few), but in general, you should be able to cope with
> manual reschedule mode.

Yes, excellent. I can work with this.

Just out of curiosity, from my first email:
> > I guess in the latter case, I will need to detect a time jump backwards
> > by, for instance, comparing the return value of ev_now() with the return
> > value of ev_now() from a previous event loop iteration in an ev_check
> > watcher.

could this method still be used to detect jumps backwards (assuming the time 
until an event triggers the next event loop iteration won't cancel out  this 
jump backwards)?

Another question:
Assuming it is now
I start a periodic to fire at
then I set system time to

I guess there's not really a way to wake up poll() on a system time jump, and 
on this jump the periodics still need to be rescheduled.
You said something about a 60sec accuracy of libev for detecting time jumps. 
Is there some internal ev_timer watcher waking up the event loop every 60sec 
to catch this?

Best regards,
Thilo Schulz

More information about the libev mailing list