libzypp  17.22.0
zyppng::EventDispatcher Class Reference

#include <eventdispatcher.h>

Inheritance diagram for zyppng::EventDispatcher:

Public Types

using Ptr = std::shared_ptr< EventDispatcher >
 
using WeakPtr = std::shared_ptr< EventDispatcher >
 
using IdleFunction = std::function< bool()>
 
- Public Types inherited from zyppng::Base
using Ptr = std::shared_ptr< Base >
 
using WeakPtr = std::weak_ptr< Base >
 

Public Member Functions

virtual ~EventDispatcher ()
 
virtual bool run_once ()
 
virtual void run ()
 
virtual void quit ()
 
ulong runningTimers () const
 
- Public Member Functions inherited from zyppng::Base
 Base ()
 
virtual ~Base ()
 
WeakPtr parent () const
 
void addChild (Base::Ptr child)
 
void removeChild (Ptr child)
 
const std::unordered_set< Ptr > & children () const
 
template<typename T >
std::vector< std::weak_ptr< T > > findChildren () const
 
template<typename T >
std::shared_ptr< T > shared_this () const
 
template<typename T >
std::shared_ptr< T > shared_this ()
 
template<typename T >
std::weak_ptr< T > weak_this () const
 
template<typename T >
std::weak_ptr< T > weak_this ()
 

Static Public Member Functions

static std::shared_ptr< EventDispatchercreateMain ()
 
static std::shared_ptr< EventDispatchercreateForThread ()
 
template<typename T = IdleFunction>
static void invokeOnIdle (T &&callback)
 Convenience function to schedule a callback to be called later. More...
 
template<typename T >
static void unrefLater (T &&ptr)
 
static std::shared_ptr< EventDispatcherinstance ()
 

Protected Member Functions

 EventDispatcher (void *ctx=nullptr)
 
void unrefLaterImpl (std::shared_ptr< void > &&ptr)
 
void invokeOnIdleImpl (IdleFunction &&callback)
 
virtual void updateEventSource (AbstractEventSource *notifier, int fd, int mode)
 
virtual void removeEventSource (AbstractEventSource *notifier, int fd=-1)
 
virtual void registerTimer (Timer *timer)
 
virtual void removeTimer (Timer *timer)
 
- Protected Member Functions inherited from zyppng::Base
 Base (BasePrivate &dd)
 

Friends

class AbstractEventSource
 
class Timer
 

Additional Inherited Members

- Protected Attributes inherited from zyppng::Base
std::unique_ptr< BasePrivated_ptr
 

Detailed Description

The EventDispatcher class implements the libzypp event loop.

A event loop is used to execute multiple tasks concurrently. This is not implemented using threads but usually by either using Timers to poll a ressource or by reacting on events from a file descriptor or socket. In a application like zypper where we heavily make use of I/O heavy tasks like downloading packages, rebuilding the repo metadata or generating a checksum over a file the application needs to wait more for those tasks to finish than actually doing anything. By using a event loop we can start one of those tasks and let the OS handle the execution and subscribe to certain events that can happen, utilizing more of the CPU compared to starting all the tasks serially.

Libzypp is using a thread local eventloop, means each thread needs to start its own loop. Only special case is when we need to work together with a already exisiting event loop, for example in a Qt application. The default implementation however uses the glib eventloop, just like Qt and GTK, so integrating libzypp here is just a matter of passing the default main context to the constructor of EventDispatcher.

Definition at line 49 of file eventdispatcher.h.

Member Typedef Documentation

◆ Ptr

Definition at line 57 of file eventdispatcher.h.

◆ WeakPtr

Definition at line 58 of file eventdispatcher.h.

◆ IdleFunction

using zyppng::EventDispatcher::IdleFunction = std::function<bool ()>

Definition at line 59 of file eventdispatcher.h.

Constructor & Destructor Documentation

◆ ~EventDispatcher()

zyppng::EventDispatcher::~EventDispatcher ( )
virtual

Definition at line 316 of file eventdispatcher_glib.cc.

◆ EventDispatcher()

zyppng::EventDispatcher::EventDispatcher ( void *  ctx = nullptr)
protected

Create a new instance of the EventDispatcher, if ctx is given it is used as the new context for the eventloop

Definition at line 300 of file eventdispatcher_glib.cc.

Member Function Documentation

◆ createMain()

std::shared_ptr< EventDispatcher > zyppng::EventDispatcher::createMain ( )
static

Creates a new EventDispatcher, use this function to create a Dispatcher running on the default thread

Note
the glib implementation will use the g_default_context(), this means it will attach to any running main loop

Definition at line 306 of file eventdispatcher_glib.cc.

◆ createForThread()

std::shared_ptr< EventDispatcher > zyppng::EventDispatcher::createForThread ( )
static

Creates a new EventDispatcher, use this function to create a Dispatcher running on a threads aside the main thread

Note
the glib implementation will use the g_main_context_get_thread_default(), this means it will attach to any loop that was set as the default for the current thread, if there is no default context a new one will be created

Definition at line 311 of file eventdispatcher_glib.cc.

◆ run_once()

bool zyppng::EventDispatcher::run_once ( )
virtual

Enters the eventloop once and dequeues all pending events, once the event queue is empty the function returns

Definition at line 438 of file eventdispatcher_glib.cc.

◆ run()

void zyppng::EventDispatcher::run ( )
virtual

Start dispatching events, this function will block until

See also
quit was called for the EventDispatcher instance

Definition at line 443 of file eventdispatcher_glib.cc.

◆ quit()

void zyppng::EventDispatcher::quit ( )
virtual

Stop dispatching events and return from the main loop.

Definition at line 448 of file eventdispatcher_glib.cc.

◆ invokeOnIdle()

template<typename T = IdleFunction>
static void zyppng::EventDispatcher::invokeOnIdle ( T &&  callback)
inlinestatic

Convenience function to schedule a callback to be called later.

Parameters
callbacka std::function that is called after all other events have been processed

Definition at line 103 of file eventdispatcher.h.

◆ unrefLater()

template<typename T >
static void zyppng::EventDispatcher::unrefLater ( T &&  ptr)
inlinestatic

Schedules a

See also
std::shared_ptr to be unreferenced in the next idle phase of the event loop.

In some cases it might be required to delay the cleanup of a ressource until the current event loop iteration was finished, in case there are more pending events for the ressource.

Definition at line 118 of file eventdispatcher.h.

◆ runningTimers()

ulong zyppng::EventDispatcher::runningTimers ( ) const

Returns the number of the currently active timers

Definition at line 467 of file eventdispatcher_glib.cc.

◆ instance()

std::shared_ptr< EventDispatcher > zyppng::EventDispatcher::instance ( )
static

Returns the EventDispatcher instance for the current thread.

Definition at line 472 of file eventdispatcher_glib.cc.

◆ unrefLaterImpl()

void zyppng::EventDispatcher::unrefLaterImpl ( std::shared_ptr< void > &&  ptr)
protected
See also
unrefLater

Definition at line 460 of file eventdispatcher_glib.cc.

◆ invokeOnIdleImpl()

void zyppng::EventDispatcher::invokeOnIdleImpl ( EventDispatcher::IdleFunction &&  callback)
protected
See also
invokeOnIdle

Definition at line 453 of file eventdispatcher_glib.cc.

◆ updateEventSource()

void zyppng::EventDispatcher::updateEventSource ( AbstractEventSource notifier,
int  fd,
int  mode 
)
protectedvirtual

Updates or registeres a event source in the event loop

Parameters
notifierThe event source implementation that should receive the event notification
fdThe file descriptor that is added to the internal watchlist
modeThe watch mode for the given file desriptor
See also
zyppng::AbstractEventSource::EventTypes

Definition at line 321 of file eventdispatcher_glib.cc.

◆ removeEventSource()

void zyppng::EventDispatcher::removeEventSource ( AbstractEventSource notifier,
int  fd = -1 
)
protectedvirtual

Removes a file descriptor from the internal watchlist, if fd is set to -1 all file descriptors associated with the

See also
AbstractEventSource are removed
Parameters
notifierThe
See also
AbstractEventSource parent of the file descriptor
Parameters
fdThe file descriptor to be removed, set to -1 to remove all descriptors for a
See also
AbstractEventSource

Definition at line 371 of file eventdispatcher_glib.cc.

◆ registerTimer()

void zyppng::EventDispatcher::registerTimer ( Timer timer)
protectedvirtual

Adds a new Timer instance to the internal timer list

Definition at line 408 of file eventdispatcher_glib.cc.

◆ removeTimer()

void zyppng::EventDispatcher::removeTimer ( Timer timer)
protectedvirtual

Removes a timer from the internal timer list, once a Timer is removed it does not fire anymore

Definition at line 424 of file eventdispatcher_glib.cc.

Friends And Related Function Documentation

◆ AbstractEventSource

friend class AbstractEventSource
friend

Definition at line 52 of file eventdispatcher.h.

◆ Timer

friend class Timer
friend

Definition at line 53 of file eventdispatcher.h.


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