ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.3
(Generate patch)

Comparing libev/ev.3 (file contents):
Revision 1.38 by root, Fri Dec 7 18:09:38 2007 UTC vs.
Revision 1.42 by root, Fri Dec 7 20:19:16 2007 UTC

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"
201The newest version of this document is also available as a html-formatted
202web page you might find easier to navigate when reading it for the first
203time: <http://cvs.schmorp.de/libev/ev.html>.
204.PP
201Libev is an event loop: you register interest in certain events (such as a 205Libev is an event loop: you register interest in certain events (such as a
202file descriptor being readable or a timeout occuring), and it will manage 206file descriptor being readable or a timeout occuring), and it will manage
203these event sources and provide your program with events. 207these event sources and provide your program with events.
204.PP 208.PP
205To do this, it must take more or less complete control over your process 209To 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 1899This automatically includes \fIev.h\fR and puts all of its definitions (many
1896and puts all of its definitions (many of them macros) into the global 1900of them macros) into the global namespace. All \*(C+ specific things are
1897namespace. All \*(C+ specific things are put into the \f(CW\*(C`ev\*(C'\fR namespace. 1901put into the \f(CW\*(C`ev\*(C'\fR namespace. It should support all the same embedding
1902options as \fIev.h\fR, most notably \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR.
1898.PP 1903.PP
1899It should support all the same embedding options as \fIev.h\fR, most notably 1904Care has been taken to keep the overhead low. The only data member the \*(C+
1900\&\f(CW\*(C`EV_MULTIPLICITY\*(C'\fR. 1905classes add (compared to plain C\-style watchers) is the event loop pointer
1906that the watcher is associated with (or no additional members at all if
1907you disable \f(CW\*(C`EV_MULTIPLICITY\*(C'\fR when embedding libev).
1908.PP
1909Currently, functions, and static and non-static member functions can be
1910used as callbacks. Other types should be easy to add as long as they only
1911need one additional pointer for context. If you need support for other
1912types of functors please contact the author (preferably after implementing
1913it).
1901.PP 1914.PP
1902Here is a list of things available in the \f(CW\*(C`ev\*(C'\fR namespace: 1915Here 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."
1917which is called \f(CW\*(C`ev::sig\*(C'\fR to avoid clashes with the \f(CW\*(C`signal\*(C'\fR macro 1930which is called \f(CW\*(C`ev::sig\*(C'\fR to avoid clashes with the \f(CW\*(C`signal\*(C'\fR macro
1918defines by many implementations. 1931defines by many implementations.
1919.Sp 1932.Sp
1920All of those classes have these methods: 1933All 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
1930The constructor takes a pointer to an object and a method pointer to 1943The constructor (optionally) takes an event loop to associate the watcher
1931the event handler callback to call in this class. The constructor calls 1944with. 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
1933before starting it. If you do not specify a loop then the constructor 1946The constructor calls \f(CW\*(C`ev_init\*(C'\fR for you, which means you have to call the
1934automatically associates the default loop with this watcher. 1947\&\f(CW\*(C`set\*(C'\fR method before starting it.
1948.Sp
1949It will not set a callback, however: You have to call the templated \f(CW\*(C`set\*(C'\fR
1950method 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
1953not allow explicit template arguments for constructors).
1935.Sp 1954.Sp
1936The destructor automatically stops the watcher if it is active. 1955The 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 *)"
1958This method sets the callback method to call. The method has to have a
1959signature of \f(CW\*(C`void (*)(ev_TYPE &, int)\*(C'\fR, it receives the watcher as
1960first argument and the \f(CW\*(C`revents\*(C'\fR as second. The object must be given as
1961parameter and is stored in the \f(CW\*(C`data\*(C'\fR member of the watcher.
1962.Sp
1963This method synthesizes efficient thunking code to call your method from
1964the C callback that libev requires. If your compiler can inline your
1965callback (i.e. it is visible to it at the place of the \f(CW\*(C`set\*(C'\fR call and
1966your compiler is good :), then the method will be fully inlined into the
1967thunking function, making it as fast as a direct C callback.
1968.Sp
1969Example: 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)"
1985Also sets a callback, but uses a static method or plain function as
1986callback. 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
1989See 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 *)"
1939Associates a different \f(CW\*(C`struct ev_loop\*(C'\fR with this watcher. You can only 1992Associates a different \f(CW\*(C`struct ev_loop\*(C'\fR with this watcher. You can only
1940do this when the watcher is inactive (and not pending either). 1993do 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])"
1943Basically the same as \f(CW\*(C`ev_TYPE_set\*(C'\fR, with the same args. Must be 1996Basically the same as \f(CW\*(C`ev_TYPE_set\*(C'\fR, with the same args. Must be
1944called at least once. Unlike the C counterpart, an active watcher gets 1997called at least once. Unlike the C counterpart, an active watcher gets
1945automatically stopped and restarted. 1998automatically stopped and restarted when reconfiguring it with this
1999method.
1946.IP "w\->start ()" 4 2000.IP "w\->start ()" 4
1947.IX Item "w->start ()" 2001.IX Item "w->start ()"
1948Starts the watcher. Note that there is no \f(CW\*(C`loop\*(C'\fR argument as the 2002Starts the watcher. Note that there is no \f(CW\*(C`loop\*(C'\fR argument, as the
1949constructor already takes the loop. 2003constructor 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 ()"
1952Stops the watcher if it is active. Again, no \f(CW\*(C`loop\*(C'\fR argument. 2006Stops 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"
2274If undefined or defined to \f(CW1\fR, then all event-loop-specific functions 2331If undefined or defined to \f(CW1\fR, then all event-loop-specific functions
2275will have the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument, and you can create 2332will have the \f(CW\*(C`struct ev_loop *\*(C'\fR as first argument, and you can create
2276additional independent event loops. Otherwise there will be no support 2333additional independent event loops. Otherwise there will be no support
2277for multiple event loops and there is no first event loop pointer 2334for multiple event loops and there is no first event loop pointer
2278argument. Instead, all functions act on the single default loop. 2335argument. 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
2342The 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
2344provide for more priorities by overriding those symbols (usually defined
2345to be \f(CW\*(C`\-2\*(C'\fR and \f(CW2\fR, respectively).
2346.Sp
2347When doing priority-based operations, libev usually has to linearly search
2348all the priorities, so having many of them (hundreds) uses a lot of space
2349and time, so using the defaults of five priorities (\-2 .. +2) is usually
2350fine.
2351.Sp
2352If 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"
2281If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If 2356If undefined or defined to be \f(CW1\fR, then periodic timers are supported. If
2282defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of 2357defined to be \f(CW0\fR, then they are not. Disabling them saves a few kB of
2283code. 2358code.
2382.SH "COMPLEXITIES" 2457.SH "COMPLEXITIES"
2383.IX Header "COMPLEXITIES" 2458.IX Header "COMPLEXITIES"
2384In this section the complexities of (many of) the algorithms used inside 2459In this section the complexities of (many of) the algorithms used inside
2385libev will be explained. For complexity discussions about backends see the 2460libev will be explained. For complexity discussions about backends see the
2386documentation for \f(CW\*(C`ev_default_init\*(C'\fR. 2461documentation for \f(CW\*(C`ev_default_init\*(C'\fR.
2462.Sp
2463All of the following are about amortised time: If an array needs to be
2464extended, libev needs to realloc and move the whole array, but this
2465happens asymptotically never with higher number of elements, so O(1) might
2466mean it might do a lengthy realloc operation in rare cases, but on average
2467it 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 2471This means that, when you have a watcher that triggers in one hour and
2472there are 100 watchers that would trigger before that then inserting will
2473have 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)"
2476That means that for changing a timer costs less than removing/adding them
2477as 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 2480These 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))"
2484These watchers are stored in lists then need to be walked to find the
2485correct watcher to remove. The lists are usually short (you don't usually
2486have 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
2493A change means an I/O watcher gets started or stopped, which requires
2494libev 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
2501Priorities are implemented by allocating some space for each
2502priority. When doing priority-based operations, libev usually has to
2503linearly 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"
2410Marc Lehmann <libev@schmorp.de>. 2508Marc Lehmann <libev@schmorp.de>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines