Imported Upstream version 17.22.1
[platform/upstream/libzypp.git] / zypp / zyppng / base / private / eventdispatcher_glib_p.h
1 #ifndef ZYPP_BASE_EVENTDISPATCHER_GLIB_P_DEFINED
2 #define ZYPP_BASE_EVENTDISPATCHER_GLIB_P_DEFINED
3
4 #include "base_p.h"
5 #include <zypp/zyppng/base/eventdispatcher.h>
6 #include <glib.h>
7 #include <thread>
8 #include <unordered_map>
9 #include <queue>
10
11 namespace zyppng {
12
13 struct GUnixPollFD
14 {
15   GIOCondition reqEvents;
16   int pollfd   = -1;
17   gpointer tag = nullptr;
18 };
19
20 /*!
21  * \internal GSource for all AbstractEventSources
22  * \attention this struct is initialized with malloc, make sure
23  *            to manually call all constructurs and destructors in the
24  *            \a create and \a destruct functions
25  */
26 struct GAbstractEventSource
27 {
28   GSource source;
29   AbstractEventSource *eventSource;
30   EventDispatcherPrivate *_ev;
31   std::vector<GUnixPollFD> pollfds;
32
33   static gboolean prepare(GSource *, gint *timeout);
34   static gboolean check(GSource *source);
35   static gboolean dispatch(GSource *source, GSourceFunc, gpointer);
36
37   static GAbstractEventSource *create (EventDispatcherPrivate *ev);
38   static void destruct ( GAbstractEventSource *src );
39 };
40
41 /*!
42  * \internal GSource for all Timers
43  * \attention this struct is initialized with malloc, make sure
44  *            to manually call all constructurs and destructors in the
45  *            \a create and \a destruct functions
46  */
47 struct GLibTimerSource
48 {
49   GSource source;
50   Timer   *_t = nullptr;
51
52   static gboolean prepare(GSource *src, gint *timeout);
53   static gboolean check(GSource *source);
54   static gboolean dispatch(GSource *source, GSourceFunc, gpointer);
55
56   static GLibTimerSource *create ();
57   static void destruct ( GLibTimerSource *src );
58 };
59
60 class EventDispatcherPrivate : public BasePrivate
61 {
62 public:
63   ZYPP_DECLARE_PUBLIC(EventDispatcher)
64   EventDispatcherPrivate( GMainContext *ctx );
65   virtual ~EventDispatcherPrivate();
66
67   bool runIdleTasks();
68   void enableIdleSource ();
69
70   std::thread::id _myThreadId;
71   GMainLoop *_loop = nullptr;
72   GMainContext *_ctx = nullptr;
73
74   GSource *_idleSource  = nullptr;
75
76   std::vector<GLibTimerSource *> _runningTimers;
77   std::vector<GAbstractEventSource *> _eventSources;
78   std::vector< std::shared_ptr<void> > _unrefLater;
79   std::queue< EventDispatcher::IdleFunction > _idleFuncs;
80 };
81
82 }
83
84
85 #endif