… | |
… | |
129 | . ds Ae AE |
129 | . ds Ae AE |
130 | .\} |
130 | .\} |
131 | .rm #[ #] #H #V #F C |
131 | .rm #[ #] #H #V #F C |
132 | .\" ======================================================================== |
132 | .\" ======================================================================== |
133 | .\" |
133 | .\" |
134 | .IX Title "EV 1" |
134 | .IX Title "LIBEV 3" |
135 | .TH EV 1 "2008-04-02" "perl v5.10.0" "User Contributed Perl Documentation" |
135 | .TH LIBEV 3 "2008-05-11" "libev-3.33" "libev - high perfromance full featured event loop" |
136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
136 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes |
137 | .\" way too many mistakes in technical documents. |
137 | .\" way too many mistakes in technical documents. |
138 | .if n .ad l |
138 | .if n .ad l |
139 | .nh |
139 | .nh |
140 | .SH "NAME" |
140 | .SH "NAME" |
… | |
… | |
203 | .Ve |
203 | .Ve |
204 | .SH "DESCRIPTION" |
204 | .SH "DESCRIPTION" |
205 | .IX Header "DESCRIPTION" |
205 | .IX Header "DESCRIPTION" |
206 | The newest version of this document is also available as an html-formatted |
206 | The newest version of this document is also available as an html-formatted |
207 | web page you might find easier to navigate when reading it for the first |
207 | web page you might find easier to navigate when reading it for the first |
208 | time: <http://cvs.schmorp.de/libev/ev.html>. |
208 | time: <http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod>. |
209 | .PP |
209 | .PP |
210 | Libev is an event loop: you register interest in certain events (such as a |
210 | Libev is an event loop: you register interest in certain events (such as a |
211 | file descriptor being readable or a timeout occurring), and it will manage |
211 | file descriptor being readable or a timeout occurring), and it will manage |
212 | these event sources and provide your program with events. |
212 | these event sources and provide your program with events. |
213 | .PP |
213 | .PP |
… | |
… | |
328 | .Sp |
328 | .Sp |
329 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
329 | See the description of \f(CW\*(C`ev_embed\*(C'\fR watchers for more info. |
330 | .IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 |
330 | .IP "ev_set_allocator (void *(*cb)(void *ptr, long size))" 4 |
331 | .IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" |
331 | .IX Item "ev_set_allocator (void *(*cb)(void *ptr, long size))" |
332 | Sets the allocation function to use (the prototype is similar \- the |
332 | Sets the allocation function to use (the prototype is similar \- the |
333 | semantics is identical \- to the realloc C function). It is used to |
333 | semantics are identical to the \f(CW\*(C`realloc\*(C'\fR C89/SuS/POSIX function). It is |
334 | allocate and free memory (no surprises here). If it returns zero when |
334 | used to allocate and free memory (no surprises here). If it returns zero |
335 | memory needs to be allocated, the library might abort or take some |
335 | when memory needs to be allocated (\f(CW\*(C`size != 0\*(C'\fR), the library might abort |
336 | potentially destructive action. The default is your system realloc |
336 | or take some potentially destructive action. |
337 | function. |
337 | .Sp |
|
|
338 | Since some systems (at least OpenBSD and Darwin) fail to implement |
|
|
339 | correct \f(CW\*(C`realloc\*(C'\fR semantics, libev will use a wrapper around the system |
|
|
340 | \&\f(CW\*(C`realloc\*(C'\fR and \f(CW\*(C`free\*(C'\fR functions by default. |
338 | .Sp |
341 | .Sp |
339 | You could override this function in high-availability programs to, say, |
342 | You could override this function in high-availability programs to, say, |
340 | free some memory if it cannot allocate memory, to use a special allocator, |
343 | free some memory if it cannot allocate memory, to use a special allocator, |
341 | or even to sleep a while and retry until some memory is available. |
344 | or even to sleep a while and retry until some memory is available. |
342 | .Sp |
345 | .Sp |
343 | Example: Replace the libev allocator with one that waits a bit and then |
346 | Example: Replace the libev allocator with one that waits a bit and then |
344 | retries). |
347 | retries (example requires a standards-compliant \f(CW\*(C`realloc\*(C'\fR). |
345 | .Sp |
348 | .Sp |
346 | .Vb 6 |
349 | .Vb 6 |
347 | \& static void * |
350 | \& static void * |
348 | \& persistent_realloc (void *ptr, size_t size) |
351 | \& persistent_realloc (void *ptr, size_t size) |
349 | \& { |
352 | \& { |
… | |
… | |
387 | .SH "FUNCTIONS CONTROLLING THE EVENT LOOP" |
390 | .SH "FUNCTIONS CONTROLLING THE EVENT LOOP" |
388 | .IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" |
391 | .IX Header "FUNCTIONS CONTROLLING THE EVENT LOOP" |
389 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two |
392 | An event loop is described by a \f(CW\*(C`struct ev_loop *\*(C'\fR. The library knows two |
390 | types of such loops, the \fIdefault\fR loop, which supports signals and child |
393 | types of such loops, the \fIdefault\fR loop, which supports signals and child |
391 | events, and dynamically created loops which do not. |
394 | events, and dynamically created loops which do not. |
392 | .PP |
|
|
393 | If you use threads, a common model is to run the default event loop |
|
|
394 | in your main thread (or in a separate thread) and for each thread you |
|
|
395 | create, you also create another event loop. Libev itself does no locking |
|
|
396 | whatsoever, so if you mix calls to the same event loop in different |
|
|
397 | threads, make sure you lock (this is usually a bad idea, though, even if |
|
|
398 | done correctly, because it's hideous and inefficient). |
|
|
399 | .IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4 |
395 | .IP "struct ev_loop *ev_default_loop (unsigned int flags)" 4 |
400 | .IX Item "struct ev_loop *ev_default_loop (unsigned int flags)" |
396 | .IX Item "struct ev_loop *ev_default_loop (unsigned int flags)" |
401 | This will initialise the default event loop if it hasn't been initialised |
397 | This will initialise the default event loop if it hasn't been initialised |
402 | yet and return it. If the default loop could not be initialised, returns |
398 | yet and return it. If the default loop could not be initialised, returns |
403 | false. If it already was initialised it simply returns it (and ignores the |
399 | false. If it already was initialised it simply returns it (and ignores the |
… | |
… | |
486 | For few fds, this backend is a bit little slower than poll and select, |
482 | For few fds, this backend is a bit little slower than poll and select, |
487 | but it scales phenomenally better. While poll and select usually scale |
483 | but it scales phenomenally better. While poll and select usually scale |
488 | like O(total_fds) where n is the total number of fds (or the highest fd), |
484 | like O(total_fds) where n is the total number of fds (or the highest fd), |
489 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
485 | epoll scales either O(1) or O(active_fds). The epoll design has a number |
490 | of shortcomings, such as silently dropping events in some hard-to-detect |
486 | of shortcomings, such as silently dropping events in some hard-to-detect |
491 | cases and rewiring a syscall per fd change, no fork support and bad |
487 | cases and requiring a syscall per fd change, no fork support and bad |
492 | support for dup. |
488 | support for dup. |
493 | .Sp |
489 | .Sp |
494 | While stopping, setting and starting an I/O watcher in the same iteration |
490 | While stopping, setting and starting an I/O watcher in the same iteration |
495 | will result in some caching, there is still a syscall per such incident |
491 | will result in some caching, there is still a syscall per such incident |
496 | (because the fd could point to a different file description now), so its |
492 | (because the fd could point to a different file description now), so its |
… | |
… | |
1515 | .IX Item "ev_periodic_again (loop, ev_periodic *)" |
1511 | .IX Item "ev_periodic_again (loop, ev_periodic *)" |
1516 | Simply stops and restarts the periodic watcher again. This is only useful |
1512 | Simply stops and restarts the periodic watcher again. This is only useful |
1517 | when you changed some parameters or the reschedule callback would return |
1513 | when you changed some parameters or the reschedule callback would return |
1518 | a different time than the last time it was called (e.g. in a crond like |
1514 | a different time than the last time it was called (e.g. in a crond like |
1519 | program when the crontabs have changed). |
1515 | program when the crontabs have changed). |
|
|
1516 | .IP "ev_tstamp ev_periodic_at (ev_periodic *)" 4 |
|
|
1517 | .IX Item "ev_tstamp ev_periodic_at (ev_periodic *)" |
|
|
1518 | When active, returns the absolute time that the watcher is supposed to |
|
|
1519 | trigger next. |
1520 | .IP "ev_tstamp offset [read\-write]" 4 |
1520 | .IP "ev_tstamp offset [read\-write]" 4 |
1521 | .IX Item "ev_tstamp offset [read-write]" |
1521 | .IX Item "ev_tstamp offset [read-write]" |
1522 | When repeating, this contains the offset value, otherwise this is the |
1522 | When repeating, this contains the offset value, otherwise this is the |
1523 | absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR). |
1523 | absolute point in time (the \f(CW\*(C`at\*(C'\fR value passed to \f(CW\*(C`ev_periodic_set\*(C'\fR). |
1524 | .Sp |
1524 | .Sp |
… | |
… | |
1532 | .IP "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read\-write]" 4 |
1532 | .IP "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read\-write]" 4 |
1533 | .IX Item "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write]" |
1533 | .IX Item "ev_tstamp (*reschedule_cb)(struct ev_periodic *w, ev_tstamp now) [read-write]" |
1534 | The current reschedule callback, or \f(CW0\fR, if this functionality is |
1534 | The current reschedule callback, or \f(CW0\fR, if this functionality is |
1535 | switched off. Can be changed any time, but changes only take effect when |
1535 | switched off. Can be changed any time, but changes only take effect when |
1536 | the periodic timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. |
1536 | the periodic timer fires or \f(CW\*(C`ev_periodic_again\*(C'\fR is being called. |
1537 | .IP "ev_tstamp at [read\-only]" 4 |
|
|
1538 | .IX Item "ev_tstamp at [read-only]" |
|
|
1539 | When active, contains the absolute time that the watcher is supposed to |
|
|
1540 | trigger next. |
|
|
1541 | .PP |
1537 | .PP |
1542 | \fIExamples\fR |
1538 | \fIExamples\fR |
1543 | .IX Subsection "Examples" |
1539 | .IX Subsection "Examples" |
1544 | .PP |
1540 | .PP |
1545 | Example: Call a callback every hour, or, more precisely, whenever the |
1541 | Example: Call a callback every hour, or, more precisely, whenever the |
… | |
… | |
1751 | as even with OS-supported change notifications, this can be |
1747 | as even with OS-supported change notifications, this can be |
1752 | resource-intensive. |
1748 | resource-intensive. |
1753 | .PP |
1749 | .PP |
1754 | At the time of this writing, only the Linux inotify interface is |
1750 | At the time of this writing, only the Linux inotify interface is |
1755 | implemented (implementing kqueue support is left as an exercise for the |
1751 | implemented (implementing kqueue support is left as an exercise for the |
|
|
1752 | reader, note, however, that the author sees no way of implementing ev_stat |
1756 | reader). Inotify will be used to give hints only and should not change the |
1753 | semantics with kqueue). Inotify will be used to give hints only and should |
1757 | semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometimes needs |
1754 | not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev |
1758 | to fall back to regular polling again even with inotify, but changes are |
1755 | sometimes needs to fall back to regular polling again even with inotify, |
1759 | usually detected immediately, and if the file exists there will be no |
1756 | but changes are usually detected immediately, and if the file exists there |
1760 | polling. |
1757 | will be no polling. |
1761 | .PP |
1758 | .PP |
1762 | \fI\s-1ABI\s0 Issues (Largefile Support)\fR |
1759 | \fI\s-1ABI\s0 Issues (Largefile Support)\fR |
1763 | .IX Subsection "ABI Issues (Largefile Support)" |
1760 | .IX Subsection "ABI Issues (Largefile Support)" |
1764 | .PP |
1761 | .PP |
1765 | Libev by default (unless the user overrides this) uses the default |
1762 | Libev by default (unless the user overrides this) uses the default |
… | |
… | |
1777 | When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only |
1774 | When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only |
1778 | available on Linux) and present at runtime, it will be used to speed up |
1775 | available on Linux) and present at runtime, it will be used to speed up |
1779 | change detection where possible. The inotify descriptor will be created lazily |
1776 | change detection where possible. The inotify descriptor will be created lazily |
1780 | when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. |
1777 | when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started. |
1781 | .PP |
1778 | .PP |
1782 | Inotify presense does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers |
1779 | Inotify presence does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers |
1783 | except that changes might be detected earlier, and in some cases, to avoid |
1780 | except that changes might be detected earlier, and in some cases, to avoid |
1784 | making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presense of inotify support |
1781 | making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presence of inotify support |
1785 | there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling. |
1782 | there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling. |
1786 | .PP |
1783 | .PP |
1787 | (There is no support for kqueue, as apparently it cannot be used to |
1784 | (There is no support for kqueue, as apparently it cannot be used to |
1788 | implement this functionality, due to the requirement of having a file |
1785 | implement this functionality, due to the requirement of having a file |
1789 | descriptor open on the object at all times). |
1786 | descriptor open on the object at all times). |
… | |
… | |
1793 | .PP |
1790 | .PP |
1794 | The \f(CW\*(C`stat ()\*(C'\fR syscall only supports full-second resolution portably, and |
1791 | The \f(CW\*(C`stat ()\*(C'\fR syscall only supports full-second resolution portably, and |
1795 | even on systems where the resolution is higher, many filesystems still |
1792 | even on systems where the resolution is higher, many filesystems still |
1796 | only support whole seconds. |
1793 | only support whole seconds. |
1797 | .PP |
1794 | .PP |
1798 | That means that, if the time is the only thing that changes, you might |
1795 | That means that, if the time is the only thing that changes, you can |
1799 | miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and calls |
1796 | easily miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and |
1800 | your callback, which does something. When there is another update within |
1797 | calls your callback, which does something. When there is another update |
1801 | the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it. |
1798 | within the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it as the stat |
|
|
1799 | data does not change. |
1802 | .PP |
1800 | .PP |
1803 | The solution to this is to delay acting on a change for a second (or till |
1801 | The solution to this is to delay acting on a change for slightly more |
1804 | the next second boundary), using a roughly one-second delay \f(CW\*(C`ev_timer\*(C'\fR |
1802 | than second (or till slightly after the next full second boundary), using |
1805 | (\f(CW\*(C`ev_timer_set (w, 0., 1.01); ev_timer_again (loop, w)\*(C'\fR). The \f(CW.01\fR |
1803 | a roughly one-second-delay \f(CW\*(C`ev_timer\*(C'\fR (e.g. \f(CW\*(C`ev_timer_set (w, 0., 1.02); |
1806 | is added to work around small timing inconsistencies of some operating |
1804 | ev_timer_again (loop, w)\*(C'\fR). |
1807 | systems. |
1805 | .PP |
|
|
1806 | The \f(CW.02\fR offset is added to work around small timing inconsistencies |
|
|
1807 | of some operating systems (where the second counter of the current time |
|
|
1808 | might be be delayed. One such system is the Linux kernel, where a call to |
|
|
1809 | \&\f(CW\*(C`gettimeofday\*(C'\fR might return a timestamp with a full second later than |
|
|
1810 | a subsequent \f(CW\*(C`time\*(C'\fR call \- if the equivalent of \f(CW\*(C`time ()\*(C'\fR is used to |
|
|
1811 | update file times then there will be a small window where the kernel uses |
|
|
1812 | the previous second to update file times but libev might already execute |
|
|
1813 | the timer callback). |
1808 | .PP |
1814 | .PP |
1809 | \fIWatcher-Specific Functions and Data Members\fR |
1815 | \fIWatcher-Specific Functions and Data Members\fR |
1810 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1816 | .IX Subsection "Watcher-Specific Functions and Data Members" |
1811 | .IP "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 4 |
1817 | .IP "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 4 |
1812 | .IX Item "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" |
1818 | .IX Item "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" |
… | |
… | |
1818 | \&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to |
1824 | \&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to |
1819 | be detected and should normally be specified as \f(CW0\fR to let libev choose |
1825 | be detected and should normally be specified as \f(CW0\fR to let libev choose |
1820 | a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same |
1826 | a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same |
1821 | path for as long as the watcher is active. |
1827 | path for as long as the watcher is active. |
1822 | .Sp |
1828 | .Sp |
1823 | The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, |
1829 | The callback will receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected, relative |
1824 | relative to the attributes at the time the watcher was started (or the |
1830 | to the attributes at the time the watcher was started (or the last change |
1825 | last change was detected). |
1831 | was detected). |
1826 | .IP "ev_stat_stat (loop, ev_stat *)" 4 |
1832 | .IP "ev_stat_stat (loop, ev_stat *)" 4 |
1827 | .IX Item "ev_stat_stat (loop, ev_stat *)" |
1833 | .IX Item "ev_stat_stat (loop, ev_stat *)" |
1828 | Updates the stat buffer immediately with new values. If you change the |
1834 | Updates the stat buffer immediately with new values. If you change the |
1829 | watched path in your callback, you could call this fucntion to avoid |
1835 | watched path in your callback, you could call this function to avoid |
1830 | detecting this change (while introducing a race condition). Can also be |
1836 | detecting this change (while introducing a race condition if you are not |
1831 | useful simply to find out the new values. |
1837 | the only one changing the path). Can also be useful simply to find out the |
|
|
1838 | new values. |
1832 | .IP "ev_statdata attr [read\-only]" 4 |
1839 | .IP "ev_statdata attr [read\-only]" 4 |
1833 | .IX Item "ev_statdata attr [read-only]" |
1840 | .IX Item "ev_statdata attr [read-only]" |
1834 | The most-recently detected attributes of the file. Although the type is of |
1841 | The most-recently detected attributes of the file. Although the type is |
1835 | \&\f(CW\*(C`ev_statdata\*(C'\fR, this is usually the (or one of the) \f(CW\*(C`struct stat\*(C'\fR types |
1842 | \&\f(CW\*(C`ev_statdata\*(C'\fR, this is usually the (or one of the) \f(CW\*(C`struct stat\*(C'\fR types |
|
|
1843 | suitable for your system, but you can only rely on the POSIX-standardised |
1836 | suitable for your system. If the \f(CW\*(C`st_nlink\*(C'\fR member is \f(CW0\fR, then there |
1844 | members to be present. If the \f(CW\*(C`st_nlink\*(C'\fR member is \f(CW0\fR, then there was |
1837 | was some error while \f(CW\*(C`stat\*(C'\fRing the file. |
1845 | some error while \f(CW\*(C`stat\*(C'\fRing the file. |
1838 | .IP "ev_statdata prev [read\-only]" 4 |
1846 | .IP "ev_statdata prev [read\-only]" 4 |
1839 | .IX Item "ev_statdata prev [read-only]" |
1847 | .IX Item "ev_statdata prev [read-only]" |
1840 | The previous attributes of the file. The callback gets invoked whenever |
1848 | The previous attributes of the file. The callback gets invoked whenever |
1841 | \&\f(CW\*(C`prev\*(C'\fR != \f(CW\*(C`attr\*(C'\fR. |
1849 | \&\f(CW\*(C`prev\*(C'\fR != \f(CW\*(C`attr\*(C'\fR, or, more precisely, one or more of these members |
|
|
1850 | differ: \f(CW\*(C`st_dev\*(C'\fR, \f(CW\*(C`st_ino\*(C'\fR, \f(CW\*(C`st_mode\*(C'\fR, \f(CW\*(C`st_nlink\*(C'\fR, \f(CW\*(C`st_uid\*(C'\fR, |
|
|
1851 | \&\f(CW\*(C`st_gid\*(C'\fR, \f(CW\*(C`st_rdev\*(C'\fR, \f(CW\*(C`st_size\*(C'\fR, \f(CW\*(C`st_atime\*(C'\fR, \f(CW\*(C`st_mtime\*(C'\fR, \f(CW\*(C`st_ctime\*(C'\fR. |
1842 | .IP "ev_tstamp interval [read\-only]" 4 |
1852 | .IP "ev_tstamp interval [read\-only]" 4 |
1843 | .IX Item "ev_tstamp interval [read-only]" |
1853 | .IX Item "ev_tstamp interval [read-only]" |
1844 | The specified interval. |
1854 | The specified interval. |
1845 | .IP "const char *path [read\-only]" 4 |
1855 | .IP "const char *path [read\-only]" 4 |
1846 | .IX Item "const char *path [read-only]" |
1856 | .IX Item "const char *path [read-only]" |
… | |
… | |
1900 | \& } |
1910 | \& } |
1901 | \& |
1911 | \& |
1902 | \& ... |
1912 | \& ... |
1903 | \& ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.); |
1913 | \& ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.); |
1904 | \& ev_stat_start (loop, &passwd); |
1914 | \& ev_stat_start (loop, &passwd); |
1905 | \& ev_timer_init (&timer, timer_cb, 0., 1.01); |
1915 | \& ev_timer_init (&timer, timer_cb, 0., 1.02); |
1906 | .Ve |
1916 | .Ve |
1907 | .ie n .Sh """ev_idle"" \- when you've got nothing better to do..." |
1917 | .ie n .Sh """ev_idle"" \- when you've got nothing better to do..." |
1908 | .el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." |
1918 | .el .Sh "\f(CWev_idle\fP \- when you've got nothing better to do..." |
1909 | .IX Subsection "ev_idle - when you've got nothing better to do..." |
1919 | .IX Subsection "ev_idle - when you've got nothing better to do..." |
1910 | Idle watchers trigger events when no other events of the same or higher |
1920 | Idle watchers trigger events when no other events of the same or higher |
… | |
… | |
1996 | .PP |
2006 | .PP |
1997 | It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) |
2007 | It is recommended to give \f(CW\*(C`ev_check\*(C'\fR watchers highest (\f(CW\*(C`EV_MAXPRI\*(C'\fR) |
1998 | priority, to ensure that they are being run before any other watchers |
2008 | priority, to ensure that they are being run before any other watchers |
1999 | after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, |
2009 | after the poll. Also, \f(CW\*(C`ev_check\*(C'\fR watchers (and \f(CW\*(C`ev_prepare\*(C'\fR watchers, |
2000 | too) should not activate (\*(L"feed\*(R") events into libev. While libev fully |
2010 | too) should not activate (\*(L"feed\*(R") events into libev. While libev fully |
2001 | supports this, they will be called before other \f(CW\*(C`ev_check\*(C'\fR watchers |
2011 | supports this, they might get executed before other \f(CW\*(C`ev_check\*(C'\fR watchers |
2002 | did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other |
2012 | did their job. As \f(CW\*(C`ev_check\*(C'\fR watchers are often used to embed other |
2003 | (non-libev) event loops those other event loops might be in an unusable |
2013 | (non-libev) event loops those other event loops might be in an unusable |
2004 | state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to |
2014 | state until their \f(CW\*(C`ev_check\*(C'\fR watcher ran (always remind yourself to |
2005 | coexist peacefully with others). |
2015 | coexist peacefully with others). |
2006 | .PP |
2016 | .PP |
… | |
… | |
2020 | .IX Subsection "Examples" |
2030 | .IX Subsection "Examples" |
2021 | .PP |
2031 | .PP |
2022 | There are a number of principal ways to embed other event loops or modules |
2032 | There are a number of principal ways to embed other event loops or modules |
2023 | into libev. Here are some ideas on how to include libadns into libev |
2033 | into libev. Here are some ideas on how to include libadns into libev |
2024 | (there is a Perl module named \f(CW\*(C`EV::ADNS\*(C'\fR that does this, which you could |
2034 | (there is a Perl module named \f(CW\*(C`EV::ADNS\*(C'\fR that does this, which you could |
2025 | use for an actually working example. Another Perl module named \f(CW\*(C`EV::Glib\*(C'\fR |
2035 | use as a working example. Another Perl module named \f(CW\*(C`EV::Glib\*(C'\fR embeds a |
2026 | embeds a Glib main context into libev, and finally, \f(CW\*(C`Glib::EV\*(C'\fR embeds \s-1EV\s0 |
2036 | Glib main context into libev, and finally, \f(CW\*(C`Glib::EV\*(C'\fR embeds \s-1EV\s0 into the |
2027 | into the Glib event loop). |
2037 | Glib event loop). |
2028 | .PP |
2038 | .PP |
2029 | Method 1: Add \s-1IO\s0 watchers and a timeout watcher in a prepare handler, |
2039 | Method 1: Add \s-1IO\s0 watchers and a timeout watcher in a prepare handler, |
2030 | and in a check watcher, destroy them and call into libadns. What follows |
2040 | and in a check watcher, destroy them and call into libadns. What follows |
2031 | is pseudo-code only of course. This requires you to either use a low |
2041 | is pseudo-code only of course. This requires you to either use a low |
2032 | priority for the check watcher or use \f(CW\*(C`ev_clear_pending\*(C'\fR explicitly, as |
2042 | priority for the check watcher or use \f(CW\*(C`ev_clear_pending\*(C'\fR explicitly, as |
… | |
… | |
2495 | it a private \s-1API\s0). |
2505 | it a private \s-1API\s0). |
2496 | .IP "\(bu" 4 |
2506 | .IP "\(bu" 4 |
2497 | Priorities are not currently supported. Initialising priorities |
2507 | Priorities are not currently supported. Initialising priorities |
2498 | will fail and all watchers will have the same priority, even though there |
2508 | will fail and all watchers will have the same priority, even though there |
2499 | is an ev_pri field. |
2509 | is an ev_pri field. |
|
|
2510 | .IP "\(bu" 4 |
|
|
2511 | In libevent, the last base created gets the signals, in libev, the |
|
|
2512 | first base created (== the default loop) gets the signals. |
2500 | .IP "\(bu" 4 |
2513 | .IP "\(bu" 4 |
2501 | Other members are not supported. |
2514 | Other members are not supported. |
2502 | .IP "\(bu" 4 |
2515 | .IP "\(bu" 4 |
2503 | The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need |
2516 | The libev emulation is \fInot\fR \s-1ABI\s0 compatible to libevent, you need |
2504 | to use the libev header file and library. |
2517 | to use the libev header file and library. |
… | |
… | |
2733 | .ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4 |
2746 | .ie n .IP """EV_DEFAULT""\fR, \f(CW""EV_DEFAULT_""" 4 |
2734 | .el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4 |
2747 | .el .IP "\f(CWEV_DEFAULT\fR, \f(CWEV_DEFAULT_\fR" 4 |
2735 | .IX Item "EV_DEFAULT, EV_DEFAULT_" |
2748 | .IX Item "EV_DEFAULT, EV_DEFAULT_" |
2736 | Similar to the other two macros, this gives you the value of the default |
2749 | Similar to the other two macros, this gives you the value of the default |
2737 | loop, if multiple loops are supported (\*(L"ev loop default\*(R"). |
2750 | loop, if multiple loops are supported (\*(L"ev loop default\*(R"). |
|
|
2751 | .ie n .IP """EV_DEFAULT_UC""\fR, \f(CW""EV_DEFAULT_UC_""" 4 |
|
|
2752 | .el .IP "\f(CWEV_DEFAULT_UC\fR, \f(CWEV_DEFAULT_UC_\fR" 4 |
|
|
2753 | .IX Item "EV_DEFAULT_UC, EV_DEFAULT_UC_" |
|
|
2754 | Usage identical to \f(CW\*(C`EV_DEFAULT\*(C'\fR and \f(CW\*(C`EV_DEFAULT_\*(C'\fR, but requires that the |
|
|
2755 | default loop has been initialised (\f(CW\*(C`UC\*(C'\fR == unchecked). Their behaviour |
|
|
2756 | is undefined when the default loop has not been initialised by a previous |
|
|
2757 | execution of \f(CW\*(C`EV_DEFAULT\*(C'\fR, \f(CW\*(C`EV_DEFAULT_\*(C'\fR or \f(CW\*(C`ev_default_init (...)\*(C'\fR. |
|
|
2758 | .Sp |
|
|
2759 | It is often prudent to use \f(CW\*(C`EV_DEFAULT\*(C'\fR when initialising the first |
|
|
2760 | watcher in a function but use \f(CW\*(C`EV_DEFAULT_UC\*(C'\fR afterwards. |
2738 | .PP |
2761 | .PP |
2739 | Example: Declare and initialise a check watcher, utilising the above |
2762 | Example: Declare and initialise a check watcher, utilising the above |
2740 | macros so it will work regardless of whether multiple loops are supported |
2763 | macros so it will work regardless of whether multiple loops are supported |
2741 | or not. |
2764 | or not. |
2742 | .PP |
2765 | .PP |
… | |
… | |
2852 | .Vb 1 |
2875 | .Vb 1 |
2853 | \& libev.m4 |
2876 | \& libev.m4 |
2854 | .Ve |
2877 | .Ve |
2855 | .Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" |
2878 | .Sh "\s-1PREPROCESSOR\s0 \s-1SYMBOLS/MACROS\s0" |
2856 | .IX Subsection "PREPROCESSOR SYMBOLS/MACROS" |
2879 | .IX Subsection "PREPROCESSOR SYMBOLS/MACROS" |
2857 | Libev can be configured via a variety of preprocessor symbols you have to define |
2880 | Libev can be configured via a variety of preprocessor symbols you have to |
2858 | before including any of its files. The default is not to build for multiplicity |
2881 | define before including any of its files. The default in the absense of |
2859 | and only include the select backend. |
2882 | autoconf is noted for every option. |
2860 | .IP "\s-1EV_STANDALONE\s0" 4 |
2883 | .IP "\s-1EV_STANDALONE\s0" 4 |
2861 | .IX Item "EV_STANDALONE" |
2884 | .IX Item "EV_STANDALONE" |
2862 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
2885 | Must always be \f(CW1\fR if you do not use autoconf configuration, which |
2863 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
2886 | keeps libev from including \fIconfig.h\fR, and it also defines dummy |
2864 | implementations for some libevent functions (such as logging, which is not |
2887 | implementations for some libevent functions (such as logging, which is not |
… | |
… | |
2883 | note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. |
2906 | note about libraries in the description of \f(CW\*(C`EV_USE_MONOTONIC\*(C'\fR, though. |
2884 | .IP "\s-1EV_USE_NANOSLEEP\s0" 4 |
2907 | .IP "\s-1EV_USE_NANOSLEEP\s0" 4 |
2885 | .IX Item "EV_USE_NANOSLEEP" |
2908 | .IX Item "EV_USE_NANOSLEEP" |
2886 | If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available |
2909 | If defined to be \f(CW1\fR, libev will assume that \f(CW\*(C`nanosleep ()\*(C'\fR is available |
2887 | and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. |
2910 | and will use it for delays. Otherwise it will use \f(CW\*(C`select ()\*(C'\fR. |
|
|
2911 | .IP "\s-1EV_USE_EVENTFD\s0" 4 |
|
|
2912 | .IX Item "EV_USE_EVENTFD" |
|
|
2913 | If defined to be \f(CW1\fR, then libev will assume that \f(CW\*(C`eventfd ()\*(C'\fR is |
|
|
2914 | available and will probe for kernel support at runtime. This will improve |
|
|
2915 | \&\f(CW\*(C`ev_signal\*(C'\fR and \f(CW\*(C`ev_async\*(C'\fR performance and reduce resource consumption. |
|
|
2916 | If undefined, it will be enabled if the headers indicate GNU/Linux + Glibc |
|
|
2917 | 2.7 or newer, otherwise disabled. |
2888 | .IP "\s-1EV_USE_SELECT\s0" 4 |
2918 | .IP "\s-1EV_USE_SELECT\s0" 4 |
2889 | .IX Item "EV_USE_SELECT" |
2919 | .IX Item "EV_USE_SELECT" |
2890 | If undefined or defined to be \f(CW1\fR, libev will compile in support for the |
2920 | If undefined or defined to be \f(CW1\fR, libev will compile in support for the |
2891 | \&\f(CW\*(C`select\*(C'\fR(2) backend. No attempt at autodetection will be done: if no |
2921 | \&\f(CW\*(C`select\*(C'\fR(2) backend. No attempt at autodetection will be done: if no |
2892 | other method takes over, select will be it. Otherwise the select backend |
2922 | other method takes over, select will be it. Otherwise the select backend |
… | |
… | |
2923 | takes precedence over select. |
2953 | takes precedence over select. |
2924 | .IP "\s-1EV_USE_EPOLL\s0" 4 |
2954 | .IP "\s-1EV_USE_EPOLL\s0" 4 |
2925 | .IX Item "EV_USE_EPOLL" |
2955 | .IX Item "EV_USE_EPOLL" |
2926 | If defined to be \f(CW1\fR, libev will compile in support for the Linux |
2956 | If defined to be \f(CW1\fR, libev will compile in support for the Linux |
2927 | \&\f(CW\*(C`epoll\*(C'\fR(7) backend. Its availability will be detected at runtime, |
2957 | \&\f(CW\*(C`epoll\*(C'\fR(7) backend. Its availability will be detected at runtime, |
2928 | otherwise another method will be used as fallback. This is the |
2958 | otherwise another method will be used as fallback. This is the preferred |
2929 | preferred backend for GNU/Linux systems. |
2959 | backend for GNU/Linux systems. If undefined, it will be enabled if the |
|
|
2960 | headers indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
2930 | .IP "\s-1EV_USE_KQUEUE\s0" 4 |
2961 | .IP "\s-1EV_USE_KQUEUE\s0" 4 |
2931 | .IX Item "EV_USE_KQUEUE" |
2962 | .IX Item "EV_USE_KQUEUE" |
2932 | If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style |
2963 | If defined to be \f(CW1\fR, libev will compile in support for the \s-1BSD\s0 style |
2933 | \&\f(CW\*(C`kqueue\*(C'\fR(2) backend. Its actual availability will be detected at runtime, |
2964 | \&\f(CW\*(C`kqueue\*(C'\fR(2) backend. Its actual availability will be detected at runtime, |
2934 | otherwise another method will be used as fallback. This is the preferred |
2965 | otherwise another method will be used as fallback. This is the preferred |
… | |
… | |
2949 | reserved for future expansion, works like the \s-1USE\s0 symbols above. |
2980 | reserved for future expansion, works like the \s-1USE\s0 symbols above. |
2950 | .IP "\s-1EV_USE_INOTIFY\s0" 4 |
2981 | .IP "\s-1EV_USE_INOTIFY\s0" 4 |
2951 | .IX Item "EV_USE_INOTIFY" |
2982 | .IX Item "EV_USE_INOTIFY" |
2952 | If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify |
2983 | If defined to be \f(CW1\fR, libev will compile in support for the Linux inotify |
2953 | interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will |
2984 | interface to speed up \f(CW\*(C`ev_stat\*(C'\fR watchers. Its actual availability will |
2954 | be detected at runtime. |
2985 | be detected at runtime. If undefined, it will be enabled if the headers |
|
|
2986 | indicate GNU/Linux + Glibc 2.4 or newer, otherwise disabled. |
2955 | .IP "\s-1EV_ATOMIC_T\s0" 4 |
2987 | .IP "\s-1EV_ATOMIC_T\s0" 4 |
2956 | .IX Item "EV_ATOMIC_T" |
2988 | .IX Item "EV_ATOMIC_T" |
2957 | Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose |
2989 | Libev requires an integer type (suitable for storing \f(CW0\fR or \f(CW1\fR) whose |
2958 | access is atomic with respect to other threads or signal contexts. No such |
2990 | access is atomic with respect to other threads or signal contexts. No such |
2959 | type is easily found in the C language, so you can provide your own type |
2991 | type is easily found in the C language, so you can provide your own type |
… | |
… | |
3034 | If undefined or defined to be \f(CW1\fR, then async watchers are supported. If |
3066 | If undefined or defined to be \f(CW1\fR, then async watchers are supported. If |
3035 | defined to be \f(CW0\fR, then they are not. |
3067 | defined to be \f(CW0\fR, then they are not. |
3036 | .IP "\s-1EV_MINIMAL\s0" 4 |
3068 | .IP "\s-1EV_MINIMAL\s0" 4 |
3037 | .IX Item "EV_MINIMAL" |
3069 | .IX Item "EV_MINIMAL" |
3038 | If you need to shave off some kilobytes of code at the expense of some |
3070 | If you need to shave off some kilobytes of code at the expense of some |
3039 | speed, define this symbol to \f(CW1\fR. Currently only used for gcc to override |
3071 | speed, define this symbol to \f(CW1\fR. Currently this is used to override some |
3040 | some inlining decisions, saves roughly 30% codesize of amd64. |
3072 | inlining decisions, saves roughly 30% codesize of amd64. It also selects a |
|
|
3073 | much smaller 2\-heap for timer management over the default 4\-heap. |
3041 | .IP "\s-1EV_PID_HASHSIZE\s0" 4 |
3074 | .IP "\s-1EV_PID_HASHSIZE\s0" 4 |
3042 | .IX Item "EV_PID_HASHSIZE" |
3075 | .IX Item "EV_PID_HASHSIZE" |
3043 | \&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by |
3076 | \&\f(CW\*(C`ev_child\*(C'\fR watchers use a small hash table to distribute workload by |
3044 | pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more |
3077 | pid. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), usually more |
3045 | than enough. If you need to manage thousands of children you might want to |
3078 | than enough. If you need to manage thousands of children you might want to |
… | |
… | |
3049 | \&\f(CW\*(C`ev_stat\*(C'\fR watchers use a small hash table to distribute workload by |
3082 | \&\f(CW\*(C`ev_stat\*(C'\fR watchers use a small hash table to distribute workload by |
3050 | inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), |
3083 | inotify watch id. The default size is \f(CW16\fR (or \f(CW1\fR with \f(CW\*(C`EV_MINIMAL\*(C'\fR), |
3051 | usually more than enough. If you need to manage thousands of \f(CW\*(C`ev_stat\*(C'\fR |
3084 | usually more than enough. If you need to manage thousands of \f(CW\*(C`ev_stat\*(C'\fR |
3052 | watchers you might want to increase this value (\fImust\fR be a power of |
3085 | watchers you might want to increase this value (\fImust\fR be a power of |
3053 | two). |
3086 | two). |
|
|
3087 | .IP "\s-1EV_USE_4HEAP\s0" 4 |
|
|
3088 | .IX Item "EV_USE_4HEAP" |
|
|
3089 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
|
|
3090 | timer and periodics heap, libev uses a 4\-heap when this symbol is defined |
|
|
3091 | to \f(CW1\fR. The 4\-heap uses more complicated (longer) code but has a |
|
|
3092 | noticable after performance with many (thousands) of watchers. |
|
|
3093 | .Sp |
|
|
3094 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
|
|
3095 | (disabled). |
|
|
3096 | .IP "\s-1EV_HEAP_CACHE_AT\s0" 4 |
|
|
3097 | .IX Item "EV_HEAP_CACHE_AT" |
|
|
3098 | Heaps are not very cache-efficient. To improve the cache-efficiency of the |
|
|
3099 | timer and periodics heap, libev can cache the timestamp (\fIat\fR) within |
|
|
3100 | the heap structure (selected by defining \f(CW\*(C`EV_HEAP_CACHE_AT\*(C'\fR to \f(CW1\fR), |
|
|
3101 | which uses 8\-12 bytes more per watcher and a few hundred bytes more code, |
|
|
3102 | but avoids random read accesses on heap changes. This noticably improves |
|
|
3103 | performance noticably with with many (hundreds) of watchers. |
|
|
3104 | .Sp |
|
|
3105 | The default is \f(CW1\fR unless \f(CW\*(C`EV_MINIMAL\*(C'\fR is set in which case it is \f(CW0\fR |
|
|
3106 | (disabled). |
3054 | .IP "\s-1EV_COMMON\s0" 4 |
3107 | .IP "\s-1EV_COMMON\s0" 4 |
3055 | .IX Item "EV_COMMON" |
3108 | .IX Item "EV_COMMON" |
3056 | By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining |
3109 | By default, all watchers have a \f(CW\*(C`void *data\*(C'\fR member. By redefining |
3057 | this macro to a something else you can include more and other types of |
3110 | this macro to a something else you can include more and other types of |
3058 | members. You have to define it each time you include one of the files, |
3111 | members. You have to define it each time you include one of the files, |
… | |
… | |
3140 | .PP |
3193 | .PP |
3141 | .Vb 2 |
3194 | .Vb 2 |
3142 | \& #include "ev_cpp.h" |
3195 | \& #include "ev_cpp.h" |
3143 | \& #include "ev.c" |
3196 | \& #include "ev.c" |
3144 | .Ve |
3197 | .Ve |
|
|
3198 | .SH "THREADS AND COROUTINES" |
|
|
3199 | .IX Header "THREADS AND COROUTINES" |
|
|
3200 | .Sh "\s-1THREADS\s0" |
|
|
3201 | .IX Subsection "THREADS" |
|
|
3202 | Libev itself is completely threadsafe, but it uses no locking. This |
|
|
3203 | means that you can use as many loops as you want in parallel, as long as |
|
|
3204 | only one thread ever calls into one libev function with the same loop |
|
|
3205 | parameter. |
|
|
3206 | .PP |
|
|
3207 | Or put differently: calls with different loop parameters can be done in |
|
|
3208 | parallel from multiple threads, calls with the same loop parameter must be |
|
|
3209 | done serially (but can be done from different threads, as long as only one |
|
|
3210 | thread ever is inside a call at any point in time, e.g. by using a mutex |
|
|
3211 | per loop). |
|
|
3212 | .PP |
|
|
3213 | If you want to know which design is best for your problem, then I cannot |
|
|
3214 | help you but by giving some generic advice: |
|
|
3215 | .IP "\(bu" 4 |
|
|
3216 | most applications have a main thread: use the default libev loop |
|
|
3217 | in that thread, or create a seperate thread running only the default loop. |
|
|
3218 | .Sp |
|
|
3219 | This helps integrating other libraries or software modules that use libev |
|
|
3220 | themselves and don't care/know about threading. |
|
|
3221 | .IP "\(bu" 4 |
|
|
3222 | one loop per thread is usually a good model. |
|
|
3223 | .Sp |
|
|
3224 | Doing this is almost never wrong, sometimes a better-performance model |
|
|
3225 | exists, but it is always a good start. |
|
|
3226 | .IP "\(bu" 4 |
|
|
3227 | other models exist, such as the leader/follower pattern, where one |
|
|
3228 | loop is handed through multiple threads in a kind of round-robbin fashion. |
|
|
3229 | .Sp |
|
|
3230 | Chosing a model is hard \- look around, learn, know that usually you cna do |
|
|
3231 | better than you currently do :\-) |
|
|
3232 | .IP "\(bu" 4 |
|
|
3233 | often you need to talk to some other thread which blocks in the |
|
|
3234 | event loop \- \f(CW\*(C`ev_async\*(C'\fR watchers can be used to wake them up from other |
|
|
3235 | threads safely (or from signal contexts...). |
|
|
3236 | .Sh "\s-1COROUTINES\s0" |
|
|
3237 | .IX Subsection "COROUTINES" |
|
|
3238 | Libev is much more accomodating to coroutines (\*(L"cooperative threads\*(R"): |
|
|
3239 | libev fully supports nesting calls to it's functions from different |
|
|
3240 | coroutines (e.g. you can call \f(CW\*(C`ev_loop\*(C'\fR on the same loop from two |
|
|
3241 | different coroutines and switch freely between both coroutines running the |
|
|
3242 | loop, as long as you don't confuse yourself). The only exception is that |
|
|
3243 | you must not do this from \f(CW\*(C`ev_periodic\*(C'\fR reschedule callbacks. |
|
|
3244 | .PP |
|
|
3245 | Care has been invested into making sure that libev does not keep local |
|
|
3246 | state inside \f(CW\*(C`ev_loop\*(C'\fR, and other calls do not usually allow coroutine |
|
|
3247 | switches. |
3145 | .SH "COMPLEXITIES" |
3248 | .SH "COMPLEXITIES" |
3146 | .IX Header "COMPLEXITIES" |
3249 | .IX Header "COMPLEXITIES" |
3147 | In this section the complexities of (many of) the algorithms used inside |
3250 | In this section the complexities of (many of) the algorithms used inside |
3148 | libev will be explained. For complexity discussions about backends see the |
3251 | libev will be explained. For complexity discussions about backends see the |
3149 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
3252 | documentation for \f(CW\*(C`ev_default_init\*(C'\fR. |
… | |
… | |
3174 | These watchers are stored in lists then need to be walked to find the |
3277 | These watchers are stored in lists then need to be walked to find the |
3175 | correct watcher to remove. The lists are usually short (you don't usually |
3278 | correct watcher to remove. The lists are usually short (you don't usually |
3176 | have many watchers waiting for the same fd or signal). |
3279 | have many watchers waiting for the same fd or signal). |
3177 | .IP "Finding the next timer in each loop iteration: O(1)" 4 |
3280 | .IP "Finding the next timer in each loop iteration: O(1)" 4 |
3178 | .IX Item "Finding the next timer in each loop iteration: O(1)" |
3281 | .IX Item "Finding the next timer in each loop iteration: O(1)" |
3179 | By virtue of using a binary heap, the next timer is always found at the |
3282 | By virtue of using a binary or 4\-heap, the next timer is always found at a |
3180 | beginning of the storage array. |
3283 | fixed position in the storage array. |
3181 | .IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 |
3284 | .IP "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" 4 |
3182 | .IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" |
3285 | .IX Item "Each change on a file descriptor per loop iteration: O(number_of_watchers_for_this_fd)" |
3183 | A change means an I/O watcher gets started or stopped, which requires |
3286 | A change means an I/O watcher gets started or stopped, which requires |
3184 | libev to recalculate its status (and possibly tell the kernel, depending |
3287 | libev to recalculate its status (and possibly tell the kernel, depending |
3185 | on backend and wether \f(CW\*(C`ev_io_set\*(C'\fR was used). |
3288 | on backend and wether \f(CW\*(C`ev_io_set\*(C'\fR was used). |
… | |
… | |
3211 | model. Libev still offers limited functionality on this platform in |
3314 | model. Libev still offers limited functionality on this platform in |
3212 | the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket |
3315 | the form of the \f(CW\*(C`EVBACKEND_SELECT\*(C'\fR backend, and only supports socket |
3213 | descriptors. This only applies when using Win32 natively, not when using |
3316 | descriptors. This only applies when using Win32 natively, not when using |
3214 | e.g. cygwin. |
3317 | e.g. cygwin. |
3215 | .PP |
3318 | .PP |
|
|
3319 | Lifting these limitations would basically require the full |
|
|
3320 | re-implementation of the I/O system. If you are into these kinds of |
|
|
3321 | things, then note that glib does exactly that for you in a very portable |
|
|
3322 | way (note also that glib is the slowest event library known to man). |
|
|
3323 | .PP |
3216 | There is no supported compilation method available on windows except |
3324 | There is no supported compilation method available on windows except |
3217 | embedding it into other applications. |
3325 | embedding it into other applications. |
3218 | .PP |
3326 | .PP |
3219 | Due to the many, low, and arbitrary limits on the win32 platform and the |
3327 | Due to the many, low, and arbitrary limits on the win32 platform and |
3220 | abysmal performance of winsockets, using a large number of sockets is not |
3328 | the abysmal performance of winsockets, using a large number of sockets |
3221 | recommended (and not reasonable). If your program needs to use more than |
3329 | is not recommended (and not reasonable). If your program needs to use |
3222 | a hundred or so sockets, then likely it needs to use a totally different |
3330 | more than a hundred or so sockets, then likely it needs to use a totally |
3223 | implementation for windows, as libev offers the \s-1POSIX\s0 model, which cannot |
3331 | different implementation for windows, as libev offers the \s-1POSIX\s0 readyness |
3224 | be implemented efficiently on windows (microsoft monopoly games). |
3332 | notification model, which cannot be implemented efficiently on windows |
|
|
3333 | (microsoft monopoly games). |
3225 | .IP "The winsocket select function" 4 |
3334 | .IP "The winsocket select function" 4 |
3226 | .IX Item "The winsocket select function" |
3335 | .IX Item "The winsocket select function" |
3227 | The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it requires |
3336 | The winsocket \f(CW\*(C`select\*(C'\fR function doesn't follow \s-1POSIX\s0 in that it requires |
3228 | socket \fIhandles\fR and not socket \fIfile descriptors\fR. This makes select |
3337 | socket \fIhandles\fR and not socket \fIfile descriptors\fR. This makes select |
3229 | very inefficient, and also requires a mapping from file descriptors |
3338 | very inefficient, and also requires a mapping from file descriptors |
… | |
… | |
3241 | .Sp |
3350 | .Sp |
3242 | Note that winsockets handling of fd sets is O(n), so you can easily get a |
3351 | Note that winsockets handling of fd sets is O(n), so you can easily get a |
3243 | complexity in the O(nA\*^X) range when using win32. |
3352 | complexity in the O(nA\*^X) range when using win32. |
3244 | .IP "Limited number of file descriptors" 4 |
3353 | .IP "Limited number of file descriptors" 4 |
3245 | .IX Item "Limited number of file descriptors" |
3354 | .IX Item "Limited number of file descriptors" |
3246 | Windows has numerous arbitrary (and low) limits on things. Early versions |
3355 | Windows has numerous arbitrary (and low) limits on things. |
3247 | of winsocket's select only supported waiting for a max. of \f(CW64\fR handles |
3356 | .Sp |
3248 | (probably owning to the fact that all windows kernels can only wait for |
3357 | Early versions of winsocket's select only supported waiting for a maximum |
3249 | \&\f(CW64\fR things at the same time internally; microsoft recommends spawning a |
3358 | of \f(CW64\fR handles (probably owning to the fact that all windows kernels |
3250 | chain of threads and wait for 63 handles and the previous thread in each). |
3359 | can only wait for \f(CW64\fR things at the same time internally; microsoft |
|
|
3360 | recommends spawning a chain of threads and wait for 63 handles and the |
|
|
3361 | previous thread in each. Great). |
3251 | .Sp |
3362 | .Sp |
3252 | Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR |
3363 | Newer versions support more handles, but you need to define \f(CW\*(C`FD_SETSIZE\*(C'\fR |
3253 | to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select |
3364 | to some high number (e.g. \f(CW2048\fR) before compiling the winsocket select |
3254 | call (which might be in libev or elsewhere, for example, perl does its own |
3365 | call (which might be in libev or elsewhere, for example, perl does its own |
3255 | select emulation on windows). |
3366 | select emulation on windows). |
… | |
… | |
3263 | .Sp |
3374 | .Sp |
3264 | This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on |
3375 | This might get you to about \f(CW512\fR or \f(CW2048\fR sockets (depending on |
3265 | windows version and/or the phase of the moon). To get more, you need to |
3376 | windows version and/or the phase of the moon). To get more, you need to |
3266 | wrap all I/O functions and provide your own fd management, but the cost of |
3377 | wrap all I/O functions and provide your own fd management, but the cost of |
3267 | calling select (O(nA\*^X)) will likely make this unworkable. |
3378 | calling select (O(nA\*^X)) will likely make this unworkable. |
|
|
3379 | .SH "PORTABILITY REQUIREMENTS" |
|
|
3380 | .IX Header "PORTABILITY REQUIREMENTS" |
|
|
3381 | In addition to a working ISO-C implementation, libev relies on a few |
|
|
3382 | additional extensions: |
|
|
3383 | .ie n .IP """sig_atomic_t volatile"" must be thread-atomic as well" 4 |
|
|
3384 | .el .IP "\f(CWsig_atomic_t volatile\fR must be thread-atomic as well" 4 |
|
|
3385 | .IX Item "sig_atomic_t volatile must be thread-atomic as well" |
|
|
3386 | The type \f(CW\*(C`sig_atomic_t volatile\*(C'\fR (or whatever is defined as |
|
|
3387 | \&\f(CW\*(C`EV_ATOMIC_T\*(C'\fR) must be atomic w.r.t. accesses from different |
|
|
3388 | threads. This is not part of the specification for \f(CW\*(C`sig_atomic_t\*(C'\fR, but is |
|
|
3389 | believed to be sufficiently portable. |
|
|
3390 | .ie n .IP """sigprocmask"" must work in a threaded environment" 4 |
|
|
3391 | .el .IP "\f(CWsigprocmask\fR must work in a threaded environment" 4 |
|
|
3392 | .IX Item "sigprocmask must work in a threaded environment" |
|
|
3393 | Libev uses \f(CW\*(C`sigprocmask\*(C'\fR to temporarily block signals. This is not |
|
|
3394 | allowed in a threaded program (\f(CW\*(C`pthread_sigmask\*(C'\fR has to be used). Typical |
|
|
3395 | pthread implementations will either allow \f(CW\*(C`sigprocmask\*(C'\fR in the \*(L"main |
|
|
3396 | thread\*(R" or will block signals process-wide, both behaviours would |
|
|
3397 | be compatible with libev. Interaction between \f(CW\*(C`sigprocmask\*(C'\fR and |
|
|
3398 | \&\f(CW\*(C`pthread_sigmask\*(C'\fR could complicate things, however. |
|
|
3399 | .Sp |
|
|
3400 | The most portable way to handle signals is to block signals in all threads |
|
|
3401 | except the initial one, and run the default loop in the initial thread as |
|
|
3402 | well. |
|
|
3403 | .ie n .IP """long"" must be large enough for common memory allocation sizes" 4 |
|
|
3404 | .el .IP "\f(CWlong\fR must be large enough for common memory allocation sizes" 4 |
|
|
3405 | .IX Item "long must be large enough for common memory allocation sizes" |
|
|
3406 | To improve portability and simplify using libev, libev uses \f(CW\*(C`long\*(C'\fR |
|
|
3407 | internally instead of \f(CW\*(C`size_t\*(C'\fR when allocating its data structures. On |
|
|
3408 | non-POSIX systems (Microsoft...) this might be unexpectedly low, but |
|
|
3409 | is still at least 31 bits everywhere, which is enough for hundreds of |
|
|
3410 | millions of watchers. |
|
|
3411 | .ie n .IP """double"" must hold a time value in seconds with enough accuracy" 4 |
|
|
3412 | .el .IP "\f(CWdouble\fR must hold a time value in seconds with enough accuracy" 4 |
|
|
3413 | .IX Item "double must hold a time value in seconds with enough accuracy" |
|
|
3414 | The type \f(CW\*(C`double\*(C'\fR is used to represent timestamps. It is required to |
|
|
3415 | have at least 51 bits of mantissa (and 9 bits of exponent), which is good |
|
|
3416 | enough for at least into the year 4000. This requirement is fulfilled by |
|
|
3417 | implementations implementing \s-1IEEE\s0 754 (basically all existing ones). |
|
|
3418 | .PP |
|
|
3419 | If you know of other additional requirements drop me a note. |
3268 | .SH "AUTHOR" |
3420 | .SH "AUTHOR" |
3269 | .IX Header "AUTHOR" |
3421 | .IX Header "AUTHOR" |
3270 | Marc Lehmann <libev@schmorp.de>. |
3422 | Marc Lehmann <libev@schmorp.de>. |
3271 | .SH "POD ERRORS" |
3423 | .SH "POD ERRORS" |
3272 | .IX Header "POD ERRORS" |
3424 | .IX Header "POD ERRORS" |
3273 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
3425 | Hey! \fBThe above document had some coding errors, which are explained below:\fR |
3274 | .IP "Around line 2996:" 4 |
3426 | .IP "Around line 3052:" 4 |
3275 | .IX Item "Around line 2996:" |
3427 | .IX Item "Around line 3052:" |
3276 | You forgot a '=back' before '=head2' |
3428 | You forgot a '=back' before '=head2' |