X-Git-Url: https://git.llucax.com/software/libev.git/blobdiff_plain/924ae10c0376cdb4b581d30f7b8a258b6b9e4853..0a7b0ac074d9987c6fa23ab20be4842e4514c7b9:/ev.html diff --git a/ev.html b/ev.html index bb85c18..d95bffd 100644 --- a/ev.html +++ b/ev.html @@ -6,7 +6,7 @@ - + @@ -135,12 +135,13 @@ watcher.

FEATURES

-

Libev supports select, poll, the linux-specific epoll, the -bsd-specific kqueue and the solaris-specific event port mechanisms -for file descriptor events (ev_io), relative timers (ev_timer), -absolute timers with customised rescheduling (ev_periodic), synchronous -signals (ev_signal), process status change events (ev_child), and -event watchers dealing with the event loop mechanism itself (ev_idle, +

Libev supports select, poll, the Linux-specific epoll, the +BSD-specific kqueue and the Solaris-specific event port mechanisms +for file descriptor events (ev_io), the Linux inotify interface +(for ev_stat), relative timers (ev_timer), absolute timers +with customised rescheduling (ev_periodic), synchronous signals +(ev_signal), process status change events (ev_child), and event +watchers dealing with the event loop mechanism itself (ev_idle, ev_embed, ev_prepare and ev_check watchers) as well as file watchers (ev_stat) and even limited support for fork events (ev_fork).

@@ -231,13 +232,14 @@ might be supported on the current system, you would need to look at recommended ones.

See the description of ev_embed watchers for more info.

-
ev_set_allocator (void *(*cb)(void *ptr, size_t size))
+
ev_set_allocator (void *(*cb)(void *ptr, long size))
-

Sets the allocation function to use (the prototype and semantics are -identical to the realloc C function). It is used to allocate and free -memory (no surprises here). If it returns zero when memory needs to be -allocated, the library might abort or take some potentially destructive -action. The default is your system realloc function.

+

Sets the allocation function to use (the prototype is similar - the +semantics is identical - to the realloc C function). It is used to +allocate and free memory (no surprises here). If it returns zero when +memory needs to be allocated, the library might abort or take some +potentially destructive action. The default is your system realloc +function.

You could override this function in high-availability programs to, say, free some memory if it cannot allocate memory, to use a special allocator, or even to sleep a while and retry until some memory is available.

@@ -325,6 +327,23 @@ or setgid) then libev will not look at the environment variable override the flags completely if it is found in the environment. This is useful to try out specific backends to test their performance, or to work around bugs.

+
+
EVFLAG_FORKCHECK
+
+

Instead of calling ev_default_fork or ev_loop_fork manually after +a fork, you can also make libev check for a fork in each iteration by +enabling this flag.

+

This works by calling getpid () on every iteration of the loop, +and thus this might slow down your event loop if you do a lot of loop +iterations and little real work, but is usually not noticable (on my +Linux system for example, getpid is actually a simple 5-insn sequence +without a syscall and thus very fast, but my Linux system also has +pthread_atfork which is even faster).

+

The big advantage of this flag is that you can forget about fork (and +forget about forgetting to tell libev about forking) when you use this +flag.

+

This flag setting cannot be overriden or specified in the LIBEV_FLAGS +environment variable.

EVBACKEND_SELECT (value 1, portable select backend)
@@ -743,7 +762,7 @@ 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).

-
callback = ev_cb (ev_TYPE *watcher)
+
callback ev_cb (ev_TYPE *watcher)

Returns the callback currently set on the watcher.

@@ -785,13 +804,41 @@ can cast it back to your own type:

} -

More interesting and less C-conformant ways of catsing your callback type -have been omitted....

+

More interesting and less C-conformant ways of casting your callback type +instead have been omitted.

+

Another common scenario is having some data structure with multiple +watchers:

+
  struct my_biggy
+  {
+    int some_data;
+    ev_timer t1;
+    ev_timer t2;
+  }
 
+
+

In this case getting the pointer to my_biggy is a bit more complicated, +you need to use offsetof:

+
  #include <stddef.h>
 
+  static void
+  t1_cb (EV_P_ struct ev_timer *w, int revents)
+  {
+    struct my_biggy big = (struct my_biggy *
+      (((char *)w) - offsetof (struct my_biggy, t1));
+  }
 
+  static void
+  t2_cb (EV_P_ struct ev_timer *w, int revents)
+  {
+    struct my_biggy big = (struct my_biggy *
+      (((char *)w) - offsetof (struct my_biggy, t2));
+  }
 
 
+
+
+
+

WATCHER TYPES

@@ -924,20 +971,21 @@ timer will not fire more than once per event loop iteration.

This will act as if the timer timed out and restart it again if it is repeating. The exact semantics are:

-

If the timer is started but nonrepeating, stop it.

-

If the timer is repeating, either start it if necessary (with the repeat -value), or reset the running timer to the repeat value.

+

If the timer is pending, its pending status is cleared.

+

If the timer is started but nonrepeating, stop it (as if it timed out).

+

If the timer is repeating, either start it if necessary (with the +repeat value), or reset the running timer to the repeat value.

This sounds a bit complicated, but here is a useful and typical -example: Imagine you have a tcp connection and you want a so-called -idle timeout, that is, you want to be called when there have been, -say, 60 seconds of inactivity on the socket. The easiest way to do -this is to configure an ev_timer with after=repeat=60 and calling +example: Imagine you have a tcp connection and you want a so-called idle +timeout, that is, you want to be called when there have been, say, 60 +seconds of inactivity on the socket. The easiest way to do this is to +configure an ev_timer with a repeat value of 60 and then call ev_timer_again each time you successfully read or write some data. If you go into an idle state where you do not expect data to travel on the -socket, you can stop the timer, and again will automatically restart it if -need be.

-

You can also ignore the after value and ev_timer_start altogether -and only ever use the repeat value:

+socket, you can ev_timer_stop the timer, and ev_timer_again will +automatically restart it if need be.

+

That means you can ignore the after value and ev_timer_start +altogether and only ever use the repeat value and ev_timer_again:

   ev_timer_init (timer, callback, 0., 5.);
    ev_timer_again (loop, timer);
    ...
@@ -948,8 +996,8 @@ and only ever use the repeat value:

ev_timer_again (loop, timer);
-

This is more efficient then stopping/starting the timer eahc time you want -to modify its timeout value.

+

This is more slightly efficient then stopping/starting the timer each time +you want to modify its timeout value.

ev_tstamp repeat [read-write]
@@ -1220,8 +1268,10 @@ not exist" is a status change like any other. The condition "path does not exist" is signified by the st_nlink field being zero (which is otherwise always forced to be at least one) and all the other fields of the stat buffer having unspecified contents.

+

The path should be absolute and must not end in a slash. If it is +relative and your working directory changes, the behaviour is undefined.

Since there is no standard to do this, the portable implementation simply -calls stat (2) regulalry on the path to see if it changed somehow. You +calls stat (2) regularly on the path to see if it changed somehow. You can specify a recommended polling interval for this case. If you specify a polling interval of 0 (highly recommended!) then a suitable, unspecified default value will be used (which you can expect to be around @@ -1231,8 +1281,13 @@ usually overkill.

This watcher type is not meant for massive numbers of stat watchers, as even with OS-supported change notifications, this can be resource-intensive.

-

At the time of this writing, no specific OS backends are implemented, but -if demand increases, at least a kqueue and inotify backend will be added.

+

At the time of this writing, only the Linux inotify interface is +implemented (implementing kqueue support is left as an exercise for the +reader). Inotify will be used to give hints only and should not change the +semantics of ev_stat watchers, which means that libev sometimes needs +to fall back to regular polling again even with inotify, but changes are +usually detected immediately, and if the file exists there will be no +polling.

ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)
ev_stat_set (ev_stat *, const char *path, ev_tstamp interval)
@@ -1786,8 +1841,9 @@ suitable for use with EV_A.

loop, if multiple loops are supported ("ev loop default").

-

Example: Declare and initialise a check watcher, working regardless of -wether multiple loops are supported or not.

+

Example: Declare and initialise a check watcher, utilising the above +macros so it will work regardless of wether multiple loops are supported +or not.

  static void
   check_cb (EV_P_ ev_timer *w, int revents)
   {
@@ -1799,9 +1855,6 @@ wether multiple loops are supported or not.

ev_check_start (EV_DEFAULT_ &check); ev_loop (EV_DEFAULT_ 0); - - -
@@ -1852,7 +1905,7 @@ in your include path (e.g. in libev/ when using -Ilibev):

ev_win32.c required on win32 platforms only - ev_select.c only when select backend is enabled (which is by default) + ev_select.c only when select backend is enabled (which is enabled by default) ev_poll.c only when poll backend is enabled (disabled by default) ev_epoll.c only when the epoll backend is enabled (disabled by default) ev_kqueue.c only when the kqueue backend is enabled (disabled by default) @@ -1989,6 +2042,12 @@ backend for Solaris 10 systems.

reserved for future expansion, works like the USE symbols above.

+
EV_USE_INOTIFY
+
+

If defined to be 1, libev will compile in support for the Linux inotify +interface to speed up ev_stat watchers. Its actual availability will +be detected at runtime.

+
EV_H

The name of the ev.h header file used to include it. The default if @@ -2053,7 +2112,15 @@ some inlining decisions, saves roughly 30% codesize of amd64.

ev_child watchers use a small hash table to distribute workload by pid. The default size is 16 (or 1 with EV_MINIMAL), usually more than enough. If you need to manage thousands of children you might want to -increase this value.

+increase this value (must be a power of two).

+
+
EV_INOTIFY_HASHSIZE
+
+

ev_staz watchers use a small hash table to distribute workload by +inotify watch id. The default size is 16 (or 1 with EV_MINIMAL), +usually more than enough. If you need to manage thousands of ev_stat +watchers you might want to increase this value (must be a power of +two).

EV_COMMON
@@ -2090,11 +2157,16 @@ interface) and EV.xs (implementation) files. Only the EV.xs

The usage in rxvt-unicode is simpler. It has a ev_cpp.h header file -that everybody includes and which overrides some autoconf choices:

-
  #define EV_USE_POLL 0
+that everybody includes and which overrides some configure choices:

+
  #define EV_MINIMAL 1
+  #define EV_USE_POLL 0
   #define EV_MULTIPLICITY 0
-  #define EV_PERIODICS 0
+  #define EV_PERIODIC_ENABLE 0
+  #define EV_STAT_ENABLE 0
+  #define EV_FORK_ENABLE 0
   #define EV_CONFIG_H <config.h>
+  #define EV_MINPRI 0
+  #define EV_MAXPRI 0
 
   #include "ev++.h"
 
@@ -2120,7 +2192,7 @@ documentation for ev_default_init.

Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)
Starting io/check/prepare/idle/signal/child watchers: O(1)
Stopping check/prepare/idle watchers: O(1)
-
Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % 16))
+
Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))
Finding the next timer per loop iteration: O(1)
Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)
Activating one watcher: O(1)