improve locking of addTimeOffset a bit
[vuplus_dvbapp] / lib / base / ebase.h
1 #ifndef __ebase_h
2 #define __ebase_h
3
4 #include <vector>
5 #include <map>
6 #include <sys/poll.h>
7 #include <sys/time.h>
8 #include <asm/types.h>
9 #include <time.h>
10
11 #include <lib/base/eptrlist.h>
12 #include <lib/python/connections.h>
13 #include <libsig_comp.h>
14
15 class eApplication;
16
17 extern eApplication* eApp;
18
19         /* TODO: remove these inlines. */
20 static inline bool operator<( const timeval &t1, const timeval &t2 )
21 {
22         return t1.tv_sec < t2.tv_sec || (t1.tv_sec == t2.tv_sec && t1.tv_usec < t2.tv_usec);
23 }
24
25 static inline timeval &operator+=( timeval &t1, const timeval &t2 )
26 {
27         t1.tv_sec += t2.tv_sec;
28         if ( (t1.tv_usec += t2.tv_usec) >= 1000000 )
29         {
30                 t1.tv_sec++;
31                 t1.tv_usec -= 1000000;
32         }
33         return t1;
34 }
35
36 static inline timeval operator+( const timeval &t1, const timeval &t2 )
37 {
38         timeval tmp;
39         tmp.tv_sec = t1.tv_sec + t2.tv_sec;
40         if ( (tmp.tv_usec = t1.tv_usec + t2.tv_usec) >= 1000000 )
41         {
42                 tmp.tv_sec++;
43                 tmp.tv_usec -= 1000000;
44         }
45         return tmp;
46 }
47
48 static inline timeval operator-( const timeval &t1, const timeval &t2 )
49 {
50         timeval tmp;
51         tmp.tv_sec = t1.tv_sec - t2.tv_sec;
52         if ( (tmp.tv_usec = t1.tv_usec - t2.tv_usec) < 0 )
53         {
54                 tmp.tv_sec--;
55                 tmp.tv_usec += 1000000;
56         }
57         return tmp;
58 }
59
60 static inline timeval operator-=( timeval &t1, const timeval &t2 )
61 {
62         t1.tv_sec -= t2.tv_sec;
63         if ( (t1.tv_usec -= t2.tv_usec) < 0 )
64         {
65                 t1.tv_sec--;
66                 t1.tv_usec += 1000000;
67         }
68         return t1;
69 }
70
71 static inline timeval &operator+=( timeval &t1, const long msek )
72 {
73         t1.tv_sec += msek / 1000;
74         if ( (t1.tv_usec += (msek % 1000) * 1000) >= 1000000 )
75         {
76                 t1.tv_sec++;
77                 t1.tv_usec -= 1000000;
78         }
79         return t1;
80 }
81
82 static inline timeval operator+( const timeval &t1, const long msek )
83 {
84         timeval tmp;
85         tmp.tv_sec = t1.tv_sec + msek / 1000;
86         if ( (tmp.tv_usec = t1.tv_usec + (msek % 1000) * 1000) >= 1000000 )
87         {
88                 tmp.tv_sec++;
89                 tmp.tv_usec -= 1000000;
90         }
91         return tmp;
92 }
93
94 static inline timeval operator-( const timeval &t1, const long msek )
95 {
96         timeval tmp;
97         tmp.tv_sec = t1.tv_sec - msek / 1000;
98         if ( (tmp.tv_usec = t1.tv_usec - (msek % 1000)*1000) < 0 )
99         {
100                 tmp.tv_sec--;
101                 tmp.tv_usec += 1000000;
102         }
103         return tmp;
104 }
105
106 static inline timeval operator-=( timeval &t1, const long msek )
107 {
108         t1.tv_sec -= msek / 1000;
109         if ( (t1.tv_usec -= (msek % 1000) * 1000) < 0 )
110         {
111                 t1.tv_sec--;
112                 t1.tv_usec += 1000000;
113         }
114         return t1;
115 }
116
117 static inline int timeval_to_usec(const timeval &t1)
118 {
119         return t1.tv_sec*1000000 + t1.tv_usec;
120 }
121
122 class eMainloop;
123
124                                         // die beiden signalquellen: SocketNotifier...
125
126 /**
127  * \brief Gives a callback when data on a file descriptor is ready.
128  *
129  * This class emits the signal \c eSocketNotifier::activate whenever the
130  * event specified by \c req is available.
131  */
132 class eSocketNotifier
133 {
134 public:
135         enum { Read=POLLIN, Write=POLLOUT, Priority=POLLPRI, Error=POLLERR, Hungup=POLLHUP };
136 private:
137         eMainloop &context;
138         int fd;
139         int state;
140         int requested;          // requested events (POLLIN, ...)
141 public:
142         /**
143          * \brief Constructs a eSocketNotifier.
144          * \param context The thread where to bind the socketnotifier to. The signal is emitted from that thread.
145          * \param fd The filedescriptor to monitor. Can be a device or a socket.
146          * \param req The events to watch to, normally either \c Read or \c Write. You can specify any events that \c poll supports.
147          * \param startnow Specifies if the socketnotifier should start immediately.
148          */
149         eSocketNotifier(eMainloop *context, int fd, int req, bool startnow=true);
150         ~eSocketNotifier();
151
152         PSignal1<void, int> activated;
153         void activate(int what) { /*emit*/ activated(what); }
154
155         void start();
156         void stop();
157         bool isRunning() { return state; }
158
159         int getFD() { return fd; }
160         int getRequested() { return requested; }
161         void setRequested(int req) { requested=req; }
162 };
163
164 class eTimer;
165
166                         // werden in einer mainloop verarbeitet
167 class eMainloop
168 {
169         friend class eTimer;
170         friend class eSocketNotifier;
171         std::map<int, eSocketNotifier*> notifiers;
172         ePtrList<eTimer> m_timer_list;
173         bool app_exit_loop;
174         bool app_quit_now;
175         int loop_level;
176         void processOneEvent();
177         int retval;
178         pthread_mutex_t recalcLock;
179 public:
180         static void addTimeOffset(int offset);
181         void addSocketNotifier(eSocketNotifier *sn);
182         void removeSocketNotifier(eSocketNotifier *sn);
183         void addTimer(eTimer* e);
184         void removeTimer(eTimer* e);
185
186         static ePtrList<eMainloop> existing_loops;
187         eMainloop()
188                 :app_quit_now(0),loop_level(0),retval(0)
189         {
190                 existing_loops.push_back(this);
191                 pthread_mutex_init(&recalcLock, 0);
192         }
193         ~eMainloop()
194         {
195                 existing_loops.remove(this);
196                 pthread_mutex_destroy(&recalcLock);
197         }
198         int looplevel() { return loop_level; }
199
200                 /* OBSOLETE. DONT USE. */
201         int exec();  // recursive enter the loop
202         void quit(int ret=0); // leave all pending loops (recursive leave())
203         void enter_loop();
204         void exit_loop();
205 };
206
207 /**
208  * \brief The application class.
209  *
210  * An application provides a mainloop, and runs in the primary thread.
211  * You can have other threads, too, but this is the primary one.
212  */
213 class eApplication: public eMainloop
214 {
215 public:
216         eApplication()
217         {
218                 if (!eApp)
219                         eApp = this;
220         }
221         ~eApplication()
222         {
223                 eApp = 0;
224         }
225 };
226
227                                 // ... und Timer
228 /**
229  * \brief Gives a callback after a specified timeout.
230  *
231  * This class emits the signal \c eTimer::timeout after the specified timeout.
232  */
233 class eTimer
234 {
235         friend class eMainloop;
236         eMainloop &context;
237         timeval nextActivation;
238         long interval;
239         bool bSingleShot;
240         bool bActive;
241 public:
242         /**
243          * \brief Constructs a timer.
244          *
245          * The timer is not yet active, it has to be started with \c start.
246          * \param context The thread from which the signal should be emitted.
247          */
248         eTimer(eMainloop *context=eApp): context(*context), bActive(false) { }
249         ~eTimer() { if (bActive) stop(); }
250
251         PSignal0<void> timeout;
252         void activate();
253
254         bool isActive() { return bActive; }
255         timeval &getNextActivation() { return nextActivation; }
256
257         void start(long msec, bool b=false);
258         void stop();
259         void changeInterval(long msek);
260         bool operator<(const eTimer& t) const { return nextActivation < t.nextActivation; }
261         void startLongTimer( int seconds );
262         void addTimeOffset(int);
263 };
264 #endif