lib/base/ebase.cpp: remove unneeded code
[enigma2.git] / lib / base / ebase.cpp
index a66d1958e9806c85bf087747ce8b3d29e254e4fb..c84f28b8e0ea2b7f8021c32ea63cef85b11fe7ff 100644 (file)
@@ -26,8 +26,11 @@ void eSocketNotifier::start()
        if (state)
                stop();
 
-       context.addSocketNotifier(this);
-       state=2;  // running but not in poll yet
+       if (eMainloop::isValid(&context))
+       {
+               context.addSocketNotifier(this);
+               state=2;  // running but not in poll yet
+       }
 }
 
 void eSocketNotifier::stop()
@@ -39,35 +42,42 @@ void eSocketNotifier::stop()
        }
 }
 
-                                       // timer
+DEFINE_REF(eTimer);
+
 void eTimer::start(long msek, bool singleShot)
 {
        if (bActive)
                stop();
 
-       bActive = true;
-       bSingleShot = singleShot;
-       interval = msek;
-       clock_gettime(CLOCK_MONOTONIC, &nextActivation);
-//     eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_nsec, msek);
-       nextActivation += (msek<0 ? 0 : msek);
-//     eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
-       context.addTimer(this);
+       if (eMainloop::isValid(&context))
+       {
+               bActive = true;
+               bSingleShot = singleShot;
+               interval = msek;
+               clock_gettime(CLOCK_MONOTONIC, &nextActivation);
+//             eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_nsec, msek);
+               nextActivation += (msek<0 ? 0 : msek);
+//             eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
+               context.addTimer(this);
+       }
 }
 
-void eTimer::startLongTimer( int seconds )
+void eTimer::startLongTimer(int seconds)
 {
        if (bActive)
                stop();
 
-       bActive = bSingleShot = true;
-       interval = 0;
-       clock_gettime(CLOCK_MONOTONIC, &nextActivation);
-//     eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_nsec, seconds);
-       if ( seconds > 0 )
-               nextActivation.tv_sec += seconds;
-//     eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
-       context.addTimer(this);
+       if (eMainloop::isValid(&context))
+       {
+               bActive = bSingleShot = true;
+               interval = 0;
+               clock_gettime(CLOCK_MONOTONIC, &nextActivation);
+//             eDebug("this = %p\nnow sec = %d, nsec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_nsec, seconds);
+               if ( seconds > 0 )
+                       nextActivation.tv_sec += seconds;
+//             eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
+               context.addTimer(this);
+       }
 }
 
 void eTimer::stop()
@@ -113,6 +123,11 @@ void eTimer::activate()   // Internal Funktion... called from eApplication
 // mainloop
 ePtrList<eMainloop> eMainloop::existing_loops;
 
+bool eMainloop::isValid(eMainloop *ml)
+{
+       return std::find(existing_loops.begin(), existing_loops.end(), ml) != existing_loops.end();
+}
+
 eMainloop::~eMainloop()
 {
        existing_loops.remove(this);
@@ -125,6 +140,17 @@ eMainloop::~eMainloop()
 void eMainloop::addSocketNotifier(eSocketNotifier *sn)
 {
        int fd = sn->getFD();
+       if (m_inActivate && m_inActivate->ref.count == 1)
+       {
+               /*  when the current active SocketNotifier's refcount is one,
+                       then no more external references are existing.
+                       So it gets destroyed when the activate callback is finished (->AddRef() / ->Release() calls in processOneEvent).
+                       But then the sn->stop() is called to late for the next Asserion.
+                       Thus we call sn->stop() here (this implicitly calls eMainloop::removeSocketNotifier) and we don't get trouble
+                       with the next Assertion.
+               */
+               m_inActivate->stop();
+       }
        ASSERT(notifiers.find(fd) == notifiers.end());
        notifiers[fd]=sn;
 }
@@ -160,7 +186,12 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy
        {
                /* process all timers which are ready. first remove them out of the list. */
                while (!m_timer_list.empty() && (poll_timeout = timeout_usec( m_timer_list.begin()->getNextActivation() ) ) <= 0 )
-                       m_timer_list.begin()->activate();
+               {
+                       eTimer *tmr = m_timer_list.begin();
+                       tmr->AddRef();
+                       tmr->activate();
+                       tmr->Release();
+               }
                if (poll_timeout < 0)
                        poll_timeout = 0;
                else /* convert us to ms */
@@ -208,17 +239,13 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy
        }
 
        m_is_idle = 1;
+       ++m_idle_count;
 
        if (this == eApp)
        {
-               gOpcode op;
-               op.dc = 0;
-               op.opcode = gOpcode::flush;
-               gRC::getInstance()->submit(op);
                Py_BEGIN_ALLOW_THREADS
                ret = ::poll(pfd, fdcount, poll_timeout);
                Py_END_ALLOW_THREADS
-               
        } else
                ret = ::poll(pfd, fdcount, poll_timeout);
 
@@ -237,14 +264,15 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy
                                if (it != notifiers.end()
                                        && it->second->state == 1) // added and in poll
                                {
-                                       eSocketNotifier *sn = it->second;
-                                       int req = sn->getRequested();
+                                       m_inActivate = it->second;
+                                       int req = m_inActivate->getRequested();
                                        if (pfd[i].revents & req) {
-                                               sn->AddRef();
-                                               sn->activate(pfd[i].revents & req);
-                                               sn->Release();
+                                               m_inActivate->AddRef();
+                                               m_inActivate->activate(pfd[i].revents & req);
+                                               m_inActivate->Release();
                                        }
                                        pfd[i].revents &= ~req;
+                                       m_inActivate = 0;
                                }
                                if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
                                        eDebug("poll: unhandled POLLERR/HUP/NVAL for fd %d(%d)", pfd[i].fd, pfd[i].revents);
@@ -403,7 +431,7 @@ eTimerPy_dealloc(eTimerPy* self)
        if (self->in_weakreflist != NULL)
                PyObject_ClearWeakRefs((PyObject *) self);
        eTimerPy_clear(self);
-       delete self->tm;
+       self->tm->Release();
        self->ob_type->tp_free((PyObject*)self);
 }
 
@@ -411,7 +439,8 @@ static PyObject *
 eTimerPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
 {
        eTimerPy *self = (eTimerPy *)type->tp_alloc(type, 0);
-       self->tm = new eTimer(eApp);
+       self->tm = eTimer::create(eApp);
+       self->tm->AddRef();
        self->in_weakreflist = NULL;
        return (PyObject *)self;
 }