… | |
… | |
2114 | |
2114 | |
2115 | =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) |
2115 | =item ev_timer_init (ev_timer *, callback, ev_tstamp after, ev_tstamp repeat) |
2116 | |
2116 | |
2117 | =item ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat) |
2117 | =item ev_timer_set (ev_timer *, ev_tstamp after, ev_tstamp repeat) |
2118 | |
2118 | |
2119 | Configure the timer to trigger after C<after> seconds. If C<repeat> |
2119 | Configure the timer to trigger after C<after> seconds (fractional and |
2120 | is C<0.>, then it will automatically be stopped once the timeout is |
2120 | negative values are supported). If C<repeat> is C<0.>, then it will |
2121 | reached. If it is positive, then the timer will automatically be |
2121 | automatically be stopped once the timeout is reached. If it is positive, |
2122 | configured to trigger again C<repeat> seconds later, again, and again, |
2122 | then the timer will automatically be configured to trigger again C<repeat> |
2123 | until stopped manually. |
2123 | seconds later, again, and again, until stopped manually. |
2124 | |
2124 | |
2125 | The timer itself will do a best-effort at avoiding drift, that is, if |
2125 | The timer itself will do a best-effort at avoiding drift, that is, if |
2126 | you configure a timer to trigger every 10 seconds, then it will normally |
2126 | you configure a timer to trigger every 10 seconds, then it will normally |
2127 | trigger at exactly 10 second intervals. If, however, your program cannot |
2127 | trigger at exactly 10 second intervals. If, however, your program cannot |
2128 | keep up with the timer (because it takes longer than those 10 seconds to |
2128 | keep up with the timer (because it takes longer than those 10 seconds to |
… | |
… | |
2225 | C<ev_timer>, which would still trigger roughly 10 seconds after starting |
2225 | C<ev_timer>, which would still trigger roughly 10 seconds after starting |
2226 | it, as it uses a relative timeout). |
2226 | it, as it uses a relative timeout). |
2227 | |
2227 | |
2228 | C<ev_periodic> watchers can also be used to implement vastly more complex |
2228 | C<ev_periodic> watchers can also be used to implement vastly more complex |
2229 | timers, such as triggering an event on each "midnight, local time", or |
2229 | timers, such as triggering an event on each "midnight, local time", or |
2230 | other complicated rules. This cannot be done with C<ev_timer> watchers, as |
2230 | other complicated rules. This cannot easily be done with C<ev_timer> |
2231 | those cannot react to time jumps. |
2231 | watchers, as those cannot react to time jumps. |
2232 | |
2232 | |
2233 | As with timers, the callback is guaranteed to be invoked only when the |
2233 | As with timers, the callback is guaranteed to be invoked only when the |
2234 | point in time where it is supposed to trigger has passed. If multiple |
2234 | point in time where it is supposed to trigger has passed. If multiple |
2235 | timers become ready during the same loop iteration then the ones with |
2235 | timers become ready during the same loop iteration then the ones with |
2236 | earlier time-out values are invoked before ones with later time-out values |
2236 | earlier time-out values are invoked before ones with later time-out values |
… | |
… | |
2322 | |
2322 | |
2323 | NOTE: I<< This callback must always return a time that is higher than or |
2323 | NOTE: I<< This callback must always return a time that is higher than or |
2324 | equal to the passed C<now> value >>. |
2324 | equal to the passed C<now> value >>. |
2325 | |
2325 | |
2326 | This can be used to create very complex timers, such as a timer that |
2326 | This can be used to create very complex timers, such as a timer that |
2327 | triggers on "next midnight, local time". To do this, you would calculate the |
2327 | triggers on "next midnight, local time". To do this, you would calculate |
2328 | next midnight after C<now> and return the timestamp value for this. How |
2328 | the next midnight after C<now> and return the timestamp value for |
2329 | you do this is, again, up to you (but it is not trivial, which is the main |
2329 | this. Here is a (completely untested, no error checking) example on how to |
2330 | reason I omitted it as an example). |
2330 | do this: |
|
|
2331 | |
|
|
2332 | #include <time.h> |
|
|
2333 | |
|
|
2334 | static ev_tstamp |
|
|
2335 | my_rescheduler (ev_periodic *w, ev_tstamp now) |
|
|
2336 | { |
|
|
2337 | time_t tnow = (time_t)now; |
|
|
2338 | struct tm tm; |
|
|
2339 | localtime_r (&tnow, &tm); |
|
|
2340 | |
|
|
2341 | tm.tm_sec = tm.tm_min = tm.tm_hour = 0; // midnight current day |
|
|
2342 | ++tm.tm_mday; // midnight next day |
|
|
2343 | |
|
|
2344 | return mktime (&tm); |
|
|
2345 | } |
|
|
2346 | |
|
|
2347 | Note: this code might run into trouble on days that have more then two |
|
|
2348 | midnights (beginning and end). |
2331 | |
2349 | |
2332 | =back |
2350 | =back |
2333 | |
2351 | |
2334 | =item ev_periodic_again (loop, ev_periodic *) |
2352 | =item ev_periodic_again (loop, ev_periodic *) |
2335 | |
2353 | |
… | |
… | |
3518 | |
3536 | |
3519 | There are some other functions of possible interest. Described. Here. Now. |
3537 | There are some other functions of possible interest. Described. Here. Now. |
3520 | |
3538 | |
3521 | =over 4 |
3539 | =over 4 |
3522 | |
3540 | |
3523 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback) |
3541 | =item ev_once (loop, int fd, int events, ev_tstamp timeout, callback, arg) |
3524 | |
3542 | |
3525 | This function combines a simple timer and an I/O watcher, calls your |
3543 | This function combines a simple timer and an I/O watcher, calls your |
3526 | callback on whichever event happens first and automatically stops both |
3544 | callback on whichever event happens first and automatically stops both |
3527 | watchers. This is useful if you want to wait for a single event on an fd |
3545 | watchers. This is useful if you want to wait for a single event on an fd |
3528 | or timeout without having to allocate/configure/start/stop/free one or |
3546 | or timeout without having to allocate/configure/start/stop/free one or |
… | |
… | |
3960 | The normal C API should work fine when used from C++: both ev.h and the |
3978 | The normal C API should work fine when used from C++: both ev.h and the |
3961 | libev sources can be compiled as C++. Therefore, code that uses the C API |
3979 | libev sources can be compiled as C++. Therefore, code that uses the C API |
3962 | will work fine. |
3980 | will work fine. |
3963 | |
3981 | |
3964 | Proper exception specifications might have to be added to callbacks passed |
3982 | Proper exception specifications might have to be added to callbacks passed |
3965 | to libev: exceptions may be thrown only from watcher callbacks, all |
3983 | to libev: exceptions may be thrown only from watcher callbacks, all other |
3966 | other callbacks (allocator, syserr, loop acquire/release and periodic |
3984 | callbacks (allocator, syserr, loop acquire/release and periodic reschedule |
3967 | reschedule callbacks) must not throw exceptions, and might need a C<throw |
3985 | callbacks) must not throw exceptions, and might need a C<noexcept> |
3968 | ()> specification. If you have code that needs to be compiled as both C |
3986 | specification. If you have code that needs to be compiled as both C and |
3969 | and C++ you can use the C<EV_THROW> macro for this: |
3987 | C++ you can use the C<EV_NOEXCEPT> macro for this: |
3970 | |
3988 | |
3971 | static void |
3989 | static void |
3972 | fatal_error (const char *msg) EV_THROW |
3990 | fatal_error (const char *msg) EV_NOEXCEPT |
3973 | { |
3991 | { |
3974 | perror (msg); |
3992 | perror (msg); |
3975 | abort (); |
3993 | abort (); |
3976 | } |
3994 | } |
3977 | |
3995 | |