… | |
… | |
196 | \& return 0; |
196 | \& return 0; |
197 | \& } |
197 | \& } |
198 | .Ve |
198 | .Ve |
199 | .SH "DESCRIPTION" |
199 | .SH "DESCRIPTION" |
200 | .IX Header "DESCRIPTION" |
200 | .IX Header "DESCRIPTION" |
|
|
201 | The newest version of this document is also available as a html-formatted |
|
|
202 | web page you might find easier to navigate when reading it for the first |
|
|
203 | time: <http://cvs.schmorp.de/libev/ev.html>. |
|
|
204 | .PP |
201 | Libev is an event loop: you register interest in certain events (such as a |
205 | Libev is an event loop: you register interest in certain events (such as a |
202 | file descriptor being readable or a timeout occuring), and it will manage |
206 | file descriptor being readable or a timeout occuring), and it will manage |
203 | these event sources and provide your program with events. |
207 | these event sources and provide your program with events. |
204 | .PP |
208 | .PP |
205 | To do this, it must take more or less complete control over your process |
209 | To do this, it must take more or less complete control over your process |
… | |
… | |
1890 | .PP |
1894 | .PP |
1891 | .Vb 1 |
1895 | .Vb 1 |
1892 | \& #include <ev++.h> |
1896 | \& #include <ev++.h> |
1893 | .Ve |
1897 | .Ve |
1894 | .PP |
1898 | .PP |
1895 | (it is not installed by default). This automatically includes \fIev.h\fR |
1899 | This automatically includes \fIev.h\fR and puts all of its definitions (many |
1896 | and puts all of its definitions (many of them macros) into the global |
1900 | of them macros) into the global namespace. All \*(C+ specific things are |
1897 | namespace. All \*(C+ specific things are put into the \f(CW\*(C`ev\*(C'\fR namespace. |
1901 | put into the \f(CW\*(C`ev\*(C'\fR namespace. It should support all the same embedding |
|
|
1902 | options as \fIev.h\fR, most notably \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. |
1898 | .PP |
1903 | .PP |
1899 | It should support all the same embedding options as \fIev.h\fR, most notably |
1904 | Care has been taken to keep the overhead low. The only data member added |
1900 | \&\f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. |
1905 | to the C\-style watchers is the event loop the watcher is associated with |
|
|
1906 | (or no additional members at all if you disable \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR when |
|
|
1907 | embedding libev). |
|
|
1908 | .PP |
|
|
1909 | Currently, functions and static and non-static member functions can be |
|
|
1910 | used as callbacks. Other types should be easy to add as long as they only |
|
|
1911 | need one additional pointer for context. If you need support for other |
|
|
1912 | types of functors please contact the author (preferably after implementing |
|
|
1913 | it). |
1901 | .PP |
1914 | .PP |
1902 | Here is a list of things available in the \f(CW\*(C`ev\*(C'\fR namespace: |
1915 | Here is a list of things available in the \f(CW\*(C`ev\*(C'\fR namespace: |
1903 | .ie n .IP """ev::READ""\fR, \f(CW""ev::WRITE"" etc." 4 |
1916 | .ie n .IP """ev::READ""\fR, \f(CW""ev::WRITE"" etc." 4 |
1904 | .el .IP "\f(CWev::READ\fR, \f(CWev::WRITE\fR etc." 4 |
1917 | .el .IP "\f(CWev::READ\fR, \f(CWev::WRITE\fR etc." 4 |
1905 | .IX Item "ev::READ, ev::WRITE etc." |
1918 | .IX Item "ev::READ, ev::WRITE etc." |
… | |
… | |
1917 | which is called \f(CW\*(C`ev::sig\*(C'\fR to avoid clashes with the \f(CW\*(C`signal\*(C'\fR macro |
1930 | which is called \f(CW\*(C`ev::sig\*(C'\fR to avoid clashes with the \f(CW\*(C`signal\*(C'\fR macro |
1918 | defines by many implementations. |
1931 | defines by many implementations. |
1919 | .Sp |
1932 | .Sp |
1920 | All of those classes have these methods: |
1933 | All of those classes have these methods: |
1921 | .RS 4 |
1934 | .RS 4 |
1922 | .IP "ev::TYPE::TYPE (object *, object::method *)" 4 |
1935 | .IP "ev::TYPE::TYPE ()" 4 |
1923 | .IX Item "ev::TYPE::TYPE (object *, object::method *)" |
1936 | .IX Item "ev::TYPE::TYPE ()" |
1924 | .PD 0 |
1937 | .PD 0 |
1925 | .IP "ev::TYPE::TYPE (object *, object::method *, struct ev_loop *)" 4 |
1938 | .IP "ev::TYPE::TYPE (struct ev_loop *)" 4 |
1926 | .IX Item "ev::TYPE::TYPE (object *, object::method *, struct ev_loop *)" |
1939 | .IX Item "ev::TYPE::TYPE (struct ev_loop *)" |
1927 | .IP "ev::TYPE::~TYPE" 4 |
1940 | .IP "ev::TYPE::~TYPE" 4 |
1928 | .IX Item "ev::TYPE::~TYPE" |
1941 | .IX Item "ev::TYPE::~TYPE" |
1929 | .PD |
1942 | .PD |
1930 | The constructor takes a pointer to an object and a method pointer to |
1943 | The constructor (optionally) takes an event loop to associate the watcher |
1931 | the event handler callback to call in this class. The constructor calls |
1944 | with. If it is omitted, it will use \f(CW\*(C`EV_DEFAULT\*(C'\fR. |
1932 | \&\f(CW\*(C`ev_init\*(C'\fR for you, which means you have to call the \f(CW\*(C`set\*(C'\fR method |
1945 | .Sp |
1933 | before starting it. If you do not specify a loop then the constructor |
1946 | The constructor calls \f(CW\*(C`ev_init\*(C'\fR for you, which means you have to call the |
1934 | automatically associates the default loop with this watcher. |
1947 | \&\f(CW\*(C`set\*(C'\fR method before starting it. |
|
|
1948 | .Sp |
|
|
1949 | It will not set a callback, however: You have to call the templated \f(CW\*(C`set\*(C'\fR |
|
|
1950 | method to set a callback before you can start the watcher. |
|
|
1951 | .Sp |
|
|
1952 | (The reason why you have to use a method is a limitation in \*(C+ which does |
|
|
1953 | not allow explicit template arguments for constructors). |
1935 | .Sp |
1954 | .Sp |
1936 | The destructor automatically stops the watcher if it is active. |
1955 | The destructor automatically stops the watcher if it is active. |
|
|
1956 | .IP "w\->set<class, &class::method> (object *)" 4 |
|
|
1957 | .IX Item "w->set<class, &class::method> (object *)" |
|
|
1958 | This method sets the callback method to call. The method has to have a |
|
|
1959 | signature of \f(CW\*(C`void (*)(ev_TYPE &, int)\*(C'\fR, it receives the watcher as |
|
|
1960 | first argument and the \f(CW\*(C`revents\*(C'\fR as second. The object must be given as |
|
|
1961 | parameter and is stored in the \f(CW\*(C`data\*(C'\fR member of the watcher. |
|
|
1962 | .Sp |
|
|
1963 | This method synthesizes efficient thunking code to call your method from |
|
|
1964 | the C callback that libev requires. If your compiler can inline your |
|
|
1965 | callback (i.e. it is visible to it at the place of the \f(CW\*(C`set\*(C'\fR call and |
|
|
1966 | your compiler is good :), then the method will be fully inlined into the |
|
|
1967 | thunking function, making it as fast as a direct C callback. |
|
|
1968 | .Sp |
|
|
1969 | Example: simple class declaration and watcher initialisation |
|
|
1970 | .Sp |
|
|
1971 | .Vb 4 |
|
|
1972 | \& struct myclass |
|
|
1973 | \& { |
|
|
1974 | \& void io_cb (ev::io &w, int revents) { } |
|
|
1975 | \& } |
|
|
1976 | .Ve |
|
|
1977 | .Sp |
|
|
1978 | .Vb 3 |
|
|
1979 | \& myclass obj; |
|
|
1980 | \& ev::io iow; |
|
|
1981 | \& iow.set <myclass, &myclass::io_cb> (&obj); |
|
|
1982 | .Ve |
|
|
1983 | .IP "w\->set (void (*function)(watcher &w, int), void *data = 0)" 4 |
|
|
1984 | .IX Item "w->set (void (*function)(watcher &w, int), void *data = 0)" |
|
|
1985 | Also sets a callback, but uses a static method or plain function as |
|
|
1986 | callback. The optional \f(CW\*(C`data\*(C'\fR argument will be stored in the watcher's |
|
|
1987 | \&\f(CW\*(C`data\*(C'\fR member and is free for you to use. |
|
|
1988 | .Sp |
|
|
1989 | See the method\-\f(CW\*(C`set\*(C'\fR above for more details. |
1937 | .IP "w\->set (struct ev_loop *)" 4 |
1990 | .IP "w\->set (struct ev_loop *)" 4 |
1938 | .IX Item "w->set (struct ev_loop *)" |
1991 | .IX Item "w->set (struct ev_loop *)" |
1939 | Associates a different \f(CW\*(C`struct ev_loop\*(C'\fR with this watcher. You can only |
1992 | Associates a different \f(CW\*(C`struct ev_loop\*(C'\fR with this watcher. You can only |
1940 | do this when the watcher is inactive (and not pending either). |
1993 | do this when the watcher is inactive (and not pending either). |
1941 | .IP "w\->set ([args])" 4 |
1994 | .IP "w\->set ([args])" 4 |
1942 | .IX Item "w->set ([args])" |
1995 | .IX Item "w->set ([args])" |
1943 | Basically the same as \f(CW\*(C`ev_TYPE_set\*(C'\fR, with the same args. Must be |
1996 | Basically the same as \f(CW\*(C`ev_TYPE_set\*(C'\fR, with the same args. Must be |
1944 | called at least once. Unlike the C counterpart, an active watcher gets |
1997 | called at least once. Unlike the C counterpart, an active watcher gets |
1945 | automatically stopped and restarted. |
1998 | automatically stopped and restarted when reconfiguring it with this |
|
|
1999 | method. |
1946 | .IP "w\->start ()" 4 |
2000 | .IP "w\->start ()" 4 |
1947 | .IX Item "w->start ()" |
2001 | .IX Item "w->start ()" |
1948 | Starts the watcher. Note that there is no \f(CW\*(C`loop\*(C'\fR argument as the |
2002 | Starts the watcher. Note that there is no \f(CW\*(C`loop\*(C'\fR argument, as the |
1949 | constructor already takes the loop. |
2003 | constructor already stores the event loop. |
1950 | .IP "w\->stop ()" 4 |
2004 | .IP "w\->stop ()" 4 |
1951 | .IX Item "w->stop ()" |
2005 | .IX Item "w->stop ()" |
1952 | Stops the watcher if it is active. Again, no \f(CW\*(C`loop\*(C'\fR argument. |
2006 | Stops the watcher if it is active. Again, no \f(CW\*(C`loop\*(C'\fR argument. |
1953 | .ie n .IP "w\->again () ""ev::timer""\fR, \f(CW""ev::periodic"" only" 4 |
2007 | .ie n .IP "w\->again () ""ev::timer""\fR, \f(CW""ev::periodic"" only" 4 |
1954 | .el .IP "w\->again () \f(CWev::timer\fR, \f(CWev::periodic\fR only" 4 |
2008 | .el .IP "w\->again () \f(CWev::timer\fR, \f(CWev::periodic\fR only" 4 |
… | |
… | |
1980 | .Vb 2 |
2034 | .Vb 2 |
1981 | \& myclass (); |
2035 | \& myclass (); |
1982 | \& } |
2036 | \& } |
1983 | .Ve |
2037 | .Ve |
1984 | .PP |
2038 | .PP |
1985 | .Vb 6 |
2039 | .Vb 4 |
1986 | \& myclass::myclass (int fd) |
2040 | \& myclass::myclass (int fd) |
1987 | \& : io (this, &myclass::io_cb), |
|
|
1988 | \& idle (this, &myclass::idle_cb) |
|
|
1989 | \& { |
2041 | \& { |
|
|
2042 | \& io .set <myclass, &myclass::io_cb > (this); |
|
|
2043 | \& idle.set <myclass, &myclass::idle_cb> (this); |
|
|
2044 | .Ve |
|
|
2045 | .PP |
|
|
2046 | .Vb 2 |
1990 | \& io.start (fd, ev::READ); |
2047 | \& io.start (fd, ev::READ); |
1991 | \& } |
2048 | \& } |
1992 | .Ve |
2049 | .Ve |
1993 | .SH "MACRO MAGIC" |
2050 | .SH "MACRO MAGIC" |
1994 | .IX Header "MACRO MAGIC" |
2051 | .IX Header "MACRO MAGIC" |
… | |
… | |
2274 | If undefined or defined to \f(CW1\fR, then all event-loop-specific functions |
2331 | If undefined or defined to \f(CW1\fR, then all event-loop-specific functions |
2275 | will have the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument, and you can create |
2332 | will have the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument, and you can create |
2276 | additional independent event loops. Otherwise there will be no support |
2333 | additional independent event loops. Otherwise there will be no support |
2277 | for multiple event loops and there is no first event loop pointer |
2334 | for multiple event loops and there is no first event loop pointer |
2278 | argument. Instead, all functions act on the single default loop. |
2335 | argument. Instead, all functions act on the single default loop. |
|
|
2336 | .IP "\s-1EV_MINPRI\s0" 4 |
|
|
2337 | .IX Item "EV_MINPRI" |
|
|
2338 | .PD 0 |
|
|
2339 | .IP "\s-1EV_MAXPRI\s0" 4 |
|
|
2340 | .IX Item "EV_MAXPRI" |
|
|
2341 | .PD |
|
|
2342 | The range of allowed priorities. \f(CW\*(C`EV_MINPRI\*(C'\fR must be smaller or equal to |
|
|
2343 | \&\f(CW\*(C`EV_MAXPRI\*(C'\fR, but otherwise there are no non-obvious limitations. You can |
|
|
2344 | provide for more priorities by overriding those symbols (usually defined |
|
|
2345 | to be \f(CW\*(C`\-2\*(C'\fR and \f(CW2\fR, respectively). |
|
|
2346 | .Sp |
|
|
2347 | When doing priority-based operations, libev usually has to linearly search |
|
|
2348 | all the priorities, so having many of them (hundreds) uses a lot of space |
|
|
2349 | and time, so using the defaults of five priorities (\-2 .. +2) is usually |
|
|
2350 | fine. |
|
|
2351 | .Sp |
|
|
2352 | If your embedding app does not need any priorities, defining these both to |
|
|
2353 | \&\f(CW0\fR will save some memory and cpu. |
2279 | .IP "\s-1EV_PERIODIC_ENABLE\s0" 4 |
2354 | .IP "\s-1EV_PERIODIC_ENABLE\s0" 4 |
2280 | .IX Item "EV_PERIODIC_ENABLE" |
2355 | .IX Item "EV_PERIODIC_ENABLE" |
2281 | If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If |
2356 | If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If |
2282 | defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of |
2357 | defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of |
2283 | code. |
2358 | code. |
… | |
… | |
2382 | .SH "COMPLEXITIES" |
2457 | .SH "COMPLEXITIES" |
2383 | .IX Header "COMPLEXITIES" |
2458 | .IX Header "COMPLEXITIES" |
2384 | In this section the complexities of (many of) the algorithms used inside |
2459 | In this section the complexities of (many of) the algorithms used inside |
2385 | libev will be explained. For complexity discussions about backends see the |
2460 | libev will be explained. For complexity discussions about backends see the |
2386 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
2461 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
|
|
2462 | .Sp |
|
|
2463 | All of the following are about amortised time: If an array needs to be |
|
|
2464 | extended, libev needs to realloc and move the whole array, but this |
|
|
2465 | happens asymptotically never with higher number of elements, so O(1) might |
|
|
2466 | mean it might do a lengthy realloc operation in rare cases, but on average |
|
|
2467 | it is much faster and asymptotically approaches constant time. |
2387 | .RS 4 |
2468 | .RS 4 |
2388 | .IP "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" 4 |
2469 | .IP "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" 4 |
2389 | .IX Item "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" |
2470 | .IX Item "Starting and stopping timer/periodic watchers: O(log skipped_other_timers)" |
2390 | .PD 0 |
2471 | This means that, when you have a watcher that triggers in one hour and |
|
|
2472 | there are 100 watchers that would trigger before that then inserting will |
|
|
2473 | have to skip those 100 watchers. |
2391 | .IP "Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)" 4 |
2474 | .IP "Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)" 4 |
2392 | .IX Item "Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)" |
2475 | .IX Item "Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)" |
|
|
2476 | That means that for changing a timer costs less than removing/adding them |
|
|
2477 | as only the relative motion in the event queue has to be paid for. |
2393 | .IP "Starting io/check/prepare/idle/signal/child watchers: O(1)" 4 |
2478 | .IP "Starting io/check/prepare/idle/signal/child watchers: O(1)" 4 |
2394 | .IX Item "Starting io/check/prepare/idle/signal/child watchers: O(1)" |
2479 | .IX Item "Starting io/check/prepare/idle/signal/child watchers: O(1)" |
2395 | .IP "Stopping check/prepare/idle watchers: O(1)" 4 |
2480 | These just add the watcher into an array or at the head of a list. |
2396 | .IX Item "Stopping check/prepare/idle watchers: O(1)" |
2481 | =item Stopping check/prepare/idle watchers: O(1) |
2397 | .IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4 |
2482 | .IP "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % \s-1EV_PID_HASHSIZE\s0))" 4 |
2398 | .IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))" |
2483 | .IX Item "Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))" |
|
|
2484 | These watchers are stored in lists then need to be walked to find the |
|
|
2485 | correct watcher to remove. The lists are usually short (you don't usually |
|
|
2486 | have many watchers waiting for the same fd or signal). |
2399 | .IP "Finding the next timer per loop iteration: O(1)" 4 |
2487 | .IP "Finding the next timer per loop iteration: O(1)" 4 |
2400 | .IX Item "Finding the next timer per loop iteration: O(1)" |
2488 | .IX Item "Finding the next timer per loop iteration: O(1)" |
|
|
2489 | .PD 0 |
2401 | .IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 |
2490 | .IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 |
2402 | .IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" |
2491 | .IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" |
|
|
2492 | .PD |
|
|
2493 | A change means an I/O watcher gets started or stopped, which requires |
|
|
2494 | libev to recalculate its status (and possibly tell the kernel). |
2403 | .IP "Activating one watcher: O(1)" 4 |
2495 | .IP "Activating one watcher: O(1)" 4 |
2404 | .IX Item "Activating one watcher: O(1)" |
2496 | .IX Item "Activating one watcher: O(1)" |
|
|
2497 | .PD 0 |
|
|
2498 | .IP "Priority handling: O(number_of_priorities)" 4 |
|
|
2499 | .IX Item "Priority handling: O(number_of_priorities)" |
|
|
2500 | .PD |
|
|
2501 | Priorities are implemented by allocating some space for each |
|
|
2502 | priority. When doing priority-based operations, libev usually has to |
|
|
2503 | linearly search all the priorities. |
2405 | .RE |
2504 | .RE |
2406 | .RS 4 |
2505 | .RS 4 |
2407 | .PD |
|
|
2408 | .SH "AUTHOR" |
2506 | .SH "AUTHOR" |
2409 | .IX Header "AUTHOR" |
2507 | .IX Header "AUTHOR" |
2410 | Marc Lehmann <libev@schmorp.de>. |
2508 | Marc Lehmann <libev@schmorp.de>. |