1 #include <lib/base/ebase.h>
7 #include <lib/base/eerror.h>
8 #include <lib/base/elock.h>
10 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
16 eSocketNotifier::~eSocketNotifier()
21 void eSocketNotifier::start()
26 context.addSocketNotifier(this);
30 void eSocketNotifier::stop()
33 context.removeSocketNotifier(this);
39 void eTimer::start(long msek, bool singleShot)
45 bSingleShot = singleShot;
47 gettimeofday(&nextActivation, 0);
48 nextActivation += (msek<0 ? 0 : msek);
49 // eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, msek);
50 // eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
51 context.addTimer(this);
54 void eTimer::startLongTimer( int seconds )
59 bActive = bSingleShot = true;
61 gettimeofday(&nextActivation, 0);
62 // eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_usec, seconds);
64 nextActivation.tv_sec += seconds;
65 // eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
66 context.addTimer(this);
74 context.removeTimer(this);
78 void eTimer::changeInterval(long msek)
80 if (bActive) // Timer is running?
82 context.removeTimer(this); // then stop
83 nextActivation -= interval; // sub old interval
86 bActive=true; // then activate Timer
88 interval = msek; // set new Interval
89 nextActivation += interval; // calc nextActivation
91 context.addTimer(this); // add Timer to context TimerList
94 void eTimer::activate() // Internal Funktion... called from eApplication
96 context.removeTimer(this);
100 nextActivation += interval;
101 context.addTimer(this);
109 void eTimer::addTimeOffset( int offset )
111 nextActivation.tv_sec += offset;
115 ePtrList<eMainloop> eMainloop::existing_loops;
117 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
119 notifiers.insert(std::pair<int,eSocketNotifier*> (sn->getFD(), sn));
122 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
124 notifiers.erase(sn->getFD());
127 void eMainloop::processOneEvent()
130 - we should use epoll(4)
131 - timer are checked twice. there was a strong reason for it, but i can't remember. (FIXME)
132 - for each time, we gettimeofday() and check wether the timer should fire.
133 we should do this all better - we know how long the poll last, so we know which
134 timers should fire. Problem is that a timer handler could have required so
135 much time that another timer fired.
137 A probably structure could look
141 time = gettimeofday()
142 timeout = calculate_pending_timers(time);
144 doPoll(timeout or infinite);
146 if (poll_had_results)
147 handle_poll_handler();
149 fire_timers(time + timeout)
152 the gettimeofday() call is required because fire_timers could last more
155 when poll did no timeout, we don't handle timers, as this will be done
156 in the next iteration (without adding overhead - we had to get the new
160 /* get current time */
162 gettimeofday(&now, 0);
163 m_now_is_invalid = 0;
165 int poll_timeout = -1; /* infinite in case of empty timer list */
169 singleLock s(recalcLock);
170 poll_timeout = timeval_to_usec(m_timer_list.begin()->getNextActivation() - now);
171 /* if current timer already passed, don't delay infinite. */
172 if (poll_timeout < 0)
175 /* convert us to ms */
176 poll_timeout /= 1000;
183 // build the poll aray
184 int fdcount = notifiers.size();
185 pollfd* pfd = new pollfd[fdcount]; // make new pollfd array
187 std::map<int,eSocketNotifier*>::iterator it(notifiers.begin());
188 for (int i=0; i < fdcount; i++, it++)
190 pfd[i].fd = it->first;
191 pfd[i].events = it->second->getRequested();
194 ret = poll(pfd, fdcount, poll_timeout);
196 /* ret > 0 means that there are some active poll entries. */
199 for (int i=0; i < fdcount ; i++)
201 if (notifiers.find(pfd[i].fd) == notifiers.end())
204 int req = notifiers[pfd[i].fd]->getRequested();
206 if (pfd[i].revents & req)
208 notifiers[pfd[i].fd]->activate(pfd[i].revents);
212 } else if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
213 eFatal("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d) -> FIX YOUR CODE", pfd[i].fd,pfd[i].revents);
217 /* when we got a signal, we get EINTR. we do not care,
218 because we check current time in timers anyway. */
220 eDebug("poll made error (%m)");
227 /* when we not processed anything, check timers. */
230 /* we know that this time has passed. */
233 singleLock s(recalcLock);
235 /* this will never change while we have the recalcLock */
236 /* we can savely return here, the timer will be re-checked soon. */
237 if (m_now_is_invalid)
240 /* process all timers which are ready. first remove them out of the list. */
241 while ((!m_timer_list.empty()) && (m_timer_list.begin()->getNextActivation() <= now))
242 m_timer_list.begin()->activate();
246 void eMainloop::addTimer(eTimer* e)
248 m_timer_list.insert_in_order(e);
251 void eMainloop::removeTimer(eTimer* e)
253 m_timer_list.remove(e);
256 int eMainloop::exec()
260 app_quit_now = false;
261 app_exit_loop = false;
267 void eMainloop::enter_loop()
270 // Status der vorhandenen Loop merken
271 bool old_exit_loop = app_exit_loop;
273 app_exit_loop = false;
275 while (!app_exit_loop && !app_quit_now)
278 // wiederherstellen der vorherigen app_exit_loop
279 app_exit_loop = old_exit_loop;
285 // do something here on exit the last loop
289 void eMainloop::exit_loop() // call this to leave the current loop
291 app_exit_loop = true;
294 void eMainloop::quit( int ret ) // call this to leave all loops
300 void eMainloop::addTimeOffset(int offset)
302 for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
303 ;it != eMainloop::existing_loops.end(); ++it)
305 singleLock s(it->recalcLock);
306 it->m_now_is_invalid = 1;
307 for (ePtrList<eTimer>::iterator tit = it->m_timer_list.begin(); tit != it->m_timer_list.end(); ++tit )
308 tit->addTimeOffset(offset);
312 eApplication* eApp = 0;