… | |
… | |
4 | <head> |
4 | <head> |
5 | <title>libev</title> |
5 | <title>libev</title> |
6 | <meta name="description" content="Pod documentation for libev" /> |
6 | <meta name="description" content="Pod documentation for libev" /> |
7 | <meta name="inputfile" content="<standard input>" /> |
7 | <meta name="inputfile" content="<standard input>" /> |
8 | <meta name="outputfile" content="<standard output>" /> |
8 | <meta name="outputfile" content="<standard output>" /> |
9 | <meta name="created" content="Mon Nov 12 08:58:02 2007" /> |
9 | <meta name="created" content="Mon Nov 12 09:16:01 2007" /> |
10 | <meta name="generator" content="Pod::Xhtml 1.57" /> |
10 | <meta name="generator" content="Pod::Xhtml 1.57" /> |
11 | <link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head> |
11 | <link rel="stylesheet" href="http://res.tst.eu/pod.css"/></head> |
12 | <body> |
12 | <body> |
13 | <div class="pod"> |
13 | <div class="pod"> |
14 | <!-- INDEX START --> |
14 | <!-- INDEX START --> |
… | |
… | |
26 | </ul> |
26 | </ul> |
27 | </li> |
27 | </li> |
28 | <li><a href="#WATCHER_TYPES">WATCHER TYPES</a> |
28 | <li><a href="#WATCHER_TYPES">WATCHER TYPES</a> |
29 | <ul><li><a href="#struct_ev_io_is_my_file_descriptor_r">struct ev_io - is my file descriptor readable or writable</a></li> |
29 | <ul><li><a href="#struct_ev_io_is_my_file_descriptor_r">struct ev_io - is my file descriptor readable or writable</a></li> |
30 | <li><a href="#struct_ev_timer_relative_and_optiona">struct ev_timer - relative and optionally recurring timeouts</a></li> |
30 | <li><a href="#struct_ev_timer_relative_and_optiona">struct ev_timer - relative and optionally recurring timeouts</a></li> |
31 | <li><a href="#ev_periodic">ev_periodic</a></li> |
31 | <li><a href="#ev_periodic_to_cron_or_not_to_cron_i">ev_periodic - to cron or not to cron it</a></li> |
32 | <li><a href="#ev_signal_signal_me_when_a_signal_ge">ev_signal - signal me when a signal gets signalled</a></li> |
32 | <li><a href="#ev_signal_signal_me_when_a_signal_ge">ev_signal - signal me when a signal gets signalled</a></li> |
33 | <li><a href="#ev_child_wait_for_pid_status_changes">ev_child - wait for pid status changes</a></li> |
33 | <li><a href="#ev_child_wait_for_pid_status_changes">ev_child - wait for pid status changes</a></li> |
34 | <li><a href="#ev_idle_when_you_ve_got_nothing_bett">ev_idle - when you've got nothing better to do</a></li> |
34 | <li><a href="#ev_idle_when_you_ve_got_nothing_bett">ev_idle - when you've got nothing better to do</a></li> |
35 | <li><a href="#prepare_and_check_your_hooks_into_th">prepare and check - your hooks into the event loop</a></li> |
35 | <li><a href="#prepare_and_check_your_hooks_into_th">prepare and check - your hooks into the event loop</a></li> |
36 | </ul> |
36 | </ul> |
… | |
… | |
55 | </div> |
55 | </div> |
56 | <h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p> |
56 | <h1 id="DESCRIPTION">DESCRIPTION</h1><p><a href="#TOP" class="toplink">Top</a></p> |
57 | <div id="DESCRIPTION_CONTENT"> |
57 | <div id="DESCRIPTION_CONTENT"> |
58 | <p>Libev is an event loop: you register interest in certain events (such as a |
58 | <p>Libev is an event loop: you register interest in certain events (such as a |
59 | file descriptor being readable or a timeout occuring), and it will manage |
59 | file descriptor being readable or a timeout occuring), and it will manage |
60 | these event sources and provide your program events.</p> |
60 | these event sources and provide your program with events.</p> |
61 | <p>To do this, it must take more or less complete control over your process |
61 | <p>To do this, it must take more or less complete control over your process |
62 | (or thread) by executing the <i>event loop</i> handler, and will then |
62 | (or thread) by executing the <i>event loop</i> handler, and will then |
63 | communicate events via a callback mechanism.</p> |
63 | communicate events via a callback mechanism.</p> |
64 | <p>You register interest in certain events by registering so-called <i>event |
64 | <p>You register interest in certain events by registering so-called <i>event |
65 | watchers</i>, which are relatively small C structures you initialise with the |
65 | watchers</i>, which are relatively small C structures you initialise with the |
… | |
… | |
71 | <div id="FEATURES_CONTENT"> |
71 | <div id="FEATURES_CONTENT"> |
72 | <p>Libev supports select, poll, the linux-specific epoll and the bsd-specific |
72 | <p>Libev supports select, poll, the linux-specific epoll and the bsd-specific |
73 | kqueue mechanisms for file descriptor events, relative timers, absolute |
73 | kqueue mechanisms for file descriptor events, relative timers, absolute |
74 | timers with customised rescheduling, signal events, process status change |
74 | timers with customised rescheduling, signal events, process status change |
75 | events (related to SIGCHLD), and event watchers dealing with the event |
75 | events (related to SIGCHLD), and event watchers dealing with the event |
76 | loop mechanism itself (idle, prepare and check watchers).</p> |
76 | loop mechanism itself (idle, prepare and check watchers). It also is quite |
|
|
77 | fast (see this <a href="http://libev.schmorp.de/bench.html">benchmark</a> comparing |
|
|
78 | it to libevent for example).</p> |
77 | |
79 | |
78 | </div> |
80 | </div> |
79 | <h1 id="CONVENTIONS">CONVENTIONS</h1><p><a href="#TOP" class="toplink">Top</a></p> |
81 | <h1 id="CONVENTIONS">CONVENTIONS</h1><p><a href="#TOP" class="toplink">Top</a></p> |
80 | <div id="CONVENTIONS_CONTENT"> |
82 | <div id="CONVENTIONS_CONTENT"> |
81 | <p>Libev is very configurable. In this manual the default configuration |
83 | <p>Libev is very configurable. In this manual the default configuration |
82 | will be described, which supports multiple event loops. For more info |
84 | will be described, which supports multiple event loops. For more info |
83 | about various configuraiton options please have a look at the file |
85 | about various configuration options please have a look at the file |
84 | <cite>README.embed</cite> in the libev distribution. If libev was configured without |
86 | <cite>README.embed</cite> in the libev distribution. If libev was configured without |
85 | support for multiple event loops, then all functions taking an initial |
87 | support for multiple event loops, then all functions taking an initial |
86 | argument of name <code>loop</code> (which is always of type <code>struct ev_loop *</code>) |
88 | argument of name <code>loop</code> (which is always of type <code>struct ev_loop *</code>) |
87 | will not have this argument.</p> |
89 | will not have this argument.</p> |
88 | |
90 | |
89 | </div> |
91 | </div> |
90 | <h1 id="TIME_AND_OTHER_GLOBAL_FUNCTIONS">TIME AND OTHER GLOBAL FUNCTIONS</h1><p><a href="#TOP" class="toplink">Top</a></p> |
92 | <h1 id="TIME_AND_OTHER_GLOBAL_FUNCTIONS">TIME AND OTHER GLOBAL FUNCTIONS</h1><p><a href="#TOP" class="toplink">Top</a></p> |
91 | <div id="TIME_AND_OTHER_GLOBAL_FUNCTIONS_CONT"> |
93 | <div id="TIME_AND_OTHER_GLOBAL_FUNCTIONS_CONT"> |
92 | <p>Libev represents time as a single floating point number. This type is |
94 | <p>Libev represents time as a single floating point number, representing the |
|
|
95 | (fractional) number of seconds since the (POSIX) epoch (somewhere near |
|
|
96 | the beginning of 1970, details are complicated, don't ask). This type is |
93 | called <code>ev_tstamp</code>, which is what you should use too. It usually aliases |
97 | called <code>ev_tstamp</code>, which is what you should use too. It usually aliases |
94 | to the double type in C.</p> |
98 | to the double type in C.</p> |
95 | <dl> |
99 | <dl> |
96 | <dt>ev_tstamp ev_time ()</dt> |
100 | <dt>ev_tstamp ev_time ()</dt> |
97 | <dd> |
101 | <dd> |
… | |
… | |
111 | not a problem.</p> |
115 | not a problem.</p> |
112 | </dd> |
116 | </dd> |
113 | <dt>ev_set_allocator (void *(*cb)(void *ptr, long size))</dt> |
117 | <dt>ev_set_allocator (void *(*cb)(void *ptr, long size))</dt> |
114 | <dd> |
118 | <dd> |
115 | <p>Sets the allocation function to use (the prototype is similar to the |
119 | <p>Sets the allocation function to use (the prototype is similar to the |
116 | realloc function). It is used to allocate and free memory (no surprises |
120 | realloc C function, the semantics are identical). It is used to allocate |
117 | here). If it returns zero when memory needs to be allocated, the library |
121 | and free memory (no surprises here). If it returns zero when memory |
118 | might abort or take some potentially destructive action. The default is |
122 | needs to be allocated, the library might abort or take some potentially |
119 | your system realloc function.</p> |
123 | destructive action. The default is your system realloc function.</p> |
120 | <p>You could override this function in high-availability programs to, say, |
124 | <p>You could override this function in high-availability programs to, say, |
121 | free some memory if it cannot allocate memory, to use a special allocator, |
125 | free some memory if it cannot allocate memory, to use a special allocator, |
122 | or even to sleep a while and retry until some memory is available.</p> |
126 | or even to sleep a while and retry until some memory is available.</p> |
123 | </dd> |
127 | </dd> |
124 | <dt>ev_set_syserr_cb (void (*cb)(const char *msg));</dt> |
128 | <dt>ev_set_syserr_cb (void (*cb)(const char *msg));</dt> |
125 | <dd> |
129 | <dd> |
126 | <p>Set the callback function to call on a retryable syscall error (such |
130 | <p>Set the callback function to call on a retryable syscall error (such |
127 | as failed select, poll, epoll_wait). The message is a printable string |
131 | as failed select, poll, epoll_wait). The message is a printable string |
128 | indicating the system call or subsystem causing the problem. If this |
132 | indicating the system call or subsystem causing the problem. If this |
129 | callback is set, then libev will expect it to remedy the sitution, no |
133 | callback is set, then libev will expect it to remedy the sitution, no |
130 | matter what, when it returns. That is, libev will geenrally retry the |
134 | matter what, when it returns. That is, libev will generally retry the |
131 | requested operation, or, if the condition doesn't go away, do bad stuff |
135 | requested operation, or, if the condition doesn't go away, do bad stuff |
132 | (such as abort).</p> |
136 | (such as abort).</p> |
133 | </dd> |
137 | </dd> |
134 | </dl> |
138 | </dl> |
135 | |
139 | |
… | |
… | |
139 | <p>An event loop is described by a <code>struct ev_loop *</code>. The library knows two |
143 | <p>An event loop is described by a <code>struct ev_loop *</code>. The library knows two |
140 | types of such loops, the <i>default</i> loop, which supports signals and child |
144 | types of such loops, the <i>default</i> loop, which supports signals and child |
141 | events, and dynamically created loops which do not.</p> |
145 | events, and dynamically created loops which do not.</p> |
142 | <p>If you use threads, a common model is to run the default event loop |
146 | <p>If you use threads, a common model is to run the default event loop |
143 | in your main thread (or in a separate thrad) and for each thread you |
147 | in your main thread (or in a separate thrad) and for each thread you |
144 | create, you also create another event loop. Libev itself does no lockign |
148 | create, you also create another event loop. Libev itself does no locking |
145 | whatsoever, so if you mix calls to different event loops, make sure you |
149 | whatsoever, so if you mix calls to the same event loop in different |
146 | lock (this is usually a bad idea, though, even if done right).</p> |
150 | threads, make sure you lock (this is usually a bad idea, though, even if |
|
|
151 | done correctly, because its hideous and inefficient).</p> |
147 | <dl> |
152 | <dl> |
148 | <dt>struct ev_loop *ev_default_loop (unsigned int flags)</dt> |
153 | <dt>struct ev_loop *ev_default_loop (unsigned int flags)</dt> |
149 | <dd> |
154 | <dd> |
150 | <p>This will initialise the default event loop if it hasn't been initialised |
155 | <p>This will initialise the default event loop if it hasn't been initialised |
151 | yet and return it. If the default loop could not be initialised, returns |
156 | yet and return it. If the default loop could not be initialised, returns |
… | |
… | |
314 | *)</code>), and you can stop watching for events at any time by calling the |
319 | *)</code>), and you can stop watching for events at any time by calling the |
315 | corresponding stop function (<code>ev_<type>_stop (loop, watcher *)</code>.</p> |
320 | corresponding stop function (<code>ev_<type>_stop (loop, watcher *)</code>.</p> |
316 | <p>As long as your watcher is active (has been started but not stopped) you |
321 | <p>As long as your watcher is active (has been started but not stopped) you |
317 | must not touch the values stored in it. Most specifically you must never |
322 | must not touch the values stored in it. Most specifically you must never |
318 | reinitialise it or call its set method.</p> |
323 | reinitialise it or call its set method.</p> |
319 | <p>You cna check wether an event is active by calling the <code>ev_is_active |
324 | <p>You cna check whether an event is active by calling the <code>ev_is_active |
320 | (watcher *)</code> macro. To see wether an event is outstanding (but the |
325 | (watcher *)</code> macro. To see whether an event is outstanding (but the |
321 | callback for it has not been called yet) you cna use the <code>ev_is_pending |
326 | callback for it has not been called yet) you cna use the <code>ev_is_pending |
322 | (watcher *)</code> macro.</p> |
327 | (watcher *)</code> macro.</p> |
323 | <p>Each and every callback receives the event loop pointer as first, the |
328 | <p>Each and every callback receives the event loop pointer as first, the |
324 | registered watcher structure as second, and a bitset of received events as |
329 | registered watcher structure as second, and a bitset of received events as |
325 | third argument.</p> |
330 | third argument.</p> |
… | |
… | |
420 | information given in the last section.</p> |
425 | information given in the last section.</p> |
421 | |
426 | |
422 | </div> |
427 | </div> |
423 | <h2 id="struct_ev_io_is_my_file_descriptor_r">struct ev_io - is my file descriptor readable or writable</h2> |
428 | <h2 id="struct_ev_io_is_my_file_descriptor_r">struct ev_io - is my file descriptor readable or writable</h2> |
424 | <div id="struct_ev_io_is_my_file_descriptor_r-2"> |
429 | <div id="struct_ev_io_is_my_file_descriptor_r-2"> |
425 | <p>I/O watchers check wether a file descriptor is readable or writable |
430 | <p>I/O watchers check whether a file descriptor is readable or writable |
426 | in each iteration of the event loop (This behaviour is called |
431 | in each iteration of the event loop (This behaviour is called |
427 | level-triggering because you keep receiving events as long as the |
432 | level-triggering because you keep receiving events as long as the |
428 | condition persists. Remember you cna stop the watcher if you don't want to |
433 | condition persists. Remember you cna stop the watcher if you don't want to |
429 | act on the event and neither want to receive future events).</p> |
434 | act on the event and neither want to receive future events).</p> |
430 | <dl> |
435 | <dl> |
… | |
… | |
478 | the timer, and again will automatically restart it if need be.</p> |
483 | the timer, and again will automatically restart it if need be.</p> |
479 | </dd> |
484 | </dd> |
480 | </dl> |
485 | </dl> |
481 | |
486 | |
482 | </div> |
487 | </div> |
483 | <h2 id="ev_periodic">ev_periodic</h2> |
488 | <h2 id="ev_periodic_to_cron_or_not_to_cron_i">ev_periodic - to cron or not to cron it</h2> |
484 | <div id="ev_periodic_CONTENT"> |
489 | <div id="ev_periodic_to_cron_or_not_to_cron_i-2"> |
485 | <p>Periodic watchers are also timers of a kind, but they are very versatile |
490 | <p>Periodic watchers are also timers of a kind, but they are very versatile |
486 | (and unfortunately a bit complex).</p> |
491 | (and unfortunately a bit complex).</p> |
487 | <p>Unlike ev_timer's, they are not based on real time (or relative time) |
492 | <p>Unlike ev_timer's, they are not based on real time (or relative time) |
488 | but on wallclock time (absolute time). You can tell a periodic watcher |
493 | but on wallclock time (absolute time). You can tell a periodic watcher |
489 | to trigger "at" some specific point in time. For example, if you tell a |
494 | to trigger "at" some specific point in time. For example, if you tell a |