WPILibC++  2020.3.2-60-g3011ebe
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_t * GetRaw () 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

◆ Close()

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.

◆ Create()

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

Create a new event loop.

The created loop is not the default event loop.

The newly created loop. May return nullptr if a failure occurs.

◆ Fork()

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.

◆ GetData()

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

Gets user-defined data.

User-defined data if any, nullptr otherwise.

◆ GetDefault()

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

Create the default event loop.

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

The newly created loop. May return nullptr if a failure occurs.

◆ GetDescriptor()

int wpi::uv::Loop::GetDescriptor ( ) const

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.

The backend file descriptor.

◆ GetRaw()

uv_loop_t* wpi::uv::Loop::GetRaw ( ) const

Get the underlying event loop data structure.

The underlying event loop data structure.

◆ GetThreadId()

std::thread::id wpi::uv::Loop::GetThreadId ( ) const

Get the thread id of the loop thread.

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

◆ GetTimeout()

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

Get the poll timeout.

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

◆ IsAlive()

bool wpi::uv::Loop::IsAlive ( ) const

Check if there are active resources.

True if there are active resources in the loop.

◆ Now()

Time wpi::uv::Loop::Now ( ) const

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.

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

◆ ReportError()

void wpi::uv::Loop::ReportError ( int  err)

Reports error.

errError code

◆ Run()

bool wpi::uv::Loop::Run ( Mode  mode = kDefault)

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.
True when done, false in all other cases.

◆ SetData()

void wpi::uv::Loop::SetData ( std::shared_ptr< void >  data)

Sets user-defined data.

dataUser-defined arbitrary data.

◆ Stop()

void wpi::uv::Loop::Stop ( )

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.

◆ UpdateTime()

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

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.

◆ Walk()

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.

callbackA function to be invoked once for each active handle.

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