NonBlockingFileIO

NonBlockingFileIO is a helper that allows you to read files without blocking the calling thread.

NonBlockingFileIO.swift:36
struct NonBlockingFileIO

It is worth noting that kqueue, epoll or poll returning claiming a file is readable does not mean that the data is already available in the kernel’s memory. In other words, a read from a file can still block even if reported as readable. This behaviour is also documented behaviour:

  • poll: “Regular files shall always poll TRUE for reading and writing.”

  • epoll: “epoll is simply a faster poll(2), and can be used wherever the latter is used since it shares the same semantics.”

  • kqueue: “Returns when the file pointer is not at the end of file.”

NonBlockingFileIO helps to work around this issue by maintaining its own thread pool that is used to read the data from the files into memory. It will then hand the (in-memory) data back which makes it available without the possibility of blocking.