libzypp  17.35.11
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/zyppng/base/Signals>
19 #include <zypp-core/zyppng/base/Base>
20 #include <zypp-core/zyppng/base/AbstractEventSource>
21 #include <memory>
22 #include <functional>
23 
24 namespace zyppng {
25 
26 class SocketNotifier;
27 class Timer;
28 
29 ZYPP_FWD_DECL_TYPE_WITH_REFS( EventDispatcher );
30 ZYPP_FWD_DECL_TYPE_WITH_REFS( UnixSignalSource );
31 class EventDispatcherPrivate;
32 
42 {
44  friend class AbstractEventSource;
45  friend class Timer;
46 
47 public:
48 
49  using Ptr = std::shared_ptr<EventDispatcher>;
50  using WeakPtr = std::shared_ptr<EventDispatcher>;
51  using IdleFunction = std::function<bool ()>;
52 
53 
54  ~EventDispatcher() override;
55 
60  virtual bool run_once();
61 
66  template< typename T = IdleFunction >
67  static void invokeOnIdle ( T &&callback )
68  {
69  auto ev = instance();
70  if ( ev )
71  ev->invokeOnIdleImpl( std::forward<T>(callback) );
72  }
73 
84  template< typename T >
85  static void unrefLater ( T &&ptr ) {
86  auto ev = instance();
87  if ( ev )
88  ev->unrefLaterImpl( std::static_pointer_cast<void>( std::forward<T>(ptr) ) );
89  }
90 
98  void clearUnrefLaterList ();
99 
103  ulong runningTimers() const;
104 
108  static std::shared_ptr<EventDispatcher> instance();
109 
115  static void setThreadDispatcher ( const std::shared_ptr<EventDispatcher> &disp );
116 
121  void *nativeDispatcherHandle () const;
122 
129  static bool waitForFdEvent ( const int fd, int events, int &revents, int &timeout );
130 
131  using WaitPidCallback = std::function<void(int, int)>;
132 
137  void trackChildProcess ( int pid, std::function<void(int, int)> callback );
138 
144  bool untrackChildProcess ( int pid );
145 
150  UnixSignalSourceRef unixSignalSource ();
151 
152 protected:
153 
158  EventDispatcher( void *ctx = nullptr );
159 
163  void unrefLaterImpl ( std::shared_ptr<void> &&ptr );
164 
168  void invokeOnIdleImpl ( IdleFunction &&callback );
169 
176  virtual void updateEventSource ( AbstractEventSource &notifier, int fd, int mode );
177 
184  virtual void removeEventSource ( AbstractEventSource &notifier, int fd = -1 );
185 
189  virtual void registerTimer ( Timer &timer );
190 
194  virtual void removeTimer ( Timer &timer );
195 
196 };
197 
198 }
199 
200 #endif
static void unrefLater(T &&ptr)
std::weak_ptr< Base > WeakPtr
Definition: base.h:66
std::function< bool()> IdleFunction
Definition: Arch.h:363
std::function< void(int, int)> WaitPidCallback
ZYPP_FWD_DECL_TYPE_WITH_REFS(Context)
The Timer class provides repetitive and single-shot timers.
Definition: timer.h:44
#define ZYPP_DECLARE_PRIVATE(Class)
Definition: zyppglobal.h:87
#define LIBZYPP_NG_EXPORT
Definition: zyppglobal.h:8
std::shared_ptr< Base > Ptr
Definition: base.h:65