lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Oct 18, 2012 at 08:59:04AM -0500, Javier Guerra Giraldez wrote:
> On Wed, Oct 17, 2012 at 11:05 PM, William Ahern
> <william@25thandclement.com> wrote:
> > You can't poll on _regular_ files. It's actually a POSIX requirement that
> > regular files always signal immediate readiness for reading and writing.
> 
> yeah, i've heard similar things.  unfortunately, i haven't found where in
> the standard is that defined. Also, it's not clear if that "regular files
> are always ready" requirement is just for select(), or also for other
> polling syscalls.

>From the description for poll:

	Regular files shall always poll TRUE for reading and writing.

>From the description for select:

	File descriptors associated with regular files shall always select
	true for ready to read, ready to write, and error conditions.

You can see the latest standard, Issue 7, here:

	http://pubs.opengroup.org/onlinepubs/9699919799/

For the definition for poll and select, goto System Interfaces in the upper
left frame, and then System Interfaces again in the lower left. If you
browse the Open Group website you'll find a form allowing you to download
the entire standard in HTML format, and I keep a local copy so I can use
grep.

> in short, is there _any_ way to really check readyness of regular files?

Define readiness. If you want a notification to tell you that a read call
won't cause the thread to sleep because of disk I/O (to load a block, or to
re-load a buffered block from swap), then no. Poll readiness is tentative.
Even if a socket polls ready for reading, there might not be anything in the
buffer when attempting a read (because some other thread drained it, or
because like on Linux packet checksums can cause a packet to get discarded
after a waking thread has been notified). There's no equivalent to EAGAIN
for regular files.

One solution is to connect a regular file to a socket. For example, a small
thread pool to move data from a file to one end of a socket pair. You can
use sendfile or splice to make it zero-copy, although I've only tested OS X
and Linux. I can't confirm whether the FreeBSD or Solaris sendfile
implementations support unix domain sockets.

If you mean ready as-in your file pointer is at the end of the file and you
want to be notified when more data is [potentially] available, then, yes.
You can use inotify on Linux, kqueue + EVFILT_VNODE on BSD, and
port_associate + PORT_SOURCE_FILE on Solaris. I support all three in cqueues
within the `notify' module

	http://25thandclement.com/~william/projects/cqueues.html

which is fully documented in

	http://25thandclement.com/~william/projects/cqueues.pdf

The low-level C abstraction is in cqueues/src/lib/notify.c.