X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/7f61bc3d979ef53b867172664694f8fcf9e5bdd0..4179355c2b48ffa197ae4cf21b521ccf0201436c:/ev.html?ds=inline diff --git a/ev.html b/ev.html index efd79da..f63538f 100644 --- a/ev.html +++ b/ev.html @@ -6,7 +6,7 @@ - + @@ -187,13 +187,15 @@ you actually want to know.

int ev_version_major ()
int ev_version_minor ()
-

You can find out the major and minor version numbers of the library +

You can find out the major and minor ABI version numbers of the library you linked against by calling the functions ev_version_major and ev_version_minor. If you want, you can compare against the global symbols EV_VERSION_MAJOR and EV_VERSION_MINOR, which specify the version of the library your program was compiled against.

+

These version numbers refer to the ABI version of the library, not the +release version.

Usually, it's a good idea to terminate if the major versions mismatch, -as this indicates an incompatible change. Minor versions are usually +as this indicates an incompatible change. Minor versions are usually compatible to older versions, so a larger minor version alone is usually not a problem.

Example: Make sure we haven't accidentally been linked against the wrong @@ -531,8 +533,9 @@ external event in conjunction with something not expressible using other libev watchers. However, a pair of ev_prepare/ev_check watchers is usually a better approach for this kind of thing.

Here are the gory details of what ev_loop does:

-
   * If there are no active watchers (reference count is zero), return.
-   - Queue prepare watchers and then call all outstanding watchers.
+
   - Before the first iteration, call any pending watchers.
+   * If there are no active watchers (reference count is zero), return.
+   - Queue all prepare watchers and then call all outstanding watchers.
    - If we have been forked, recreate the kernel state.
    - Update the kernel state with all outstanding changes.
    - Update the "event loop time".
@@ -771,8 +774,9 @@ it.

Returns a true value iff the watcher is pending, (i.e. it has outstanding events but its callback has not yet been invoked). As long as a watcher is pending (but not active) you must not call an init function on it (but -ev_TYPE_set is safe) and you must make sure the watcher is available to -libev (e.g. you cnanot free () it).

+ev_TYPE_set is safe), you must not change its priority, and you must +make sure the watcher is available to libev (e.g. you cannot free () +it).

callback ev_cb (ev_TYPE *watcher)
@@ -797,12 +801,26 @@ example, to reduce latency after idling, or more often, to bind two watchers on the same event and make sure one is called first.

If you need to suppress invocation when higher priority events are pending you need to look at ev_idle watchers, which provide this functionality.

+

You must not change the priority of a watcher as long as it is active or +pending.

The default priority used by watchers when no priority has been set is always 0, which is supposed to not be too high and not be too low :).

Setting a priority outside the range of EV_MINPRI to EV_MAXPRI is fine, as long as you do not mind that the priority value you query might or might not have been adjusted to be within valid range.

+
ev_invoke (loop, ev_TYPE *watcher, int revents)
+
+

Invoke the watcher with the given loop and revents. Neither +loop nor revents need to be valid as long as the watcher callback +can deal with that fact.

+
+
int ev_clear_pending (loop, ev_TYPE *watcher)
+
+

If the watcher is pending, this function returns clears its pending status +and returns its revents bitset (as if its callback was invoked). If the +watcher isn't pending it does nothing and returns 0.

+
@@ -1083,10 +1101,10 @@ to trigger "at" some specific point in time. For example, if you tell periodic watcher to trigger in 10 seconds (by specifiying e.g. ev_now () + 10.) and then reset your system clock to the last year, then it will take a year to trigger the event (unlike an ev_timer, which would trigger -roughly 10 seconds later and of course not if you reset your system time -again).

+roughly 10 seconds later).

They can also be used to implement vastly more complex timers, such as -triggering an event on eahc midnight, local time.

+triggering an event on each midnight, local time or other, complicated, +rules.

As with timers, the callback is guarenteed to be invoked only when the time (at) has been passed, but if multiple periodic timers become ready during the same loop iteration then order of execution is undefined.

@@ -1098,18 +1116,18 @@ during the same loop iteration then order of execution is undefined.

operation, and we will explain them from simplest to complex:

-
* absolute timer (interval = reschedule_cb = 0)
+
* absolute timer (at = time, interval = reschedule_cb = 0)

In this configuration the watcher triggers an event at the wallclock time at and doesn't repeat. It will not adjust when a time jump occurs, that is, if it is to be run at January 1st 2011 then it will run when the system time reaches or surpasses this time.

-
* non-repeating interval timer (interval > 0, reschedule_cb = 0)
+
* non-repeating interval timer (at = offset, interval > 0, reschedule_cb = 0)

In this mode the watcher will always be scheduled to time out at the next -at + N * interval time (for some integer N) and then repeat, regardless -of any time jumps.

+at + N * interval time (for some integer N, which can also be negative) +and then repeat, regardless of any time jumps.

This can be used to create timers that do not drift with respect to system time:

   ev_periodic_set (&periodic, 0., 3600., 0);
@@ -1122,8 +1140,11 @@ by 3600.

Another way to think about it (for the mathematically inclined) is that ev_periodic will try to run the callback in this mode at the next possible time where time = at (mod interval), regardless of any time jumps.

+

For numerical stability it is preferable that the at value is near +ev_now () (the current time), but there is no range requirement for +this value.

-
* manual reschedule mode (reschedule_cb = callback)
+
* manual reschedule mode (at and interval ignored, reschedule_cb = callback)

In this mode the values for interval and at are both being ignored. Instead, each time the periodic watcher gets scheduled, the @@ -1132,7 +1153,7 @@ current time as second argument.

NOTE: This callback MUST NOT stop or destroy any periodic watcher, ever, or make any event loop modifications. If you need to stop it, return now + 1e30 (or so, fudge fudge) and stop it afterwards (e.g. by -starting a prepare watcher).

+starting an ev_prepare watcher, which is legal).

Its prototype is ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now), e.g.:

   static ev_tstamp my_rescheduler (struct ev_periodic *w, ev_tstamp now)
@@ -1162,6 +1183,13 @@ reason I omitted it as an example).

when you changed some parameters or the reschedule callback would return a different time than the last time it was called (e.g. in a crond like program when the crontabs have changed).

+
+
ev_tstamp offset [read-write]
+
+

When repeating, this contains the offset value, otherwise this is the +absolute point in time (the at value passed to ev_periodic_set).

+

Can be modified any time, but changes only take effect when the periodic +timer fires or ev_periodic_again is being called.

ev_tstamp interval [read-write]
@@ -1470,6 +1498,15 @@ with priority higher than or equal to the event loop and one coroutine of lower priority, but only once, using idle watchers to keep the event loop from blocking if lower-priority coroutines are active, thus mapping low-priority coroutines to idle/background tasks).

+

It is recommended to give ev_check watchers highest (EV_MAXPRI) +priority, to ensure that they are being run before any other watchers +after the poll. Also, ev_check watchers (and ev_prepare watchers, +too) should not activate ("feed") events into libev. While libev fully +supports this, they will be called before other ev_check watchers did +their job. As ev_check watchers are often used to embed other event +loops those other event loops might be in an unusable state until their +ev_check watcher ran (always remind yourself to coexist peacefully with +others).

ev_prepare_init (ev_prepare *, callback)
ev_check_init (ev_check *, callback)
@@ -1479,21 +1516,23 @@ parameters of any kind. There are ev_prepare_set and ev_check macros, but using them is utterly, utterly and completely pointless.

-

Example: To include a library such as adns, you would add IO watchers -and a timeout watcher in a prepare handler, as required by libadns, and -in a check watcher, destroy them and call into libadns. What follows is -pseudo-code only of course:

+

There are a number of principal ways to embed other event loops or modules +into libev. Here are some ideas on how to include libadns into libev +(there is a Perl module named EV::ADNS that does this, which you could +use for an actually working example. Another Perl module named EV::Glib +embeds a Glib main context into libev, and finally, Glib::EV embeds EV +into the Glib event loop).

+

Method 1: Add IO watchers and a timeout watcher in a prepare handler, +and in a check watcher, destroy them and call into libadns. What follows +is pseudo-code only of course. This requires you to either use a low +priority for the check watcher or use ev_clear_pending explicitly, as +the callbacks for the IO/timeout watchers might not have been called yet.

  static ev_io iow [nfd];
   static ev_timer tw;
 
   static void
   io_cb (ev_loop *loop, ev_io *w, int revents)
   {
-    // set the relevant poll flags
-    // could also call adns_processreadable etc. here
-    struct pollfd *fd = (struct pollfd *)w->data;
-    if (revents & EV_READ ) fd->revents |= fd->events & POLLIN;
-    if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT;
   }
 
   // create io watchers for each fd and a timer before blocking
@@ -1509,7 +1548,7 @@ pseudo-code only of course:

ev_timer_init (&tw, 0, timeout * 1e-3); ev_timer_start (loop, &tw); - // create on ev_io per pollfd + // create one ev_io per pollfd for (int i = 0; i < nfd; ++i) { ev_io_init (iow + i, io_cb, fds [i].fd, @@ -1517,7 +1556,6 @@ pseudo-code only of course:

| (fds [i].events & POLLOUT ? EV_WRITE : 0))); fds [i].revents = 0; - iow [i].data = fds + i; ev_io_start (loop, iow + i); } } @@ -1529,11 +1567,79 @@ pseudo-code only of course:

ev_timer_stop (loop, &tw); for (int i = 0; i < nfd; ++i) - ev_io_stop (loop, iow + i); + { + // set the relevant poll flags + // could also call adns_processreadable etc. here + struct pollfd *fd = fds + i; + int revents = ev_clear_pending (iow + i); + if (revents & EV_READ ) fd->revents |= fd->events & POLLIN; + if (revents & EV_WRITE) fd->revents |= fd->events & POLLOUT; + + // now stop the watcher + ev_io_stop (loop, iow + i); + } adns_afterpoll (adns, fds, nfd, timeval_from (ev_now (loop)); } +
+

Method 2: This would be just like method 1, but you run adns_afterpoll +in the prepare watcher and would dispose of the check watcher.

+

Method 3: If the module to be embedded supports explicit event +notification (adns does), you can also make use of the actual watcher +callbacks, and only destroy/create the watchers in the prepare watcher.

+
  static void
+  timer_cb (EV_P_ ev_timer *w, int revents)
+  {
+    adns_state ads = (adns_state)w->data;
+    update_now (EV_A);
+
+    adns_processtimeouts (ads, &tv_now);
+  }
+
+  static void
+  io_cb (EV_P_ ev_io *w, int revents)
+  {
+    adns_state ads = (adns_state)w->data;
+    update_now (EV_A);
+
+    if (revents & EV_READ ) adns_processreadable  (ads, w->fd, &tv_now);
+    if (revents & EV_WRITE) adns_processwriteable (ads, w->fd, &tv_now);
+  }
+
+  // do not ever call adns_afterpoll
+
+
+

Method 4: Do not use a prepare or check watcher because the module you +want to embed is too inflexible to support it. Instead, youc na override +their poll function. The drawback with this solution is that the main +loop is now no longer controllable by EV. The Glib::EV module does +this.

+
  static gint
+  event_poll_func (GPollFD *fds, guint nfds, gint timeout)
+  {
+    int got_events = 0;
+
+    for (n = 0; n < nfds; ++n)
+      // create/start io watcher that sets the relevant bits in fds[n] and increment got_events
+
+    if (timeout >= 0)
+      // create/start timer
+
+    // poll
+    ev_loop (EV_A_ 0);
+
+    // stop timer again
+    if (timeout >= 0)
+      ev_timer_stop (EV_A_ &to);
+
+    // stop io watchers again - their callbacks should have set
+    for (n = 0; n < nfds; ++n)
+      ev_io_stop (EV_A_ iow [n]);
+
+    return got_events;
+  }
+
 
 
 
@@ -1742,11 +1848,11 @@ the callback model to a model using method callbacks on objects.

of them macros) into the global namespace. All C++ specific things are put into the ev namespace. It should support all the same embedding options as ev.h, most notably EV_MULTIPLICITY.

-

Care has been taken to keep the overhead low. The only data member added -to the C-style watchers is the event loop the watcher is associated with -(or no additional members at all if you disable EV_MULTIPLICITY when -embedding libev).

-

Currently, functions and static and non-static member functions can be +

Care has been taken to keep the overhead low. The only data member the C++ +classes add (compared to plain C-style watchers) is the event loop pointer +that the watcher is associated with (or no additional members at all if +you disable EV_MULTIPLICITY when embedding libev).

+

Currently, functions, and static and non-static member functions can be used as callbacks. Other types should be easy to add as long as they only need one additional pointer for context. If you need support for other types of functors please contact the author (preferably after implementing @@ -1808,12 +1914,18 @@ thunking function, making it as fast as a direct C callback.

-
w->set (void (*function)(watcher &w, int), void *data = 0)
+
w->set<function> (void *data = 0)

Also sets a callback, but uses a static method or plain function as callback. The optional data argument will be stored in the watcher's data member and is free for you to use.

+

The prototype of the function must be void (*)(ev::TYPE &w, int).

See the method-set above for more details.

+

Example:

+
  static void io_cb (ev::io &w, int revents) { }
+  iow.set <io_cb> ();
+
+
w->set (struct ev_loop *)