<meta name="description" content="Pod documentation for libev" />
<meta name="inputfile" content="<standard input>" />
<meta name="outputfile" content="<standard output>" />
- <meta name="created" content="Fri Dec 7 19:03:06 2007" />
+ <meta name="created" content="Sat Dec 8 15:31:35 2007" />
<meta name="generator" content="Pod::Xhtml 1.57" />
<link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head>
<body>
</div>
<h1 id="DESCRIPTION">DESCRIPTION</h1>
<div id="DESCRIPTION_CONTENT">
+<p>The newest version of this document is also available as a html-formatted
+web page you might find easier to navigate when reading it for the first
+time: <a href="http://cvs.schmorp.de/libev/ev.html">http://cvs.schmorp.de/libev/ev.html</a>.</p>
<p>Libev is an event loop: you register interest in certain events (such as a
file descriptor being readable or a timeout occuring), and it will manage
these event sources and provide your program with events.</p>
<p>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
-<code>ev_TYPE_set</code> is safe) and you must make sure the watcher is available to
-libev (e.g. you cnanot <code>free ()</code> it).</p>
+<code>ev_TYPE_set</code> is safe), you must not change its priority, and you must
+make sure the watcher is available to libev (e.g. you cannot <code>free ()</code>
+it).</p>
</dd>
<dt>callback ev_cb (ev_TYPE *watcher)</dt>
<dd>
watchers on the same event and make sure one is called first.</p>
<p>If you need to suppress invocation when higher priority events are pending
you need to look at <code>ev_idle</code> watchers, which provide this functionality.</p>
+ <p>You <i>must not</i> change the priority of a watcher as long as it is active or
+pending.</p>
<p>The default priority used by watchers when no priority has been set is
always <code>0</code>, which is supposed to not be too high and not be too low :).</p>
<p>Setting a priority outside the range of <code>EV_MINPRI</code> to <code>EV_MAXPRI</code> 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.</p>
</dd>
+ <dt>ev_invoke (loop, ev_TYPE *watcher, int revents)</dt>
+ <dd>
+ <p>Invoke the <code>watcher</code> with the given <code>loop</code> and <code>revents</code>. Neither
+<code>loop</code> nor <code>revents</code> need to be valid as long as the watcher callback
+can deal with that fact.</p>
+ </dd>
+ <dt>int ev_clear_pending (loop, ev_TYPE *watcher)</dt>
+ <dd>
+ <p>If the watcher is pending, this function returns clears its pending status
+and returns its <code>revents</code> bitset (as if its callback was invoked). If the
+watcher isn't pending it does nothing and returns <code>0</code>.</p>
+ </dd>
</dl>
<pre> #include <ev++.h>
</pre>
-<p>(it is not installed by default). This automatically includes <cite>ev.h</cite>
-and puts all of its definitions (many of them macros) into the global
-namespace. All C++ specific things are put into the <code>ev</code> namespace.</p>
-<p>It should support all the same embedding options as <cite>ev.h</cite>, most notably
-<code>EV_MULTIPLICITY</code>.</p>
+<p>This automatically includes <cite>ev.h</cite> and puts all of its definitions (many
+of them macros) into the global namespace. All C++ specific things are
+put into the <code>ev</code> namespace. It should support all the same embedding
+options as <cite>ev.h</cite>, most notably <code>EV_MULTIPLICITY</code>.</p>
+<p>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 <code>EV_MULTIPLICITY</code> when embedding libev).</p>
+<p>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
+it).</p>
<p>Here is a list of things available in the <code>ev</code> namespace:</p>
<dl>
<dt><code>ev::READ</code>, <code>ev::WRITE</code> etc.</dt>
<p>All of those classes have these methods:</p>
<p>
<dl>
- <dt>ev::TYPE::TYPE (object *, object::method *)</dt>
- <dt>ev::TYPE::TYPE (object *, object::method *, struct ev_loop *)</dt>
+ <dt>ev::TYPE::TYPE ()</dt>
+ <dt>ev::TYPE::TYPE (struct ev_loop *)</dt>
<dt>ev::TYPE::~TYPE</dt>
<dd>
- <p>The constructor takes a pointer to an object and a method pointer to
-the event handler callback to call in this class. The constructor calls
-<code>ev_init</code> for you, which means you have to call the <code>set</code> method
-before starting it. If you do not specify a loop then the constructor
-automatically associates the default loop with this watcher.</p>
+ <p>The constructor (optionally) takes an event loop to associate the watcher
+with. If it is omitted, it will use <code>EV_DEFAULT</code>.</p>
+ <p>The constructor calls <code>ev_init</code> for you, which means you have to call the
+<code>set</code> method before starting it.</p>
+ <p>It will not set a callback, however: You have to call the templated <code>set</code>
+method to set a callback before you can start the watcher.</p>
+ <p>(The reason why you have to use a method is a limitation in C++ which does
+not allow explicit template arguments for constructors).</p>
<p>The destructor automatically stops the watcher if it is active.</p>
</dd>
+ <dt>w->set<class, &class::method> (object *)</dt>
+ <dd>
+ <p>This method sets the callback method to call. The method has to have a
+signature of <code>void (*)(ev_TYPE &, int)</code>, it receives the watcher as
+first argument and the <code>revents</code> as second. The object must be given as
+parameter and is stored in the <code>data</code> member of the watcher.</p>
+ <p>This method synthesizes efficient thunking code to call your method from
+the C callback that libev requires. If your compiler can inline your
+callback (i.e. it is visible to it at the place of the <code>set</code> call and
+your compiler is good :), then the method will be fully inlined into the
+thunking function, making it as fast as a direct C callback.</p>
+ <p>Example: simple class declaration and watcher initialisation</p>
+<pre> struct myclass
+ {
+ void io_cb (ev::io &w, int revents) { }
+ }
+
+ myclass obj;
+ ev::io iow;
+ iow.set <myclass, &myclass::io_cb> (&obj);
+
+</pre>
+ </dd>
+ <dt>w->set<function> (void *data = 0)</dt>
+ <dd>
+ <p>Also sets a callback, but uses a static method or plain function as
+callback. The optional <code>data</code> argument will be stored in the watcher's
+<code>data</code> member and is free for you to use.</p>
+ <p>The prototype of the <code>function</code> must be <code>void (*)(ev::TYPE &w, int)</code>.</p>
+ <p>See the method-<code>set</code> above for more details.</p>
+ <p>Example:</p>
+<pre> static void io_cb (ev::io &w, int revents) { }
+ iow.set <io_cb> ();
+
+</pre>
+ </dd>
<dt>w->set (struct ev_loop *)</dt>
<dd>
<p>Associates a different <code>struct ev_loop</code> with this watcher. You can only
<dt>w->set ([args])</dt>
<dd>
<p>Basically the same as <code>ev_TYPE_set</code>, with the same args. Must be
-called at least once. Unlike the C counterpart, an active watcher gets
-automatically stopped and restarted.</p>
+called at least once. Unlike the C counterpart, an active watcher gets
+automatically stopped and restarted when reconfiguring it with this
+method.</p>
</dd>
<dt>w->start ()</dt>
<dd>
- <p>Starts the watcher. Note that there is no <code>loop</code> argument as the
-constructor already takes the loop.</p>
+ <p>Starts the watcher. Note that there is no <code>loop</code> argument, as the
+constructor already stores the event loop.</p>
</dd>
<dt>w->stop ()</dt>
<dd>
}
myclass::myclass (int fd)
- : io (this, &myclass::io_cb),
- idle (this, &myclass::idle_cb)
{
+ io .set <myclass, &myclass::io_cb > (this);
+ idle.set <myclass, &myclass::idle_cb> (this);
+
io.start (fd, ev::READ);
}
additional independent event loops. Otherwise there will be no support
for multiple event loops and there is no first event loop pointer
argument. Instead, all functions act on the single default loop.</p>
+ </dd>
+ <dt>EV_MINPRI</dt>
+ <dt>EV_MAXPRI</dt>
+ <dd>
+ <p>The range of allowed priorities. <code>EV_MINPRI</code> must be smaller or equal to
+<code>EV_MAXPRI</code>, but otherwise there are no non-obvious limitations. You can
+provide for more priorities by overriding those symbols (usually defined
+to be <code>-2</code> and <code>2</code>, respectively).</p>
+ <p>When doing priority-based operations, libev usually has to linearly search
+all the priorities, so having many of them (hundreds) uses a lot of space
+and time, so using the defaults of five priorities (-2 .. +2) is usually
+fine.</p>
+ <p>If your embedding app does not need any priorities, defining these both to
+<code>0</code> will save some memory and cpu.</p>
</dd>
<dt>EV_PERIODIC_ENABLE</dt>
<dd>
<p>In this section the complexities of (many of) the algorithms used inside
libev will be explained. For complexity discussions about backends see the
documentation for <code>ev_default_init</code>.</p>
+ <p>All of the following are about amortised time: If an array needs to be
+extended, libev needs to realloc and move the whole array, but this
+happens asymptotically never with higher number of elements, so O(1) might
+mean it might do a lengthy realloc operation in rare cases, but on average
+it is much faster and asymptotically approaches constant time.</p>
<p>
<dl>
<dt>Starting and stopping timer/periodic watchers: O(log skipped_other_timers)</dt>
+ <dd>
+ <p>This means that, when you have a watcher that triggers in one hour and
+there are 100 watchers that would trigger before that then inserting will
+have to skip those 100 watchers.</p>
+ </dd>
<dt>Changing timer/periodic watchers (by autorepeat, again): O(log skipped_other_timers)</dt>
+ <dd>
+ <p>That means that for changing a timer costs less than removing/adding them
+as only the relative motion in the event queue has to be paid for.</p>
+ </dd>
<dt>Starting io/check/prepare/idle/signal/child watchers: O(1)</dt>
- <dt>Stopping check/prepare/idle watchers: O(1)</dt>
+ <dd>
+ <p>These just add the watcher into an array or at the head of a list.
+=item Stopping check/prepare/idle watchers: O(1)</p>
+ </dd>
<dt>Stopping an io/signal/child watcher: O(number_of_watchers_for_this_(fd/signal/pid % EV_PID_HASHSIZE))</dt>
+ <dd>
+ <p>These watchers are stored in lists then need to be walked to find the
+correct watcher to remove. The lists are usually short (you don't usually
+have many watchers waiting for the same fd or signal).</p>
+ </dd>
<dt>Finding the next timer per loop iteration: O(1)</dt>
<dt>Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)</dt>
+ <dd>
+ <p>A change means an I/O watcher gets started or stopped, which requires
+libev to recalculate its status (and possibly tell the kernel).</p>
+ </dd>
<dt>Activating one watcher: O(1)</dt>
+ <dt>Priority handling: O(number_of_priorities)</dt>
+ <dd>
+ <p>Priorities are implemented by allocating some space for each
+priority. When doing priority-based operations, libev usually has to
+linearly search all the priorities.</p>
+ </dd>
</dl>
</p>