Anklang-0.3.0.dev712+gdc4e642f anklang-0.3.0.dev712+gdc4e642f
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | List of all members
Ase::LoopImpl Class Reference

Loop implementation with internal state. More...

Inheritance diagram for Ase::LoopImpl:
[legend]

Classes

struct  MakeSharedAllocator_
 

Public Types

typedef std::vector< LoopSourcePSourceList
 
- Public Types inherited from Ase::Loop
typedef std::function< void(void)> VoidSlot
 
typedef std::function< bool(void)> BoolSlot
 
typedef std::function< void(PollFD &)> VPfdSlot
 
typedef std::function< bool(PollFD &)> BPfdSlot
 
typedef std::function< bool(const LoopState &)> DispatcherSlot
 
typedef std::function< bool(int8)> USignalSlot
 
typedef std::function< void(int, int)> SigchldSlot
 

Public Member Functions

bool finishable_L ()
 
int run () override
 Run the event loop.
 
bool running () override
 Indicates if quit() has been called already.
 
void wakeup () override
 Wake up the event loop.
 
void quit (int quit_code) override
 Stop the event loop.
 
bool finishable () override
 Indicates wether this loop has no primary sources left to process.
 
bool iterate (bool may_block) override
 Iterate the main loop once.
 
void iterate_pending () override
 Iterate pending sources.
 
bool pending () override
 Check if iterate() needs to be called for dispatching.
 
bool set_g_main_context (GlibGMainContext *glib_main_context) override
 Set context to integrate with a GLib GMainContext loop.
 
bool has_quit () override
 Check if quit() has been called.
 
bool iterate_loops_Lm (LoopState &, bool b, bool d)
 
void destroy_loop () override
 Remove all sources from a loop and prevent any further execution.
 
LoopSourcePfind_first_L ()
 
LoopSourcePfind_source_L (LoopID id)
 
bool has_primary_L (void)
 
void remove_source_Lm (LoopSourceP source)
 
void kill_sources_Lm (void)
 
void unpoll_sources_U ()
 
void collect_sources_Lm (LoopState &)
 
bool prepare_sources_Lm (LoopState &, std::vector< PollFD > &)
 
bool check_sources_Lm (LoopState &, const std::vector< PollFD > &)
 
void dispatch_source_Lm (LoopState &)
 
void process_atomic_stacks ()
 
LoopID add_source (LoopSourceP loop_source, LoopPriority priority) override
 Add an event source to the loop.
 
void cancel (LoopID id) override
 Cancel an event source.
 
void cancel (LoopID *idp) override
 Cancel an event source.
 
bool has_primary () override
 Indicates whether loop contains primary sources.
 
LoopID exec_sigchld (int64_t pid, const SigchldSlot &vfunc, LoopPriority priority) override
 Execute a signal callback for prepare, check, dispatch.
 
bool exec_once (uint delay_ms, LoopID *once_id, const VoidSlot &vfunc, LoopPriority priority) override
 Execute a callback once on SIGCHLD for pid.
 
- Public Member Functions inherited from Ase::Loop
LoopID exec_dispatcher (const DispatcherSlot &sl, LoopPriority priority=LoopPriority::NORMAL)
 
LoopID exec_usignal (int8 signum, const USignalSlot &sl, LoopPriority priority=LoopPriority::USIGNAL)
 Execute a single dispatcher callback for prepare, check, dispatch.
 
template<IsLoopCallback Func>
LoopID add (Func &&func, std::chrono::milliseconds interval=std::chrono::milliseconds(0), LoopPriority priority=LoopPriority::NORMAL)
 
template<IsLoopCallback Func>
requires IsAwaitable<std::invoke_result_t<Coroutine>>
LoopID add (Func &&func, LoopPriority priority)
 
template<class Coroutine >
requires IsAwaitable<std::invoke_result_t<Coroutine>>
LoopID add (Coroutine &&coroutine, LoopPriority priority=LoopPriority::NORMAL)
 
template<class BoolVoidPollFunctor >
LoopID exec_io_handler (BoolVoidPollFunctor &&bvf, int fd, const String &mode, LoopPriority priority=LoopPriority::NORMAL)
 Execute a callback after polling for mode on fd, returning true repeats callback.
 
template<typename Result >
std::shared_ptr< Promise< Result > > make_promise (const std::function< void(std::function< void(Result)>)> &executor)
 Create promise and immediately run executor.
 
std::shared_ptr< Promise< void > > make_promise (const std::function< void(std::function< void()>)> &executor)
 Create promise and immediately run executor.
 
std::shared_ptr< Promise< uint64_t > > delay (std::chrono::milliseconds ms)
 Create a promise that resolves after ms milliseconds and returns the elapsed delay.
 
- Public Member Functions inherited from std::enable_shared_from_this< Loop >
enable_shared_from_this (T... args)
 
operator= (T... args)
 
shared_from_this (T... args)
 
weak_from_this (T... args)
 
~enable_shared_from_this (T... args)
 

Static Public Member Functions

template<typename ... Args>
static std::shared_ptr< LoopImplmake_shared (Args &&...args)
 
- Static Public Member Functions inherited from Ase::Loop
static LoopP current ()
 Return the thread-local singleton loop, created on first call.
 

Public Attributes

SourceList sources_
 
std::vector< LoopSourcePpoll_sources_
 
AtomicStack< LoopSourcePpending_add_stack_
 
AtomicStack< LoopID > pending_cancel_stack_
 
std::atomic< int16_tquit_code_
 
uint running_
 
uint rr_index_
 
int16 dispatch_priority_
 
EventFd eventfd_
 
GlibGMainContext * gcontext_
 

Additional Inherited Members

Detailed Description

Loop implementation with internal state.

Definition at line 74 of file loop.cc.

Member Typedef Documentation

◆ SourceList

Definition at line 81 of file loop.cc.

Constructor & Destructor Documentation

◆ LoopImpl()

Ase::LoopImpl::LoopImpl ( )
explicit

Definition at line 331 of file loop.cc.

◆ ~LoopImpl()

Ase::LoopImpl::~LoopImpl ( )
virtual

Definition at line 342 of file loop.cc.

Member Function Documentation

◆ add_source()

LoopID Ase::LoopImpl::add_source ( LoopSourceP  source,
LoopPriority  priority 
)
overridevirtual

Add an event source to the loop.

This method adds a LoopSource to the event loop with a specified priority. The source will be monitored and dispatched according to its implementation and the loop's iteration logic. This method is thread-safe and can be called from any thread. If the loop is currently blocked in a poll() call, it will be woken up to process the new source.

Parameters
sourceThe event source to add.
priorityThe priority at which the source should be dispatched.
Returns
A unique LoopID for the added source, or LoopID::INVALID on failure.

Implements Ase::Loop.

Definition at line 185 of file loop.cc.

References assert_return, uint16_t, and wakeup().

Referenced by exec_sigchld().

◆ cancel() [1/2]

void Ase::LoopImpl::cancel ( LoopID *  idp)
overridevirtual

Cancel an event source.

This method removes a source from the loop using its unique LoopID. If the source is currently being dispatched, it will be removed after the dispatch callback returns. This method is thread-safe.

Parameters
idpPointer to the unique ID of the source to cancel. The ID will be reset to INVALID (0).

Implements Ase::Loop.

Definition at line 237 of file loop.cc.

References cancel().

◆ cancel() [2/2]

void Ase::LoopImpl::cancel ( LoopID  id)
overridevirtual

Cancel an event source.

This method removes a source from the loop using its unique LoopID. If the source is currently being dispatched, it will be removed after the dispatch callback returns. This method is thread-safe.

Parameters
idThe unique ID of the source to cancel.

Implements Ase::Loop.

Definition at line 222 of file loop.cc.

References Ase::AtomicStack< Value, GrowOnlyAllocator >::push(), return_unless, and wakeup().

Referenced by cancel(), and exec_once().

◆ check_sources_Lm()

bool Ase::LoopImpl::check_sources_Lm ( LoopState state,
const std::vector< PollFD > &  pfda 
)

Definition at line 637 of file loop.cc.

◆ collect_sources_Lm()

void Ase::LoopImpl::collect_sources_Lm ( LoopState state)

Definition at line 555 of file loop.cc.

◆ destroy_loop()

void Ase::LoopImpl::destroy_loop ( )
overridevirtual

Remove all sources from a loop and prevent any further execution.

The destroy_loop() method removes all sources from a loop and in case of a sub Loop (see create_sub_loop()) removes it from its associated main loop. Calling destroy_loop() on a main loop also calls destroy_loop() for all its sub loops. Note that LoopImpl objects are artificially kept alive until LoopImpl::destroy_loop() is called, so calling destroy_loop() is mandatory for LoopImpl objects to prevent object leaks.

Implements Ase::Loop.

Definition at line 358 of file loop.cc.

References set_g_main_context().

◆ dispatch_source_Lm()

void Ase::LoopImpl::dispatch_source_Lm ( LoopState state)

Definition at line 671 of file loop.cc.

◆ exec_once()

bool Ase::LoopImpl::exec_once ( uint  delay_ms,
LoopID *  once_id,
const VoidSlot vfunc,
LoopPriority  priority 
)
overridevirtual

Execute a callback once on SIGCHLD for pid.

Execute a callback once, re-schedules the callback if 0 != *once_id.

Implements Ase::Loop.

Definition at line 247 of file loop.cc.

References assert_return, cancel(), std::vector::push_back(), uint16_t, uint64_t, and wakeup().

◆ exec_sigchld()

LoopID Ase::LoopImpl::exec_sigchld ( int64_t  pid,
const SigchldSlot vfunc,
LoopPriority  priority 
)
overridevirtual

Execute a signal callback for prepare, check, dispatch.

Implements Ase::Loop.

Definition at line 280 of file loop.cc.

References add_source().

◆ find_first_L()

LoopSourceP & Ase::LoopImpl::find_first_L ( )

Definition at line 128 of file loop.cc.

◆ find_source_L()

LoopSourceP & Ase::LoopImpl::find_source_L ( LoopID  id)

Definition at line 135 of file loop.cc.

◆ finishable()

bool Ase::LoopImpl::finishable ( )
overridevirtual

Indicates wether this loop has no primary sources left to process.

Implements Ase::Loop.

Definition at line 436 of file loop.cc.

◆ finishable_L()

bool Ase::LoopImpl::finishable_L ( )

Definition at line 429 of file loop.cc.

◆ has_primary()

bool Ase::LoopImpl::has_primary ( )
overridevirtual

Indicates whether loop contains primary sources.

Implements Ase::Loop.

Definition at line 169 of file loop.cc.

◆ has_primary_L()

bool Ase::LoopImpl::has_primary_L ( void  )

Definition at line 145 of file loop.cc.

◆ has_quit()

bool Ase::LoopImpl::has_quit ( )
overridevirtual

Check if quit() has been called.

Implements Ase::Loop.

Definition at line 406 of file loop.cc.

◆ iterate()

bool Ase::LoopImpl::iterate ( bool  may_block)
overridevirtual

Iterate the main loop once.

LoopImpl::iterate() is the heart of the main event loop. For loop iteration, all event sources are polled for incoming events. Then dispatchable sources are picked one per iteration and dispatched in round-robin fashion. If no sources need immediate dispatching and may_block is true, iterate() will wait for events to become available.

Parameters
may_blockIf true, iterate() will wait for events occour.
Returns
Whether more sources need immediate dispatching.

Implements Ase::Loop.

Definition at line 452 of file loop.cc.

◆ iterate_loops_Lm()

bool Ase::LoopImpl::iterate_loops_Lm ( LoopState state,
bool  b,
bool  d 
)

Definition at line 703 of file loop.cc.

◆ iterate_pending()

void Ase::LoopImpl::iterate_pending ( )
overridevirtual

Iterate pending sources.

This method is used to iterate pending sources, when called after quit() it will continue to iterate until all sources are dispatched, unless the loop is quit again.

Implements Ase::Loop.

Definition at line 469 of file loop.cc.

References ISLIKELY, and uint16_t.

◆ kill_sources_Lm()

void Ase::LoopImpl::kill_sources_Lm ( void  )

Definition at line 286 of file loop.cc.

◆ make_shared()

template<typename ... Args>
static std::shared_ptr< LoopImpl > Ase::LoopImpl::make_shared ( Args &&...  args)
static

Definition at line 80 of file loop.cc.

◆ pending()

bool Ase::LoopImpl::pending ( )
overridevirtual

Check if iterate() needs to be called for dispatching.

Implements Ase::Loop.

Definition at line 486 of file loop.cc.

◆ prepare_sources_Lm()

bool Ase::LoopImpl::prepare_sources_Lm ( LoopState state,
std::vector< PollFD > &  pfda 
)

Definition at line 600 of file loop.cc.

◆ process_atomic_stacks()

void Ase::LoopImpl::process_atomic_stacks ( )

Definition at line 154 of file loop.cc.

◆ quit()

void Ase::LoopImpl::quit ( int  quit_code)
overridevirtual

Stop the event loop.

This method signals the event loop to stop processing events and exit its run() method. The provided quit_code will be the return value of Loop::run(). This method is safe to call from any thread.

Parameters
quit_codeThe exit code for the loop to return.

Implements Ase::Loop.

Definition at line 420 of file loop.cc.

References wakeup().

◆ remove_source_Lm()

void Ase::LoopImpl::remove_source_Lm ( LoopSourceP  source)

Definition at line 201 of file loop.cc.

◆ run()

int Ase::LoopImpl::run ( )
overridevirtual

Run the event loop.

This method starts the event loop and continues to process events until quit() is called. It returns the exit code provided to quit().

Returns
The exit code passed to quit().

Implements Ase::Loop.

Definition at line 385 of file loop.cc.

References ISLIKELY.

◆ running()

bool Ase::LoopImpl::running ( )
overridevirtual

Indicates if quit() has been called already.

Implements Ase::Loop.

Definition at line 400 of file loop.cc.

◆ set_g_main_context()

bool Ase::LoopImpl::set_g_main_context ( GlibGMainContext *  glib_main_context)
overridevirtual

Set context to integrate with a GLib GMainContext loop.

Implements Ase::Loop.

Definition at line 497 of file loop.cc.

Referenced by destroy_loop().

◆ unpoll_sources_U()

void Ase::LoopImpl::unpoll_sources_U ( )

Definition at line 548 of file loop.cc.

◆ wakeup()

void Ase::LoopImpl::wakeup ( )
overridevirtual

Wake up the event loop.

This method wakes up the event loop if it is currently blocked waiting for events. It is safe to call from any thread.

Implements Ase::Loop.

Definition at line 372 of file loop.cc.

References Ase::EventFd::opened(), and Ase::EventFd::wakeup().

Referenced by add_source(), cancel(), exec_once(), and quit().

Member Data Documentation

◆ dispatch_priority_

int16 Ase::LoopImpl::dispatch_priority_

Definition at line 89 of file loop.cc.

◆ eventfd_

EventFd Ase::LoopImpl::eventfd_

Definition at line 90 of file loop.cc.

◆ gcontext_

GlibGMainContext* Ase::LoopImpl::gcontext_

Definition at line 91 of file loop.cc.

◆ pending_add_stack_

AtomicStack<LoopSourceP> Ase::LoopImpl::pending_add_stack_

Definition at line 84 of file loop.cc.

◆ pending_cancel_stack_

AtomicStack<LoopID> Ase::LoopImpl::pending_cancel_stack_

Definition at line 85 of file loop.cc.

◆ poll_sources_

std::vector<LoopSourceP> Ase::LoopImpl::poll_sources_

Definition at line 83 of file loop.cc.

◆ quit_code_

std::atomic<int16_t> Ase::LoopImpl::quit_code_

Definition at line 86 of file loop.cc.

◆ rr_index_

uint Ase::LoopImpl::rr_index_

Definition at line 88 of file loop.cc.

◆ running_

uint Ase::LoopImpl::running_

Definition at line 87 of file loop.cc.

◆ sources_

SourceList Ase::LoopImpl::sources_

Definition at line 82 of file loop.cc.


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