<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
On 08.09.2010 16:02, Masahiro Nakagawa wrote:
<blockquote cite="mid:op.vipvynt5t9qk7k@mac-repeatedly.local"
type="cite"><br>
I am thinking about std.event.
<br>
This module supports system-dependent APIs(kqueue, epoll, etc...)
and
<br>
abstraction layer.
<br>
<br>
</blockquote>
I think the best solution would be a wrapper / abstraction layer for
libev. All of the mentioned APIs (kqueue, epoll, select...) are
broken in some way, so writing a new event loop system in D will be
painful and it will take a long time until it gets stable. Also
libev already has support for more event sources (timer, periodic,
signal, stat, ...).<br>
<br>
Quoting libev documentation:<br>
<br>
EVBACKEND_SELECT<br>
<span class="Apple-style-span" style="border-collapse: separate;
color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style:
normal; font-variant: normal; font-weight: normal; letter-spacing:
normal; line-height: normal; orphans: 2; text-indent: 0px;
text-transform: none; white-space: normal; widows: 2;
word-spacing: 0px; font-size: medium;"><span
class="Apple-style-span" style="border-collapse: collapse;
font-family: Verdana,Arial,sans-serif; text-align: justify;">Not<span
class="Apple-converted-space"> </span><i>completely</i><span
class="Apple-converted-space"> </span>standard, as libev tries
to roll its own fd_set with no limits on the number of fds, but
if that fails, expect a fairly low limit on the number of fds
when using this backend.<br>
</span></span><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0);
font-family: 'Times New Roman'; font-style: normal; font-variant:
normal; font-weight: normal; letter-spacing: normal; line-height:
normal; orphans: 2; text-indent: 0px; text-transform: none;
white-space: normal; widows: 2; word-spacing: 0px; font-size:
medium;"><span class="Apple-style-span" style="border-collapse:
collapse; font-family: Verdana,Arial,sans-serif; text-align:
justify;">This backend maps<span class="Apple-converted-space"> </span><code
style="color: rgb(0, 102, 0); font-family: 'Andale
Mono',monospace;">EV_READ</code><span
class="Apple-converted-space"> </span>to the<span
class="Apple-converted-space"> </span><code style="color:
rgb(0, 102, 0); font-family: 'Andale Mono',monospace;">readfds</code><span
class="Apple-converted-space"> </span>set and<span
class="Apple-converted-space"> </span><code style="color:
rgb(0, 102, 0); font-family: 'Andale Mono',monospace;">EV_WRITE</code><span
class="Apple-converted-space"> </span>to the<span
class="Apple-converted-space"> </span><code style="color:
rgb(0, 102, 0); font-family: 'Andale Mono',monospace;">writefds</code><span
class="Apple-converted-space"> </span>set (and to work around
Microsoft Windows bugs, also onto the<span
class="Apple-converted-space"> </span><code style="color:
rgb(0, 102, 0); font-family: 'Andale Mono',monospace;">exceptfds</code><span
class="Apple-converted-space"> </span>set on that platform).<br>
<br>
EVBACKEND_POLL<br>
</span></span><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0);
font-family: 'Times New Roman'; font-style: normal; font-variant:
normal; font-weight: normal; letter-spacing: normal; line-height:
normal; orphans: 2; text-indent: 0px; text-transform: none;
white-space: normal; widows: 2; word-spacing: 0px; font-size:
medium;"><span class="Apple-style-span" style="border-collapse:
collapse; font-family: Verdana,Arial,sans-serif; text-align:
justify;">It's more complicated than select, but handles sparse
fds better and has no artificial limit on the number of fds you
can use (except it will slow down considerably with a lot of
inactive fds).<br>
<br>
EVBACKEND_EPOLL<br>
</span></span><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0);
font-family: 'Times New Roman'; font-style: normal; font-variant:
normal; font-weight: normal; letter-spacing: normal; line-height:
normal; orphans: 2; text-indent: 0px; text-transform: none;
white-space: normal; widows: 2; word-spacing: 0px; font-size:
medium;"><span class="Apple-style-span" style="border-collapse:
collapse; font-family: Verdana,Arial,sans-serif; text-align:
justify;">
<p style="margin-left: 0px; padding-left: 0px; margin-top: 0px;
padding-top: 0px; margin-bottom: 1ex; padding-bottom: 0px;">The
epoll mechanism deserves honorable mention as the most
misdesigned of the more advanced event mechanisms: mere
annoyances include silently dropping file descriptors,
requiring a system call per change per file descriptor (and
unnecessary guessing of parameters), problems with dup and so
on. The biggest issue is fork races, however - if a program
forks then<span class="Apple-converted-space"> </span><i>both</i><span
class="Apple-converted-space"> </span>parent and child
process have to recreate the epoll set, which can take
considerable time (one syscall per file descriptor) and is of
course hard to detect.</p>
<p style="margin-left: 0px; padding-left: 0px; margin-top: 0px;
padding-top: 0px; margin-bottom: 1ex; padding-bottom: 0px;">Epoll
is also notoriously buggy - embedding epoll fds<span
class="Apple-converted-space"> </span><i>should</i><span
class="Apple-converted-space"> </span>work, but of course<span
class="Apple-converted-space"> </span><i>doesn't</i>, and
epoll just loves to report events for totally<span
class="Apple-converted-space"> </span><i>different</i><span
class="Apple-converted-space"> </span>file descriptors (even
already closed ones, so one cannot even remove them from the
set) than registered in the set (especially on SMP systems).
Libev tries to counter these spurious notifications by
employing an additional generation counter and comparing that
against the events to filter out spurious ones, recreating the
set when required.<br>
</p>
EVBACKEND_KQUEUE<br>
</span></span><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0);
font-family: 'Times New Roman'; font-style: normal; font-variant:
normal; font-weight: normal; letter-spacing: normal; line-height:
normal; orphans: 2; text-indent: 0px; text-transform: none;
white-space: normal; widows: 2; word-spacing: 0px; font-size:
medium;"><span class="Apple-style-span" style="border-collapse:
collapse; font-family: Verdana,Arial,sans-serif; text-align:
justify;">Kqueue deserves special mention, as at the time of
this writing, it was broken on all BSDs except NetBSD (usually
it doesn't work reliably with anything but sockets and pipes,
except on Darwin, where of course it's completely useless).
Unlike epoll, however, whose brokenness is by design, these
kqueue bugs can (and eventually will) be fixed without API
changes to existing programs. For this reason it's not being
"auto-detected" unless you explicitly specify it in the flags
(i.e. using<span class="Apple-converted-space"> </span><code
style="color: rgb(0, 102, 0); font-family: 'Andale
Mono',monospace;">EVBACKEND_KQUEUE</code>) or libev was
compiled on a known-to-be-good (-enough) system like NetBSD.<br>
<br>
</span></span><span class="Apple-style-span"
style="border-collapse: separate; color: rgb(0, 0, 0);
font-family: 'Times New Roman'; font-style: normal; font-variant:
normal; font-weight: normal; letter-spacing: normal; line-height:
normal; orphans: 2; text-indent: 0px; text-transform: none;
white-space: normal; widows: 2; word-spacing: 0px; font-size:
medium;"><span class="Apple-style-span" style="border-collapse:
collapse; font-family: Verdana,Arial,sans-serif; text-align:
justify;"><span class="Apple-converted-space"></span>While
stopping, setting and starting an I/O watcher does never cause
an extra system call as with<span class="Apple-converted-space"> </span><code
style="color: rgb(0, 102, 0); font-family: 'Andale
Mono',monospace;">EVBACKEND_EPOLL</code>, it still adds up to
two event changes per incident. Support for<span
class="Apple-converted-space"> </span><code style="color:
rgb(0, 102, 0); font-family: 'Andale Mono',monospace;">fork ()</code><span
class="Apple-converted-space"> </span>is very bad (but sane,
unlike epoll) and it drops fds silently in similarly
hard-to-detect cases</span></span><br>
<br>
EVBACKEND_PORT<br>
<span class="Apple-style-span" style="border-collapse: separate;
color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style:
normal; font-variant: normal; font-weight: normal; letter-spacing:
normal; line-height: normal; orphans: 2; text-indent: 0px;
text-transform: none; white-space: normal; widows: 2;
word-spacing: 0px; font-size: medium;"><span
class="Apple-style-span" style="border-collapse: collapse;
font-family: Verdana,Arial,sans-serif; text-align: justify;">
<p style="margin-left: 0px; padding-left: 0px; margin-top: 0px;
padding-top: 0px; margin-bottom: 1ex; padding-bottom: 0px;">This
uses the Solaris 10 event port mechanism. As with everything
on Solaris, it's really slow, but it still scales very well
(O(active_fds)).</p>
<p style="margin-left: 0px; padding-left: 0px; margin-top: 0px;
padding-top: 0px; margin-bottom: 1ex; padding-bottom: 0px;">Please
note that Solaris event ports can deliver a lot of spurious
notifications, so you need to use non-blocking I/O or other
means to avoid blocking when no data (or space) is available.</p>
<p style="margin-left: 0px; padding-left: 0px; margin-top: 0px;
padding-top: 0px; margin-bottom: 1ex; padding-bottom: 0px;">While
this backend scales well, it requires one system call per
active file descriptor per loop iteration. For small and
medium numbers of file descriptors a "slow"<span
class="Apple-converted-space"> </span><code style="color:
rgb(0, 102, 0); font-family: 'Andale Mono',monospace;">EVBACKEND_SELECT</code><span
class="Apple-converted-space"> </span>or<span
class="Apple-converted-space"> </span><code style="color:
rgb(0, 102, 0); font-family: 'Andale Mono',monospace;">EVBACKEND_POLL</code><span
class="Apple-converted-space"> </span>backend might perform
better</p>
</span></span><br>
<pre class="moz-signature" cols="72">--
Johannes Pfau</pre>
</body>
</html>