i think it is not fatal when a socketnotifier doesnt handle all POLL signals (like...
[enigma2.git] / lib / base / ebase.cpp
index 19444d20d46e75440023daac6e8c0f75d523f994..04b15ee8b5c22ddca65fbc2cb45a1d1c258a78a5 100644 (file)
@@ -45,9 +45,8 @@ void eTimer::start(long msek, bool singleShot)
        bSingleShot = singleShot;
        interval = msek;
        gettimeofday(&nextActivation, 0);
-       nextActivation.tv_sec -= context.getTimerOffset();
-       nextActivation += (msek<0 ? 0 : msek);
 //     eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, msek);
+       nextActivation += (msek<0 ? 0 : msek);
 //     eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
        context.addTimer(this);
 }
@@ -60,8 +59,7 @@ void eTimer::startLongTimer( int seconds )
        bActive = bSingleShot = true;
        interval = 0;
        gettimeofday(&nextActivation, 0);
-       nextActivation.tv_sec -= context.getTimerOffset();
-//     eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, msek);
+//     eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_usec, seconds);
        if ( seconds > 0 )
                nextActivation.tv_sec += seconds;
 //     eDebug("next Activation sec = %d, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
@@ -108,7 +106,7 @@ void eTimer::activate()   // Internal Funktion... called from eApplication
        /*emit*/ timeout();
 }
 
-inline void eTimer::recalc( int offset )
+void eTimer::addTimeOffset( int offset )
 {
        nextActivation.tv_sec += offset;
 }
@@ -116,20 +114,6 @@ inline void eTimer::recalc( int offset )
 // mainloop
 ePtrList<eMainloop> eMainloop::existing_loops;
 
-void eMainloop::setTimerOffset( int difference )
-{
-       singleLock s(recalcLock);
-       if (!TimerList)
-               timer_offset=0;
-       else
-       {
-               if ( timer_offset )
-                       eDebug("time_offset %d avail.. add new offset %d than new is %d",
-                       timer_offset, difference, timer_offset+difference);
-               timer_offset+=difference;
-       }
-}
-
 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
 {
        notifiers.insert(std::pair<int,eSocketNotifier*> (sn->getFD(), sn));
@@ -137,148 +121,182 @@ void eMainloop::addSocketNotifier(eSocketNotifier *sn)
 
 void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
 {
-       notifiers.erase(sn->getFD());
+       for (std::multimap<int,eSocketNotifier*>::iterator i = notifiers.find(sn->getFD());
+                       i != notifiers.end();
+                       ++i)
+               if (i->second == sn)
+                       return notifiers.erase(i);
+       eFatal("removed socket notifier which is not present");
 }
 
-void eMainloop::processOneEvent()
+int eMainloop::processOneEvent(unsigned int user_timeout)
 {
-               /* notes:
-                 - we should use epoll(4)
-                 - timer are checked twice. there was a strong reason for it, but i can't remember. (FIXME)
-                 - for each time, we gettimeofday() and check wether the timer should fire.
-                   we should do this all better - we know how long the poll last, so we know which
-                   timers should fire. Problem is that a timer handler could have required so
-                   much time that another timer fired.
-
-                   A probably structure could look
-
-                   while (1)
-                   {
-                           time = gettimeofday()
-                           timeout = calculate_pending_timers(time);
-
-                     doPoll(timeout or infinite);
-
-                       if (poll_had_results)
-                               handle_poll_handler();
-                       else
-                                   fire_timers(time + timeout)
-                         }
-
-                         the gettimeofday() call is required because fire_timers could last more
-                         than nothing.
-
-                         when poll did no timeout, we don't handle timers, as this will be done
-                         in the next iteration (without adding overhead - we had to get the new
-                         time anyway
-               */
-
-               // first, process pending timers...
-       long usec=0;
-
-       if ( TimerList )
-               doRecalcTimers();
-       while (TimerList && (usec = timeout_usec( TimerList.begin()->getNextActivation() ) ) <= 0 )
+       int return_reason = 0;
+               /* get current time */
+       timeval now;
+       gettimeofday(&now, 0);
+       m_now_is_invalid = 0;
+       
+       int poll_timeout = -1; /* infinite in case of empty timer list */
+       
+       if (m_timer_list)
        {
-               TimerList.begin()->activate();
-               doRecalcTimers();
+               singleLock s(recalcLock);
+               poll_timeout = timeval_to_usec(m_timer_list.begin()->getNextActivation() - now);
+                       /* if current timer already passed, don't delay infinite. */
+               if (poll_timeout < 0)
+                       poll_timeout = 0;
+                       
+                       /* convert us to ms */
+               poll_timeout /= 1000;
        }
-
-       int fdAnz = notifiers.size();
-       pollfd pfd[fdAnz];
-
-// fill pfd array
-       std::map<int,eSocketNotifier*>::iterator it(notifiers.begin());
-       for (int i=0; i < fdAnz; i++, it++)
+       
+       if ((user_timeout > 0) && (poll_timeout > user_timeout))
        {
-               pfd[i].fd = it->first;
-               pfd[i].events = it->second->getRequested();
+               poll_timeout = user_timeout;
+               return_reason = 1;
        }
-
-               // to the poll. When there are no timers, we have an infinite timeout
-       int ret=poll(pfd, fdAnz, TimerList ? usec / 1000 : -1);  // convert to ms
-
-       if (ret>0)
+       
+       int ret = 0;
+       
+       if (poll_timeout)
        {
-       //              eDebug("bin aussem poll raus und da war was");
-               for (int i=0; i < fdAnz ; i++)
-               {
-                       if( notifiers.find(pfd[i].fd) == notifiers.end())
-                               continue;
 
-                       int req = notifiers[pfd[i].fd]->getRequested();
+               std::multimap<int,eSocketNotifier*>::iterator it;
+               std::map<int,int> fd_merged;
+               std::map<int,int>::const_iterator fd_merged_it;
+               
+               for (it = notifiers.begin(); it != notifiers.end(); ++it)
+                       fd_merged[it->first] |= it->second->getRequested();
+               
+               fd_merged_it = fd_merged.begin();
+               
+               int fdcount = fd_merged.size();
+
+                       // build the poll aray
+               pollfd* pfd = new pollfd[fdcount];  // make new pollfd array
+               
+               for (int i=0; i < fdcount; i++, fd_merged_it++)
+               {
+                       pfd[i].fd = fd_merged_it->first;
+                       pfd[i].events = fd_merged_it->second;
+               }
 
-                       if ( pfd[i].revents & req )
+               ret = poll(pfd, fdcount, poll_timeout);
+               
+                       /* ret > 0 means that there are some active poll entries. */
+               if (ret > 0)
+               {
+                       return_reason = 0;
+                       for (int i=0; i < fdcount ; i++)
+                       {
+                               it = notifiers.begin();
+                               
+                               int handled = 0;
+                               
+                               std::multimap<int,eSocketNotifier*>::iterator 
+                                       l = notifiers.lower_bound(pfd[i].fd),
+                                       u = notifiers.upper_bound(pfd[i].fd);
+                               
+                               ePtrList<eSocketNotifier> n;
+                               
+                               for (; l != u; ++l)
+                                       n.push_back(l->second);
+                               
+                               for (ePtrList<eSocketNotifier>::iterator li(n.begin()); li != n.end(); ++li)
+                               {
+                                       int req = li->getRequested();
+                                       
+                                       handled |= req;
+                               
+                                       if (pfd[i].revents & req)
+                                               (*li)->activate(pfd[i].revents);
+                               }
+                               if ((pfd[i].revents&~handled) & (POLLERR|POLLHUP|POLLNVAL))
+                                       eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
+                       }
+                       
+                       ret = 1; /* poll did not timeout. */
+               } else if (ret < 0)
+               {
+                               /* when we got a signal, we get EINTR. */
+                       if (errno != EINTR)
+                               eDebug("poll made error (%m)");
+                       else
                        {
-                               notifiers[pfd[i].fd]->activate(pfd[i].revents);
-                               if (!--ret)
-                                       break;
+                               return_reason = 2;
+                               ret = -1; /* don't assume the timeout has passed when we got a signal */
                        }
-                       else if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
-                               eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd,pfd[i].revents);
                }
+               delete [] pfd;
        }
-       else if (ret<0)
+       
+               /* when we not processed anything, check timers. */
+       if (!ret)
        {
-                       /* when we got a signal, we get EINTR. we do not care,
-                          because we check current time in timers anyway. */
-               if (errno != EINTR)
-                       eDebug("poll made error");
+                       /* we know that this time has passed. */
+               now += poll_timeout;
+               
+               singleLock s(recalcLock);
+
+                       /* this will never change while we have the recalcLock */
+                       /* we can savely return here, the timer will be re-checked soon. */
+               if (m_now_is_invalid)
+                       return 0;
+
+                       /* process all timers which are ready. first remove them out of the list. */
+               while ((!m_timer_list.empty()) && (m_timer_list.begin()->getNextActivation() <= now))
+                       m_timer_list.begin()->activate();
        }
+       
+       return return_reason;
 }
 
-int eMainloop::exec()
+void eMainloop::addTimer(eTimer* e)
 {
-       if (!loop_level)
-       {
-               app_quit_now = false;
-               app_exit_loop = false;
-               enter_loop();
-       }
-       return retval;
+       m_timer_list.insert_in_order(e);
 }
 
-void eMainloop::enter_loop()
+void eMainloop::removeTimer(eTimer* e)
 {
-       loop_level++;
-       // Status der vorhandenen Loop merken
-       bool old_exit_loop = app_exit_loop;
-
-       app_exit_loop = false;
-
-       while (!app_exit_loop && !app_quit_now)
-               processOneEvent();
-
-       // wiederherstellen der vorherigen app_exit_loop
-       app_exit_loop = old_exit_loop;
-
-       --loop_level;
+       m_timer_list.remove(e);
+}
 
-       if (!loop_level)
-       {
-               // do something here on exit the last loop
-       }
+int eMainloop::iterate(unsigned int user_timeout)
+{
+       int ret = 0;
+       
+       do
+       { 
+               if (app_quit_now) break; 
+               ret = processOneEvent(user_timeout);
+       } while (ret == 0);
+       
+       return ret;
 }
 
-void eMainloop::exit_loop()  // call this to leave the current loop
+int eMainloop::runLoop()
 {
-       app_exit_loop = true;
+       while (!app_quit_now)
+               iterate();
+       return retval;
 }
 
-void eMainloop::quit( int ret )   // call this to leave all loops
+void eMainloop::quit(int ret)
 {
-       retval=ret;
+       retval = ret;
        app_quit_now = true;
 }
 
-inline void eMainloop::doRecalcTimers()
+void eMainloop::addTimeOffset(int offset)
 {
-       singleLock s(recalcLock);
-       if ( timer_offset )
+       for (ePtrList<eMainloop>::iterator it(eMainloop::existing_loops)
+               ;it != eMainloop::existing_loops.end(); ++it)
        {
-               for (ePtrList<eTimer>::iterator it(TimerList); it != TimerList.end(); ++it )
-                       it->recalc( timer_offset );
-               timer_offset=0;
+               singleLock s(it->recalcLock);
+               it->m_now_is_invalid = 1;
+               for (ePtrList<eTimer>::iterator tit = it->m_timer_list.begin(); tit != it->m_timer_list.end(); ++tit )
+                       tit->addTimeOffset(offset);
        }
 }