libzypp 17.38.7
eventdispatcher.h
Go to the documentation of this file.
1/*---------------------------------------------------------------------\
2| ____ _ __ __ ___ |
3| |__ / \ / / . \ . \ |
4| / / \ V /| _/ _/ |
5| / /__ | | | | | | |
6| /_____||_| |_| |_| |
7| |
8\----------------------------------------------------------------------/
9*
10* This file contains private API, this might break at any time between releases.
11* You have been warned!
12*
13*/
14#ifndef ZYPP_BASE_EVENTDISPATCHER_DEFINED
15#define ZYPP_BASE_EVENTDISPATCHER_DEFINED
16
18#include <zypp-core/ng/base/Signals>
19#include <zypp-core/ng/base/Base>
20#include <zypp-core/ng/base/AbstractEventSource>
21#include <memory>
22#include <functional>
23
24typedef struct _GMainContext GMainContext;
25
26namespace zyppng {
27
28class SocketNotifier;
29class Timer;
30ZYPP_FWD_DECL_TYPE_WITH_REFS( EventDispatcher );
33
42class EventDispatcher : public Base
43{
45 friend class AbstractEventSource;
46 friend class Timer;
47
48public:
49
50 using Ptr = EventDispatcherRef;
51 using WeakPtr = EventDispatcherWeakRef;
52 using IdleFunction = std::function<bool ()>;
53 using TimeoutFunction = std::function<bool ()>;
54
55
56 ~EventDispatcher() override;
57
62 virtual bool run_once();
63
72 {
73 auto ev = instance();
74 if ( ev )
75 ev->invokeOnIdleImpl( std::move(callback) );
76 }
77
82 template< typename T = TimeoutFunction >
83 static void invokeAfter ( T &&callback, uint32_t timeout )
84 {
85 auto ev = instance();
86 if ( ev )
87 ev->invokeAfterImpl( std::forward<T>(callback), timeout );
88 }
89
90
101 template< typename T >
102 static void unrefLater ( T &&ptr ) {
103 auto ev = instance();
104 if ( ev )
105 ev->unrefLaterImpl( std::static_pointer_cast<void>( std::forward<T>(ptr) ) );
106 }
107
115 void clearUnrefLaterList ();
116
120 ulong runningTimers() const;
121
125 static std::shared_ptr<EventDispatcher> instance();
126
132 static void setThreadDispatcher ( const std::shared_ptr<EventDispatcher> &disp );
133
138 void *nativeDispatcherHandle () const;
139
146 static bool waitForFdEvent ( const int fd, int events, int &revents, int &timeout );
147
148 using WaitPidCallback = std::function<void(int, int)>;
149
154 void trackChildProcess ( int pid, std::function<void(int, int)> callback );
155
161 bool untrackChildProcess ( int pid );
162
163
168 UnixSignalSourceRef unixSignalSource ();
169
171
172protected:
173
178 EventDispatcher( void *ctx = nullptr );
179
183 void unrefLaterImpl ( std::shared_ptr<void> &&ptr );
184
189
190
194 void invokeAfterImpl (TimeoutFunction &&callback, uint32_t timeout );
195
202 virtual void updateEventSource ( AbstractEventSource &notifier, int fd, int mode );
203
210 virtual void removeEventSource ( AbstractEventSource &notifier, int fd = -1 );
211
215 virtual void registerTimer ( Timer &timer );
216
220 virtual void removeTimer ( Timer &timer );
221
222};
223
224}
225
226#endif
friend class AbstractEventSource
static void unrefLater(T &&ptr)
static void invokeAfter(T &&callback, uint32_t timeout)
Convenience function to schedule a callback to be called later.
UnixSignalSourceRef unixSignalSource()
EventDispatcherWeakRef WeakPtr
virtual void registerTimer(Timer &timer)
std::function< void(int, int)> WaitPidCallback
void unrefLaterImpl(std::shared_ptr< void > &&ptr)
std::function< bool()> TimeoutFunction
void * nativeDispatcherHandle() const
Returns the native dispatcher handle if the used implementation supports it.
std::function< bool()> IdleFunction
EventDispatcher(void *ctx=nullptr)
static bool waitForFdEvent(const int fd, int events, int &revents, int &timeout)
virtual void removeTimer(Timer &timer)
void trackChildProcess(int pid, std::function< void(int, int)> callback)
virtual void updateEventSource(AbstractEventSource &notifier, int fd, int mode)
virtual void removeEventSource(AbstractEventSource &notifier, int fd=-1)
static void invokeOnIdle(IdleFunction &&callback)
Convenience function to schedule a callback to be called later.
static void setThreadDispatcher(const std::shared_ptr< EventDispatcher > &disp)
static std::shared_ptr< EventDispatcher > instance()
void invokeOnIdleImpl(IdleFunction &&callback)
void invokeAfterImpl(TimeoutFunction &&callback, uint32_t timeout)
EventDispatcherRef Ptr
struct _GMainContext GMainContext
Definition ansi.h:855
Callbacks light.
Definition Callback.h:146
#define ZYPP_DECLARE_PRIVATE(Class)
Definition zyppglobal.h:80
#define ZYPP_FWD_DECL_TYPE_WITH_REFS(T)
Definition zyppglobal.h:119