Sane C++ Libraries: SC::AsyncEventLoop Struct Reference (original) (raw)

Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEventLoop pushes all AsyncRequest derived classes to I/O queues in the OS. More...

#include <[Async.h](Async%5F8h%5Fsource.html)>

Classes
struct InternalDefinition
struct Options
Options given to AsyncEventLoop::create. More...
Public Member Functions
AsyncEventLoop (const AsyncEventLoop &)=delete
AsyncEventLoop (AsyncEventLoop &&)=delete
AsyncEventLoop & operator= (AsyncEventLoop &&)=delete
AsyncEventLoop & operator= (const AsyncEventLoop &)=delete
Result create (Options options=Options())
Creates the event loop kernel object.
Result close ()
Closes the event loop kernel object.
Result start (AsyncRequest &async)
Queues an async request request that has been correctly setup.
void interrupt ()
Interrupts the event loop even if it has active request on it.
bool isInitialized () const
Returns true if create has been already called (successfully)
bool needsThreadPoolForFileOperations () const
Returns true if backend needs a thread pool for non-blocking fs operations (anything but io_uring basically)
Result run ()
Blocks until there are no more active queued requests, dispatching all completions.
Result runOnce ()
Blocks until at least one request proceeds, ensuring forward progress, dispatching all completions.
Result runNoWait ()
Process active requests if any, dispatching their completions, or returns immediately without blocking.
Result submitRequests (AsyncKernelEvents &kernelEvents)
Submits all queued async requests.
Result blockingPoll (AsyncKernelEvents &kernelEvents)
Blocks until at least one event happens, ensuring forward progress, without executing completions.
Result dispatchCompletions (AsyncKernelEvents &kernelEvents)
Invokes completions for the AsyncKernelEvents collected by a call to AsyncEventLoop::blockingPoll.
Result wakeUpFromExternalThread (AsyncLoopWakeUp &wakeUp)
Wake up the event loop from a thread different than the one where run() is called (and potentially blocked).
Result wakeUpFromExternalThread ()
Wake up the event loop from a thread different than the one where run() is called (and potentially blocked)
Result createAsyncTCPSocket (SocketFlags::AddressFamily family, SocketDescriptor &outDescriptor)
Creates an async TCP (IPV4 / IPV6) socket registered with the eventLoop.
Result createAsyncUDPSocket (SocketFlags::AddressFamily family, SocketDescriptor &outDescriptor)
Creates an async UCP (IPV4 / IPV6) socket registered with the eventLoop.
Result associateExternallyCreatedSocket (SocketDescriptor &outDescriptor)
Associates a previously created TCP / UDP socket with the eventLoop.
Result associateExternallyCreatedFileDescriptor (FileDescriptor &outDescriptor)
Associates a previously created File Descriptor with the eventLoop.
void updateTime ()
Updates loop time to "now".
TimeMs getLoopTime () const
Get Loop time (monotonic)
int getNumberOfActiveRequests () const
Obtain the total number of active requests.
int getNumberOfSubmittedRequests () const
Obtain the total number of submitted requests.
AsyncLoopTimeout * findEarliestLoopTimeout () const
Returns the next AsyncLoopTimeout that will be executed (shortest relativeTimeout)
void excludeFromActiveCount (AsyncRequest &async)
Excludes the request from active handles count (to avoid it keeping event loop alive)
void includeInActiveCount (AsyncRequest &async)
Reverses the effect of excludeFromActiveCount for the request.
void enumerateRequests (Function< void(AsyncRequest &)> enumerationCallback)
Enumerates all requests objects associated with this loop.
void setListeners (AsyncEventLoopListeners *listeners)
Sets reference to listeners that will signal different events in loop lifetime.
void clearSequence (AsyncSequence &sequence)
Clears the sequence.
Static Public Member Functions
static Result removeAllAssociationsFor (SocketDescriptor &outDescriptor)
Removes association of a TCP Socket with any event loop.
static Result removeAllAssociationsFor (FileDescriptor &outDescriptor)
Removes association of a File Descriptor with any event loop.
static bool isExcludedFromActiveCount (const AsyncRequest &async)
Checks if excludeFromActiveCount() has been called on the given request.
static bool tryLoadingLiburing ()
Check if liburing is loadable (only on Linux)
Friends
struct AsyncRequest
struct AsyncFileWrite
struct AsyncFileRead
struct AsyncFileSystemOperation
struct AsyncResult

Asynchronous I/O (files, sockets, timers, processes, fs events, threads wake-up) (see Async) AsyncEventLoop pushes all AsyncRequest derived classes to I/O queues in the OS.

See also

AsyncEventLoopMonitor can be used to integrate AsyncEventLoop with a GUI event loop

Basic lifetime for an event loop is:

AsyncEventLoop eventLoop;

SC_TRY(eventLoop.create());

SC_TRY(eventLoop.close());

associateExternallyCreatedFileDescriptor()

Result SC::AsyncEventLoop::associateExternallyCreatedFileDescriptor ( FileDescriptor & outDescriptor )

Associates a previously created File Descriptor with the eventLoop.

associateExternallyCreatedSocket()

Associates a previously created TCP / UDP socket with the eventLoop.

blockingPoll()

Blocks until at least one event happens, ensuring forward progress, without executing completions.

It's one of the three building blocks of AsyncEventLoop::runOnce allowing co-operation of AsyncEventLoop within another event loop (for example a GUI event loop or another IO event loop).

One possible example of such integration with a GUI event loop could:

Waiting on requests blocks the current thread with 0% CPU utilization.

Parameters

kernelEvents Mandatory parameter to store kernel IO events WITHOUT running their completions. In that case user is expected to run completions passing it to AsyncEventLoop::dispatchCompletions.

See also

AsyncEventLoop::submitRequests sends async requests to kernel before calling blockingPoll

AsyncEventLoop::dispatchCompletions invokes callbacks associated with kernel events after blockingPoll

AsyncEventLoop::setListeners sets function called before and after entering kernel poll

clearSequence()

void SC::AsyncEventLoop::clearSequence ( AsyncSequence & sequence )

Clears the sequence.

close()

Result SC::AsyncEventLoop::close ( )

Closes the event loop kernel object.

create()

Creates the event loop kernel object.

createAsyncTCPSocket()

Creates an async TCP (IPV4 / IPV6) socket registered with the eventLoop.

createAsyncUDPSocket()

Creates an async UCP (IPV4 / IPV6) socket registered with the eventLoop.

dispatchCompletions()

Invokes completions for the AsyncKernelEvents collected by a call to AsyncEventLoop::blockingPoll.

This is typically done when user wants to pool for events on a thread (calling AsyncEventLoop::blockingPoll) and dispatch the callbacks on another thread (calling AsyncEventLoop::dispatchCompletions). The typical example would be integrating AsyncEventLoop with a GUI event loop.

See also

AsyncEventLoop::blockingPoll for a description on how to integrate AsyncEventLoop with another event loop

enumerateRequests()

Enumerates all requests objects associated with this loop.

excludeFromActiveCount()

void SC::AsyncEventLoop::excludeFromActiveCount ( AsyncRequest & async )

Excludes the request from active handles count (to avoid it keeping event loop alive)

findEarliestLoopTimeout()

AsyncLoopTimeout * SC::AsyncEventLoop::findEarliestLoopTimeout ( ) const nodiscard

getLoopTime()

TimeMs SC::AsyncEventLoop::getLoopTime ( ) const nodiscard

Get Loop time (monotonic)

getNumberOfActiveRequests()

int SC::AsyncEventLoop::getNumberOfActiveRequests ( ) const nodiscard

Obtain the total number of active requests.

getNumberOfSubmittedRequests()

int SC::AsyncEventLoop::getNumberOfSubmittedRequests ( ) const nodiscard

Obtain the total number of submitted requests.

includeInActiveCount()

void SC::AsyncEventLoop::includeInActiveCount ( AsyncRequest & async )

Reverses the effect of excludeFromActiveCount for the request.

interrupt()

void SC::AsyncEventLoop::interrupt ( )

Interrupts the event loop even if it has active request on it.

isExcludedFromActiveCount()

static bool SC::AsyncEventLoop::isExcludedFromActiveCount ( const AsyncRequest & async) staticnodiscard

Checks if excludeFromActiveCount() has been called on the given request.

isInitialized()

bool SC::AsyncEventLoop::isInitialized ( ) const nodiscard

Returns true if create has been already called (successfully)

needsThreadPoolForFileOperations()

bool SC::AsyncEventLoop::needsThreadPoolForFileOperations ( ) const nodiscard

Returns true if backend needs a thread pool for non-blocking fs operations (anything but io_uring basically)

removeAllAssociationsFor() [1/2]

Removes association of a File Descriptor with any event loop.

removeAllAssociationsFor() [2/2]

Removes association of a TCP Socket with any event loop.

run()

Result SC::AsyncEventLoop::run ( )

Blocks until there are no more active queued requests, dispatching all completions.

It's useful for applications where the eventLoop is the only (or the main) loop. One example could be a console based app doing socket IO or a web server. Waiting on kernel events blocks the current thread with 0% CPU utilization.

See also

AsyncEventLoop::blockingPoll to integrate the loop with a GUI event loop

runNoWait()

Result SC::AsyncEventLoop::runNoWait ( )

Process active requests if any, dispatching their completions, or returns immediately without blocking.

It's useful for game-like applications where the event loop runs every frame and one would like to check and dispatch its I/O callbacks in-between frames. This call allows poll-checking I/O without blocking.

See also

AsyncEventLoop::blockingPoll to integrate the loop with a GUI event loop

runOnce()

Result SC::AsyncEventLoop::runOnce ( )

Blocks until at least one request proceeds, ensuring forward progress, dispatching all completions.

It's useful for application where it's needed to run some idle work after every IO event. Waiting on requests blocks the current thread with 0% CPU utilization.

This function is a shortcut invoking async event loop building blocks:

setListeners()

Sets reference to listeners that will signal different events in loop lifetime.

Note

The structure pointed by this pointer must be valid throughout loop lifetime

start()

Queues an async request request that has been correctly setup.

Note

The request will be validated immediately and activated during next event loop cycle

submitRequests()

tryLoadingLiburing()

static bool SC::AsyncEventLoop::tryLoadingLiburing ( ) staticnodiscard

Check if liburing is loadable (only on Linux)

Returns

true if liburing has been loaded, false otherwise (and on any non-Linux os)

updateTime()

void SC::AsyncEventLoop::updateTime ( )

Updates loop time to "now".

wakeUpFromExternalThread() [1/2]

Result SC::AsyncEventLoop::wakeUpFromExternalThread ( )

Wake up the event loop from a thread different than the one where run() is called (and potentially blocked)

wakeUpFromExternalThread() [2/2]

Wake up the event loop from a thread different than the one where run() is called (and potentially blocked).

The parameter is an AsyncLoopWakeUp that must have been previously started (with AsyncLoopWakeUp::start).


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