Blame docs/src/poll.rst

Packit b5b901
Packit b5b901
.. _poll:
Packit b5b901
Packit b5b901
:c:type:`uv_poll_t` --- Poll handle
Packit b5b901
===================================
Packit b5b901
Packit b5b901
Poll handles are used to watch file descriptors for readability,
Packit b5b901
writability and disconnection similar to the purpose of :man:`poll(2)`.
Packit b5b901
Packit b5b901
The purpose of poll handles is to enable integrating external libraries that
Packit b5b901
rely on the event loop to signal it about the socket status changes, like
Packit b5b901
c-ares or libssh2. Using uv_poll_t for any other purpose is not recommended;
Packit b5b901
:c:type:`uv_tcp_t`, :c:type:`uv_udp_t`, etc. provide an implementation that is faster and
Packit b5b901
more scalable than what can be achieved with :c:type:`uv_poll_t`, especially on
Packit b5b901
Windows.
Packit b5b901
Packit b5b901
It is possible that poll handles occasionally signal that a file descriptor is
Packit b5b901
readable or writable even when it isn't. The user should therefore always
Packit b5b901
be prepared to handle EAGAIN or equivalent when it attempts to read from or
Packit b5b901
write to the fd.
Packit b5b901
Packit b5b901
It is not okay to have multiple active poll handles for the same socket, this
Packit b5b901
can cause libuv to busyloop or otherwise malfunction.
Packit b5b901
Packit b5b901
The user should not close a file descriptor while it is being polled by an
Packit b5b901
active poll handle. This can cause the handle to report an error,
Packit b5b901
but it might also start polling another socket. However the fd can be safely
Packit b5b901
closed immediately after a call to :c:func:`uv_poll_stop` or :c:func:`uv_close`.
Packit b5b901
Packit b5b901
.. note::
Packit b5b901
    On windows only sockets can be polled with poll handles. On Unix any file
Packit b5b901
    descriptor that would be accepted by :man:`poll(2)` can be used.
Packit b5b901
Packit b5b901
.. note::
Packit b5b901
    On AIX, watching for disconnection is not supported.
Packit b5b901
Packit b5b901
Data types
Packit b5b901
----------
Packit b5b901
Packit b5b901
.. c:type:: uv_poll_t
Packit b5b901
Packit b5b901
    Poll handle type.
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_poll_cb)(uv_poll_t* handle, int status, int events)
Packit b5b901
Packit b5b901
    Type definition for callback passed to :c:func:`uv_poll_start`.
Packit b5b901
Packit b5b901
.. c:type:: uv_poll_event
Packit b5b901
Packit b5b901
    Poll event types
Packit b5b901
Packit b5b901
    ::
Packit b5b901
Packit b5b901
        enum uv_poll_event {
Packit b5b901
            UV_READABLE = 1,
Packit b5b901
            UV_WRITABLE = 2,
Packit b5b901
            UV_DISCONNECT = 4,
Packit b5b901
            UV_PRIORITIZED = 8
Packit b5b901
        };
Packit b5b901
Packit b5b901
Packit b5b901
Public members
Packit b5b901
^^^^^^^^^^^^^^
Packit b5b901
Packit b5b901
N/A
Packit b5b901
Packit b5b901
.. seealso:: The :c:type:`uv_handle_t` members also apply.
Packit b5b901
Packit b5b901
Packit b5b901
API
Packit b5b901
---
Packit b5b901
Packit b5b901
.. c:function:: int uv_poll_init(uv_loop_t* loop, uv_poll_t* handle, int fd)
Packit b5b901
Packit b5b901
    Initialize the handle using a file descriptor.
Packit b5b901
Packit b5b901
    .. versionchanged:: 1.2.2 the file descriptor is set to non-blocking mode.
Packit b5b901
Packit b5b901
.. c:function:: int uv_poll_init_socket(uv_loop_t* loop, uv_poll_t* handle, uv_os_sock_t socket)
Packit b5b901
Packit b5b901
    Initialize the handle using a socket descriptor. On Unix this is identical
Packit b5b901
    to :c:func:`uv_poll_init`. On windows it takes a SOCKET handle.
Packit b5b901
Packit b5b901
    .. versionchanged:: 1.2.2 the socket is set to non-blocking mode.
Packit b5b901
Packit b5b901
.. c:function:: int uv_poll_start(uv_poll_t* handle, int events, uv_poll_cb cb)
Packit b5b901
Packit b5b901
    Starts polling the file descriptor. `events` is a bitmask made up of
Packit b5b901
    UV_READABLE, UV_WRITABLE, UV_PRIORITIZED and UV_DISCONNECT. As soon as an
Packit b5b901
    event is detected the callback will be called with `status` set to 0, and the
Packit b5b901
    detected events set on the `events` field.
Packit b5b901
Packit b5b901
    The UV_PRIORITIZED event is used to watch for sysfs interrupts or TCP out-of-band
Packit b5b901
    messages.
Packit b5b901
Packit b5b901
    The UV_DISCONNECT event is optional in the sense that it may not be
Packit b5b901
    reported and the user is free to ignore it, but it can help optimize the shutdown
Packit b5b901
    path because an extra read or write call might be avoided.
Packit b5b901
Packit b5b901
    If an error happens while polling, `status` will be < 0 and corresponds
Packit b5b901
    with one of the UV_E* error codes (see :ref:`errors`). The user should
Packit b5b901
    not close the socket while the handle is active. If the user does that
Packit b5b901
    anyway, the callback *may* be called reporting an error status, but this
Packit b5b901
    is **not** guaranteed.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        Calling :c:func:`uv_poll_start` on a handle that is already active is fine. Doing so
Packit b5b901
        will update the events mask that is being watched for.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        Though UV_DISCONNECT can be set, it is unsupported on AIX and as such will not be set
Packit b5b901
        on the `events` field in the callback.
Packit b5b901
Packit b5b901
    .. versionchanged:: 1.9.0 Added the UV_DISCONNECT event.
Packit b5b901
    .. versionchanged:: 1.14.0 Added the UV_PRIORITIZED event.
Packit b5b901
Packit b5b901
.. c:function:: int uv_poll_stop(uv_poll_t* poll)
Packit b5b901
Packit b5b901
    Stop polling the file descriptor, the callback will no longer be called.
Packit b5b901
Packit b5b901
.. seealso:: The :c:type:`uv_handle_t` API functions also apply.