uvw  1.3.0
Public Member Functions | Static Public Member Functions | List of all members
uvw::Loop Class Referencefinal

The Loop class. More...

#include <loop.hpp>

Inheritance diagram for uvw::Loop:
Inheritance graph
[legend]
Collaboration diagram for uvw::Loop:
Collaboration graph
[legend]

Public Member Functions

template<typename... Args>
void configure (Configure flag, Args &&... args)
 Sets additional loop options. More...
 
template<typename R , typename... Args>
std::enable_if_t< std::is_base_of< BaseHandle, R >::value, std::shared_ptr< R > > resource (Args &&... args)
 Creates resources of handles' types. More...
 
template<typename R , typename... Args>
std::enable_if_t< not std::is_base_of< BaseHandle, R >::value, std::shared_ptr< R > > resource (Args &&... args)
 Creates resources of types other than handles' ones. More...
 
void close ()
 Releases all internal loop resources. More...
 
template<Mode mode = Mode::DEFAULT>
bool run () noexcept
 Runs the event loop. More...
 
bool alive () const noexcept
 Checks if there are active resources. More...
 
void stop () noexcept
 Stops the event loop. More...
 
int descriptor () const noexcept
 Get backend file descriptor. More...
 
std::pair< bool, Time > timeout () const noexcept
 Gets the poll timeout. More...
 
Time now () const noexcept
 Returns the current timestamp in milliseconds. More...
 
void update () const noexcept
 Updates the event loop’s concept of now. More...
 
void walk (std::function< void(BaseHandle &)> callback)
 Walks the list of handles. More...
 
void fork () noexcept
 Reinitialize any kernel state necessary in the child process after a fork(2) system call. More...
 
- Public Member Functions inherited from uvw::Emitter< Loop >
Connection< E > on (Listener< E > f)
 Registers a long-lived listener with the event emitter. More...
 
Connection< E > once (Listener< E > f)
 Registers a short-lived listener with the event emitter. More...
 
void erase (Connection< E > conn) noexcept
 Disconnects a listener from the event emitter. More...
 
void clear () noexcept
 Disconnects all the listeners for the given event type.
 
void clear () noexcept
 Disconnects all the listeners.
 
bool empty () const noexcept
 Checks if there are listeners registered for the specific event. More...
 
bool empty () const noexcept
 Checks if there are listeners registered with the event emitter. More...
 

Static Public Member Functions

static std::shared_ptr< Loopcreate ()
 Initializes a new Loop instance. More...
 
static std::shared_ptr< LoopgetDefault ()
 Gets the initialized default loop. More...
 

Detailed Description

The Loop class.

The event loop is the central part of uvw's functionalities, as well as libuv's ones.
It takes care of polling for I/O and scheduling callbacks to be run based on different sources of events.

Definition at line 142 of file loop.hpp.

Member Function Documentation

◆ alive()

bool uvw::Loop::alive ( ) const
inlinenoexcept

Checks if there are active resources.

Returns
True if there are active resources in the loop.

Definition at line 314 of file loop.hpp.

◆ close()

void uvw::Loop::close ( )
inline

Releases all internal loop resources.

Call this function only when the loop has finished executing and all open handles and requests have been closed, or the loop will emit an error.

An ErrorEvent will be emitted in case of errors.

Definition at line 280 of file loop.hpp.

◆ configure()

template<typename... Args>
void uvw::Loop::configure ( Configure  flag,
Args &&...  args 
)
inline

Sets additional loop options.

You should normally call this before the first call to uv_run() unless mentioned otherwise.
Supported options:

  • Loop::Configure::BLOCK_SIGNAL: Block a signal when polling for new events. A second argument is required and it is the signal number.

An ErrorEvent will be emitted in case of errors.

See the official documentation for further details.

Definition at line 232 of file loop.hpp.

◆ create()

static std::shared_ptr<Loop> uvw::Loop::create ( )
inlinestatic

Initializes a new Loop instance.

Returns
A pointer to the newly created loop.

Definition at line 161 of file loop.hpp.

◆ descriptor()

int uvw::Loop::descriptor ( ) const
inlinenoexcept

Get backend file descriptor.

Only kqueue, epoll and event ports are supported.
This can be used in conjunction with run<Loop::Mode::NOWAIT>() to poll in one thread and run the event loop’s callbacks in another.

Returns
The backend file descriptor.

Definition at line 339 of file loop.hpp.

◆ fork()

void uvw::Loop::fork ( )
inlinenoexcept

Reinitialize any kernel state necessary in the child process after a fork(2) system call.

Previously started watchers will continue to be started in the child process.

It is necessary to explicitly call this function on every event loop created in the parent process that you plan to continue to use in the child, including the default loop (even if you don’t continue to use it in the parent). This function must be called before calling any API function using the loop in the child. Failure to do so will result in undefined behaviour, possibly including duplicate events delivered to both parent and child or aborting the child process.

When possible, it is preferred to create a new loop in the child process instead of reusing a loop created in the parent. New loops created in the child process after the fork should not use this function.

Note that this function is not implemented on Windows.
Note also that this function is experimental in libuv. It may contain bugs, and is subject to change or removal. API and ABI stability is not guaranteed.

An ErrorEvent will be emitted in case of errors.

See the official documentation for further details.

Definition at line 430 of file loop.hpp.

◆ getDefault()

static std::shared_ptr<Loop> uvw::Loop::getDefault ( )
inlinestatic

Gets the initialized default loop.

It may return an empty pointer in case of failure.
This function is just a convenient way for having a global loop throughout an application, the default loop is in no way different than the ones initialized with create().
As such, the default loop can be closed with close() so the resources associated with it are freed (even if it is not strictly necessary).

Returns
The initialized default loop.

Definition at line 184 of file loop.hpp.

◆ now()

Time uvw::Loop::now ( ) const
inlinenoexcept

Returns the current timestamp in milliseconds.

The timestamp is cached at the start of the event loop tick.
The timestamp increases monotonically from some arbitrary point in time.
Don’t make assumptions about the starting point, you will only get disappointed.

Returns
The current timestamp in milliseconds (actual type is std::chrono::duration<uint64_t, std::milli>).

Definition at line 366 of file loop.hpp.

◆ resource() [1/2]

template<typename R , typename... Args>
std::enable_if_t<std::is_base_of<BaseHandle, R>::value, std::shared_ptr<R> > uvw::Loop::resource ( Args &&...  args)
inline

Creates resources of handles' types.

This should be used as a default method to create resources.
The arguments are the ones required for the specific resource.

Use it as loop->resource<uvw::TimerHandle>().

Returns
A pointer to the newly created resource.

Definition at line 250 of file loop.hpp.

◆ resource() [2/2]

template<typename R , typename... Args>
std::enable_if_t<not std::is_base_of<BaseHandle, R>::value, std::shared_ptr<R> > uvw::Loop::resource ( Args &&...  args)
inline

Creates resources of types other than handles' ones.

This should be used as a default method to create resources.
The arguments are the ones required for the specific resource.

Use it as loop->resource<uvw::WorkReq>().

Returns
A pointer to the newly created resource.

Definition at line 268 of file loop.hpp.

◆ run()

template<Mode mode = Mode::DEFAULT>
bool uvw::Loop::run ( )
inlinenoexcept

Runs the event loop.

Available modes are:

  • Loop::Mode::DEFAULT: Runs the event loop until there are no more active and referenced handles or requests.
  • Loop::Mode::ONCE: Poll for i/o once. Note that this function blocks if there are no pending callbacks.
  • Loop::Mode::NOWAIT: Poll for i/o once but don’t block if there are no pending callbacks.

See the official documentation for further details.

Returns
True when done, false in all other cases.

Definition at line 304 of file loop.hpp.

◆ stop()

void uvw::Loop::stop ( )
inlinenoexcept

Stops the event loop.

It causes run() to end as soon as possible.
This will happen not sooner than the next loop iteration.
If this function was called before blocking for I/O, the loop won’t block for I/O on this iteration.

Definition at line 326 of file loop.hpp.

◆ timeout()

std::pair<bool, Time> uvw::Loop::timeout ( ) const
inlinenoexcept

Gets the poll timeout.

Returns
A std::pair composed as it follows:
  • A boolean value that is true in case of valid timeout, false otherwise.
  • Milliseconds (std::chrono::duration<uint64_t, std::milli>).

Definition at line 349 of file loop.hpp.

◆ update()

void uvw::Loop::update ( ) const
inlinenoexcept

Updates the event loop’s concept of now.

The current time is cached at the start of the event loop tick in order to reduce the number of time-related system calls.
You won’t normally need to call this function unless you have callbacks that block the event loop for longer periods of time, where longer is somewhat subjective but probably on the order of a millisecond or more.

Definition at line 379 of file loop.hpp.

◆ walk()

void uvw::Loop::walk ( std::function< void(BaseHandle &)>  callback)
inline

Walks the list of handles.

The callback will be executed once for each handle that is still active.

Parameters
callbackA function to be invoked once for each active handle.

Definition at line 390 of file loop.hpp.


The documentation for this class was generated from the following file: