Blame docs/src/loop.rst

Packit b5b901
Packit b5b901
.. _loop:
Packit b5b901
Packit b5b901
:c:type:`uv_loop_t` --- Event loop
Packit b5b901
==================================
Packit b5b901
Packit b5b901
The event loop is the central part of libuv's functionality. It takes care
Packit b5b901
of polling for i/o and scheduling callbacks to be run based on different sources
Packit b5b901
of events.
Packit b5b901
Packit b5b901
Packit b5b901
Data types
Packit b5b901
----------
Packit b5b901
Packit b5b901
.. c:type:: uv_loop_t
Packit b5b901
Packit b5b901
    Loop data type.
Packit b5b901
Packit b5b901
.. c:type:: uv_run_mode
Packit b5b901
Packit b5b901
    Mode used to run the loop with :c:func:`uv_run`.
Packit b5b901
Packit b5b901
    ::
Packit b5b901
Packit b5b901
        typedef enum {
Packit b5b901
            UV_RUN_DEFAULT = 0,
Packit b5b901
            UV_RUN_ONCE,
Packit b5b901
            UV_RUN_NOWAIT
Packit b5b901
        } uv_run_mode;
Packit b5b901
Packit b5b901
.. c:type:: void (*uv_walk_cb)(uv_handle_t* handle, void* arg)
Packit b5b901
Packit b5b901
    Type definition for callback passed to :c:func:`uv_walk`.
Packit b5b901
Packit b5b901
Packit b5b901
Public members
Packit b5b901
^^^^^^^^^^^^^^
Packit b5b901
Packit b5b901
.. c:member:: void* uv_loop_t.data
Packit b5b901
Packit b5b901
    Space for user-defined arbitrary data. libuv does not use and does not
Packit b5b901
    touch this field.
Packit b5b901
Packit b5b901
Packit b5b901
API
Packit b5b901
---
Packit b5b901
Packit b5b901
.. c:function:: int uv_loop_init(uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Initializes the given `uv_loop_t` structure.
Packit b5b901
Packit b5b901
.. c:function:: int uv_loop_configure(uv_loop_t* loop, uv_loop_option option, ...)
Packit b5b901
Packit b5b901
    .. versionadded:: 1.0.2
Packit b5b901
Packit b5b901
    Set additional loop options.  You should normally call this before the
Packit b5b901
    first call to :c:func:`uv_run` unless mentioned otherwise.
Packit b5b901
Packit b5b901
    Returns 0 on success or a UV_E* error code on failure.  Be prepared to
Packit b5b901
    handle UV_ENOSYS; it means the loop option is not supported by the platform.
Packit b5b901
Packit b5b901
    Supported options:
Packit b5b901
Packit b5b901
    - UV_LOOP_BLOCK_SIGNAL: Block a signal when polling for new events.  The
Packit b5b901
      second argument to :c:func:`uv_loop_configure` is the signal number.
Packit b5b901
Packit b5b901
      This operation is currently only implemented for SIGPROF signals,
Packit b5b901
      to suppress unnecessary wakeups when using a sampling profiler.
Packit b5b901
      Requesting other signals will fail with UV_EINVAL.
Packit b5b901
Packit b5b901
.. c:function:: int uv_loop_close(uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Releases all internal loop resources. Call this function only when the loop
Packit b5b901
    has finished executing and all open handles and requests have been closed,
Packit b5b901
    or it will return UV_EBUSY. After this function returns, the user can free
Packit b5b901
    the memory allocated for the loop.
Packit b5b901
Packit b5b901
.. c:function:: uv_loop_t* uv_default_loop(void)
Packit b5b901
Packit b5b901
    Returns the initialized default loop. It may return NULL in case of
Packit b5b901
    allocation failure.
Packit b5b901
Packit b5b901
    This function is just a convenient way for having a global loop throughout
Packit b5b901
    an application, the default loop is in no way different than the ones
Packit b5b901
    initialized with :c:func:`uv_loop_init`. As such, the default loop can (and
Packit b5b901
    should) be closed with :c:func:`uv_loop_close` so the resources associated
Packit b5b901
    with it are freed.
Packit b5b901
Packit b5b901
    .. warning::
Packit b5b901
        This function is not thread safe.
Packit b5b901
Packit b5b901
.. c:function:: int uv_run(uv_loop_t* loop, uv_run_mode mode)
Packit b5b901
Packit b5b901
    This function runs the event loop. It will act differently depending on the
Packit b5b901
    specified mode:
Packit b5b901
Packit b5b901
    - UV_RUN_DEFAULT: Runs the event loop until there are no more active and
Packit b5b901
      referenced handles or requests. Returns non-zero if :c:func:`uv_stop`
Packit b5b901
      was called and there are still active handles or requests.  Returns
Packit b5b901
      zero in all other cases.
Packit b5b901
    - UV_RUN_ONCE: Poll for i/o once. Note that this function blocks if
Packit b5b901
      there are no pending callbacks. Returns zero when done (no active handles
Packit b5b901
      or requests left), or non-zero if more callbacks are expected (meaning
Packit b5b901
      you should run the event loop again sometime in the future).
Packit b5b901
    - UV_RUN_NOWAIT: Poll for i/o once but don't block if there are no
Packit b5b901
      pending callbacks. Returns zero if done (no active handles
Packit b5b901
      or requests left), or non-zero if more callbacks are expected (meaning
Packit b5b901
      you should run the event loop again sometime in the future).
Packit b5b901
Packit Service e08953
    :c:func:`uv_run` is not reentrant. It must not be called from a callback.
Packit Service e08953
Packit b5b901
.. c:function:: int uv_loop_alive(const uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Returns non-zero if there are referenced active handles, active
Packit b5b901
    requests or closing handles in the loop.
Packit b5b901
Packit b5b901
.. c:function:: void uv_stop(uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Stop the event loop, causing :c:func:`uv_run` to end as soon as
Packit b5b901
    possible. This will happen not sooner than the next loop iteration.
Packit b5b901
    If this function was called before blocking for i/o, the loop won't block
Packit b5b901
    for i/o on this iteration.
Packit b5b901
Packit b5b901
.. c:function:: size_t uv_loop_size(void)
Packit b5b901
Packit b5b901
    Returns the size of the `uv_loop_t` structure. Useful for FFI binding
Packit b5b901
    writers who don't want to know the structure layout.
Packit b5b901
Packit b5b901
.. c:function:: int uv_backend_fd(const uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Get backend file descriptor. Only kqueue, epoll and event ports are
Packit b5b901
    supported.
Packit b5b901
Packit b5b901
    This can be used in conjunction with `uv_run(loop, UV_RUN_NOWAIT)` to
Packit b5b901
    poll in one thread and run the event loop's callbacks in another see
Packit b5b901
    test/test-embed.c for an example.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        Embedding a kqueue fd in another kqueue pollset doesn't work on all platforms. It's not
Packit b5b901
        an error to add the fd but it never generates events.
Packit b5b901
Packit b5b901
.. c:function:: int uv_backend_timeout(const uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Get the poll timeout. The return value is in milliseconds, or -1 for no
Packit b5b901
    timeout.
Packit b5b901
Packit b5b901
.. c:function:: uint64_t uv_now(const uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Return the current timestamp in milliseconds. The timestamp is cached at
Packit b5b901
    the start of the event loop tick, see :c:func:`uv_update_time` for details
Packit b5b901
    and rationale.
Packit b5b901
Packit b5b901
    The timestamp increases monotonically from some arbitrary point in time.
Packit b5b901
    Don't make assumptions about the starting point, you will only get
Packit b5b901
    disappointed.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
        Use :c:func:`uv_hrtime` if you need sub-millisecond granularity.
Packit b5b901
Packit b5b901
.. c:function:: void uv_update_time(uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Update the event loop's concept of "now". Libuv caches the current time
Packit b5b901
    at the start of the event loop tick in order to reduce the number of
Packit b5b901
    time-related system calls.
Packit b5b901
Packit b5b901
    You won't normally need to call this function unless you have callbacks
Packit b5b901
    that block the event loop for longer periods of time, where "longer" is
Packit b5b901
    somewhat subjective but probably on the order of a millisecond or more.
Packit b5b901
Packit b5b901
.. c:function:: void uv_walk(uv_loop_t* loop, uv_walk_cb walk_cb, void* arg)
Packit b5b901
Packit b5b901
    Walk the list of handles: `walk_cb` will be executed with the given `arg`.
Packit b5b901
Packit b5b901
.. c:function:: int uv_loop_fork(uv_loop_t* loop)
Packit b5b901
Packit b5b901
    .. versionadded:: 1.12.0
Packit b5b901
Packit b5b901
    Reinitialize any kernel state necessary in the child process after
Packit b5b901
    a :man:`fork(2)` system call.
Packit b5b901
Packit b5b901
    Previously started watchers will continue to be started in the
Packit b5b901
    child process.
Packit b5b901
Packit b5b901
    It is necessary to explicitly call this function on every event
Packit b5b901
    loop created in the parent process that you plan to continue to
Packit b5b901
    use in the child, including the default loop (even if you don't
Packit b5b901
    continue to use it in the parent). This function must be called
Packit b5b901
    before calling :c:func:`uv_run` or any other API function using
Packit b5b901
    the loop in the child. Failure to do so will result in undefined
Packit b5b901
    behaviour, possibly including duplicate events delivered to both
Packit b5b901
    parent and child or aborting the child process.
Packit b5b901
Packit b5b901
    When possible, it is preferred to create a new loop in the child
Packit b5b901
    process instead of reusing a loop created in the parent. New loops
Packit b5b901
    created in the child process after the fork should not use this
Packit b5b901
    function.
Packit b5b901
Packit b5b901
    This function is not implemented on Windows, where it returns ``UV_ENOSYS``.
Packit b5b901
Packit b5b901
    .. caution::
Packit b5b901
Packit b5b901
       This function is experimental. It may contain bugs, and is subject to
Packit b5b901
       change or removal. API and ABI stability is not guaranteed.
Packit b5b901
Packit b5b901
    .. note::
Packit b5b901
Packit b5b901
        On Mac OS X, if directory FS event handles were in use in the
Packit b5b901
        parent process *for any event loop*, the child process will no
Packit b5b901
        longer be able to use the most efficient FSEvent
Packit b5b901
        implementation. Instead, uses of directory FS event handles in
Packit b5b901
        the child will fall back to the same implementation used for
Packit b5b901
        files and on other kqueue-based systems.
Packit b5b901
Packit b5b901
    .. caution::
Packit b5b901
Packit b5b901
       On AIX and SunOS, FS event handles that were already started in
Packit b5b901
       the parent process at the time of forking will *not* deliver
Packit b5b901
       events in the child process; they must be closed and restarted.
Packit b5b901
       On all other platforms, they will continue to work normally
Packit b5b901
       without any further intervention.
Packit b5b901
Packit b5b901
    .. caution::
Packit b5b901
Packit b5b901
       Any previous value returned from :c:func:`uv_backend_fd` is now
Packit b5b901
       invalid. That function must be called again to determine the
Packit b5b901
       correct backend file descriptor.
Packit b5b901
Packit b5b901
.. c:function:: void* uv_loop_get_data(const uv_loop_t* loop)
Packit b5b901
Packit b5b901
    Returns `loop->data`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0
Packit b5b901
Packit b5b901
.. c:function:: void* uv_loop_set_data(uv_loop_t* loop, void* data)
Packit b5b901
Packit b5b901
    Sets `loop->data` to `data`.
Packit b5b901
Packit b5b901
    .. versionadded:: 1.19.0