<div dir="ltr"><p dir="ltr">I think I didn't explain myself correctly, so let me give a different example.</p><div>Let's make a file descriptor that counts to 9.</div><div><br></div><div>I.e, we want to emulate the behavior:</div>
<div><br></div><div><font face="courier new, monospace"> int fd = make_counter_fd();</font></div><div><font face="courier new, monospace"> assert(write(fd, buf, 100) == 11);</font></div><div><font face="courier new, monospace"> assert(strcmp(buf, "0123456789") == 0);</font></div>
<div><br></div><div>Let me describe a very simple version of the main poll loop:</div><div><br></div><div><font face="courier new, monospace"> struct poll_cb {</font></div><div><font face="courier new, monospace"> int fd;</font></div>
<div><font face="courier new, monospace"> int (*read_cb)(int fd);</font></div><div><font face="courier new, monospace"> int (*write_cb)(int fd);</font></div><div><font face="courier new, monospace"> };</font></div>
<div><font face="courier new, monospace"> while (poll(&pfd, nr, -1) != -1) {</font></div><div><font face="courier new, monospace"> for (int i=0; i < nr; i++) {</font></div><div><font face="courier new, monospace"> if (pfd[i].revent |= POLLIN)</font></div>
<div><font face="courier new, monospace"> poll_cbs[i].read_cb(pfd[i].fd);</font></div><div><font face="courier new, monospace"> if (pfd[i].revent |= POLLOUT) </font></div><div><font face="courier new, monospace"> poll_cbs[i].write_cb(pfd[i].fd);<br>
</font></div><div><font face="courier new, monospace"> if (pfd[i].revent |= POLLHUP)</font></div><div><font face="courier new, monospace"> pfds[i].fd = -1; // don't poll again</font></div><div>
<font face="courier new, monospace"> }</font></div><div><font face="courier new, monospace"> }</font></div><div><br></div><div>Now, in order to emulate the 0-9 file descriptor, we'll associate to a "/dev/zero" a read callback function that looks roughly like</div>
<div><br></div><div><font face="courier new, monospace"> int written = 0;</font></div><div><font face="courier new, monospace"> char buf[10];</font></div><div><font face="courier new, monospace"> int zero_to_nine_cb(int fd) {</font></div>
<div><font face="courier new, monospace"> for (int i=0; i< 10; i++) buf[i] = '0' + i;</font></div><div><font face="courier new, monospace"> close(fd); // note, we never touched the fd</font></div>
<div><font face="courier new, monospace"> }</font></div><div><br></div><div>But I have to use a file descriptor! Otherwise poll will have no reason to call my callback.</div>
<div class="gmail_quote">On Jun 19, <a href="tel:2013" value="+9722013" target="_blank">2013</a> 7:47 AM, "Shachar Shemesh" <<a href="mailto:shachar@shemesh.biz" target="_blank">shachar@shemesh.biz</a>> wrote:<br type="attribution">
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div style="direction:ltr" bgcolor="#FFFFFF" text="#000000">
<div>On 18/06/13 22:16, Elazar Leibovich
wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">I'm using it as a fake "always non-blocking" file
descriptor.
<div><br>
</div>
<div>My main libevent-like poll loop looks like:</div>
<div><br>
</div>
<div><font face="courier new, monospace"> poll(fds)</font></div>
<div><font face="courier new, monospace"> for fd in
fds:</font></div>
<div><font face="courier new, monospace"> if
fd.revents | POLLIN:</font></div>
<div><font face="courier new, monospace">
fd.read_callback()</font></div>
<div><font face="courier new, monospace"> if
fd.revents | POLLOUT:</font></div>
<div><font face="courier new, monospace">
fd.write_callback()</font></div>
<div><br>
</div>
<div>Now let's say I want a fake filedescriptor that
always reads 'z's (a sleepy fd).</div>
</div>
</blockquote>
Why? What you just did was to turn the whole thing into a
non-sleeping loop. If that's the case, simply call poll with a zero
timeout, so it won't sleep, and call your callback at the end of
each loop. No need to artificially introduce another file descriptor
into the mix.<br>
<br>
Mind you, I still don't understand WHY you'd want such a thing. This
code will, by definition, consume 100% CPU all the time.<br>
<br>
Shachar<br>
</div>
</blockquote></div>
</div>