WPILibC++  2019.3.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
wpi::uv::Loop Class Referencefinal

Event loop. More...

#include <Loop.h>

Inheritance diagram for wpi::uv::Loop:

Public Types

enum  Mode { kDefault = UV_RUN_DEFAULT, kOnce = UV_RUN_ONCE, kNoWait = UV_RUN_NOWAIT }
 
using Time = std::chrono::duration< uint64_t, std::milli >
 

Public Member Functions

 Loop (const private_init &) noexcept
 
 Loop (const Loop &)=delete
 
Loopoperator= (const Loop &)=delete
 
 Loop (Loop &&oth)=delete
 
Loopoperator= (Loop &&oth)=delete
 
void Close ()
 Release all internal loop resources. More...
 
bool Run (Mode mode=kDefault)
 Run the event loop. More...
 
bool IsAlive () const noexcept
 Check if there are active resources. More...
 
void Stop () noexcept
 Stop the event loop. More...
 
int GetDescriptor () const noexcept
 Get backend file descriptor. More...
 
std::pair< bool, Time > GetTimeout () const noexcept
 Get the poll timeout. More...
 
Time Now () const noexcept
 Return the current timestamp in milliseconds. More...
 
void UpdateTime () noexcept
 Update the event loop’s concept of now. More...
 
void Walk (std::function< void(Handle &)> callback)
 Walk the list of handles. More...
 
void Fork ()
 Reinitialize any kernel state necessary in the child process after a fork(2) system call. More...
 
uv_loop_tGetRaw () const noexcept
 Get the underlying event loop data structure. More...
 
template<typename T = void>
std::shared_ptr< T > GetData () const
 Gets user-defined data. More...
 
void SetData (std::shared_ptr< void > data)
 Sets user-defined data. More...
 
std::thread::id GetThreadId () const
 Get the thread id of the loop thread. More...
 
void ReportError (int err)
 Reports error. More...
 

Static Public Member Functions

static std::shared_ptr< LoopCreate ()
 Create a new event loop. More...
 
static std::shared_ptr< LoopGetDefault ()
 Create the default event loop. More...
 

Public Attributes

sig::Signal< Errorerror
 Error signal.
 

Detailed Description

Event loop.

The event loop is the central part of uv functionality. It takes care of polling for I/O and scheduling signals to be generated based on different sources of events.

The event loop is not moveable, copyable, or directly constructible. Use Create() to create an event loop, or GetDefault() to get the default loop if you know your program will only have a single one.

Member Function Documentation

void wpi::uv::Loop::Close ( )

Release 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.

error() will be emitted in case of errors.

static std::shared_ptr<Loop> wpi::uv::Loop::Create ( )
static

Create a new event loop.

The created loop is not the default event loop.

Returns
The newly created loop. May return nullptr if a failure occurs.
void wpi::uv::Loop::Fork ( )

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.

error() will be emitted in case of errors.

template<typename T = void>
std::shared_ptr<T> wpi::uv::Loop::GetData ( ) const
inline

Gets user-defined data.

Returns
User-defined data if any, nullptr otherwise.
static std::shared_ptr<Loop> wpi::uv::Loop::GetDefault ( )
static

Create the default event loop.

Only use this event loop if a single loop is needed for the entire application.

Returns
The newly created loop. May return nullptr if a failure occurs.
int wpi::uv::Loop::GetDescriptor ( ) const
inlinenoexcept

Get backend file descriptor.

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

Returns
The backend file descriptor.
uv_loop_t* wpi::uv::Loop::GetRaw ( ) const
inlinenoexcept

Get the underlying event loop data structure.

Returns
The underlying event loop data structure.
std::thread::id wpi::uv::Loop::GetThreadId ( ) const
inline

Get the thread id of the loop thread.

If the loop is not currently running, returns default-constructed thread id.

std::pair<bool, Time> wpi::uv::Loop::GetTimeout ( ) const
inlinenoexcept

Get the poll timeout.

Returns
A std::pair composed of a boolean value that is true in case of valid timeout, false otherwise, and the timeout (std::chrono::duration<uint64_t, std::milli>).
bool wpi::uv::Loop::IsAlive ( ) const
inlinenoexcept

Check if there are active resources.

Returns
True if there are active resources in the loop.
Time wpi::uv::Loop::Now ( ) const
inlinenoexcept

Return 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>).
void wpi::uv::Loop::ReportError ( int  err)
inline

Reports error.

Parameters
errError code
bool wpi::uv::Loop::Run ( Mode  mode = kDefault)
inline

Run the event loop.

Available modes are:

  • Loop::kDefault: Run the event loop until there are no active and referenced handles or requests.
  • Loop::kOnce: Run a single event loop iteration. Note that this function blocksif there are no pending callbacks.
  • Loop::kNoWait: Run a single event loop iteration, but don't block if there are no pending callbacks.
Returns
True when done, false in all other cases.
void wpi::uv::Loop::SetData ( std::shared_ptr< void >  data)
inline

Sets user-defined data.

Parameters
dataUser-defined arbitrary data.
void wpi::uv::Loop::Stop ( )
inlinenoexcept

Stop the event loop.

This will cause 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.

void wpi::uv::Loop::UpdateTime ( )
inlinenoexcept

Update 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.

void wpi::uv::Loop::Walk ( std::function< void(Handle &)>  callback)

Walk 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.

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