DoxigAlpha

pollPosix

Function parameters

Parameters

#
self:*Self
nanoseconds:?u64

Type definitions in this namespace

Types

#
AnyReader
Deprecated in favor of `Reader`.
AnyWriter
Deprecated in favor of `Writer`.

Deprecated in favor of `Reader`.

Functions

#
GenericReader
Deprecated in favor of `Reader`.
GenericWriter
Deprecated in favor of `Writer`.
FixedBufferStream
Deprecated in favor of `Reader`.
fixedBufferStream
Deprecated in favor of `Reader`.
CountingReader
Deprecated with no replacement; inefficient pattern
countingReader
Deprecated with no replacement; inefficient pattern
PollFiles
Given an enum, returns a struct with fields of that enum, each field

Deprecated in favor of `Writer.Discarding`.

Values

#
null_writer
Deprecated in favor of `Writer.Discarding`.

Source

Implementation

#
fn pollPosix(self: *Self, nanoseconds: ?u64) !bool {
    const gpa = self.gpa;
    // We ask for ensureUnusedCapacity with this much extra space. This
    // has more of an effect on small reads because once the reads
    // start to get larger the amount of space an ArrayList will
    // allocate grows exponentially.
    const bump_amt = 512;

    const err_mask = posix.POLL.ERR | posix.POLL.NVAL | posix.POLL.HUP;

    const events_len = try posix.poll(&self.poll_fds, if (nanoseconds) |ns|
        std.math.cast(i32, ns / std.time.ns_per_ms) orelse std.math.maxInt(i32)
    else
        -1);
    if (events_len == 0) {
        for (self.poll_fds) |poll_fd| {
            if (poll_fd.fd != -1) return true;
        } else return false;
    }

    var keep_polling = false;
    for (&self.poll_fds, &self.readers) |*poll_fd, *r| {
        // Try reading whatever is available before checking the error
        // conditions.
        // It's still possible to read after a POLL.HUP is received,
        // always check if there's some data waiting to be read first.
        if (poll_fd.revents & posix.POLL.IN != 0) {
            const buf = try writableSliceGreedyAlloc(r, gpa, bump_amt);
            const amt = posix.read(poll_fd.fd, buf) catch |err| switch (err) {
                error.BrokenPipe => 0, // Handle the same as EOF.
                else => |e| return e,
            };
            advanceBufferEnd(r, amt);
            if (amt == 0) {
                // Remove the fd when the EOF condition is met.
                poll_fd.fd = -1;
            } else {
                keep_polling = true;
            }
        } else if (poll_fd.revents & err_mask != 0) {
            // Exclude the fds that signaled an error.
            poll_fd.fd = -1;
        } else if (poll_fd.fd != -1) {
            keep_polling = true;
        }
    }
    return keep_polling;
}