dont add cmd to arglist
[enigma2.git] / lib / base / ebase.cpp
index 01ee6a5386fc3a4a74d77d00e92ba6a37af6b1ae..bd2ec589227946199b60071faf4ab4617dc850ba 100644 (file)
@@ -6,10 +6,13 @@
 
 #include <lib/base/eerror.h>
 #include <lib/base/elock.h>
+#include <lib/gdi/grc.h>
+
+DEFINE_REF(eSocketNotifier);
 
 eSocketNotifier::eSocketNotifier(eMainloop *context, int fd, int requested, bool startnow): context(*context), fd(fd), state(0), requested(requested)
 {
-       if (startnow)   
+       if (startnow)
                start();
 }
 
@@ -30,12 +33,14 @@ void eSocketNotifier::start()
 void eSocketNotifier::stop()
 {
        if (state)
+       {
+               state=0;
                context.removeSocketNotifier(this);
-
-       state=0;
+       }
 }
 
-                                       // timer
+DEFINE_REF(eTimer);
+
 void eTimer::start(long msek, bool singleShot)
 {
        if (bActive)
@@ -44,11 +49,10 @@ void eTimer::start(long msek, bool singleShot)
        bActive = true;
        bSingleShot = singleShot;
        interval = msek;
-       gettimeofday(&nextActivation, 0);
-       nextActivation.tv_sec -= context.getTimeOffset();
-//     eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d msec", this, nextActivation.tv_sec, nextActivation.tv_usec, 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, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
+//     eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
        context.addTimer(this);
 }
 
@@ -59,12 +63,11 @@ void eTimer::startLongTimer( int seconds )
 
        bActive = bSingleShot = true;
        interval = 0;
-       gettimeofday(&nextActivation, 0);
-       nextActivation.tv_sec -= context.getTimeOffset();
-//     eDebug("this = %p\nnow sec = %d, usec = %d\nadd %d sec", this, nextActivation.tv_sec, nextActivation.tv_usec, seconds);
+       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, usec = %d", nextActivation.tv_sec, nextActivation.tv_usec );
+//     eDebug("next Activation sec = %d, nsec = %d", nextActivation.tv_sec, nextActivation.tv_nsec );
        context.addTimer(this);
 }
 
@@ -108,18 +111,12 @@ void eTimer::activate()   // Internal Funktion... called from eApplication
        /*emit*/ timeout();
 }
 
-void eTimer::addTimeOffset( int offset )
-{
-       nextActivation.tv_sec += offset;
-}
-
 // mainloop
 ePtrList<eMainloop> eMainloop::existing_loops;
 
 eMainloop::~eMainloop()
 {
        existing_loops.remove(this);
-       pthread_mutex_destroy(&recalcLock);
        for (std::map<int, eSocketNotifier*>::iterator it(notifiers.begin());it != notifiers.end();++it)
                it->second->stop();
        while(m_timer_list.begin() != m_timer_list.end())
@@ -138,8 +135,13 @@ void eMainloop::removeSocketNotifier(eSocketNotifier *sn)
        int fd = sn->getFD();
        std::map<int,eSocketNotifier*>::iterator i(notifiers.find(fd));
        if (i != notifiers.end())
-               return notifiers.erase(i);
-       eFatal("removed socket notifier which is not present");
+       {
+               notifiers.erase(i);
+               return;
+       }
+       for (i = notifiers.begin(); i != notifiers.end(); ++i)
+               eDebug("fd=%d, sn=%d", i->second->getFD(), (void*)i->second);
+       eFatal("removed socket notifier which is not present, fd=%d", fd);
 }
 
 int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePyObject additional)
@@ -155,22 +157,20 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy
 
        long poll_timeout = -1; /* infinite in case of empty timer list */
 
-       if (!m_timer_list.empty() || twisted_timeout > 0)
+       if (!m_timer_list.empty())
        {
-               applyTimeOffset();
-               if (!m_timer_list.empty())
+               /* 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 )
                {
-                       /* 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();
-                               applyTimeOffset();
-                       }
-                       if (poll_timeout < 0)
-                               poll_timeout = 0;
-                       else /* convert us to ms */
-                               poll_timeout /= 1000;
+                       eTimer *tmr = m_timer_list.begin();
+                       tmr->AddRef();
+                       tmr->activate();
+                       tmr->Release();
                }
+               if (poll_timeout < 0)
+                       poll_timeout = 0;
+               else /* convert us to ms */
+                       poll_timeout /= 1000;
        }
 
        if ((twisted_timeout > 0) && (poll_timeout > 0) && ((unsigned int)poll_timeout > twisted_timeout))
@@ -189,6 +189,7 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy
                // build the poll aray
        pollfd pfd[fdcount];  // make new pollfd array
        std::map<int,eSocketNotifier*>::iterator it = notifiers.begin();
+
        int i=0;
        for (; i < nativecount; ++i, ++it)
        {
@@ -199,6 +200,11 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy
 
        if (additional)
        {
+#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
+               typedef int Py_ssize_t;
+# define PY_SSIZE_T_MAX INT_MAX
+# define PY_SSIZE_T_MIN INT_MIN
+#endif
                PyObject *key, *val;
                Py_ssize_t pos=0;
                while (PyDict_Next(additional, &pos, &key, &val)) {
@@ -211,12 +217,17 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy
 
        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);
-       
+
        m_is_idle = 0;
 
                        /* ret > 0 means that there are some active poll entries. */
@@ -232,9 +243,13 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy
                                if (it != notifiers.end()
                                        && it->second->state == 1) // added and in poll
                                {
-                                       int req = it->second->getRequested();
-                                       if (pfd[i].revents & req)
-                                               it->second->activate(pfd[i].revents & req);
+                                       eSocketNotifier *sn = it->second;
+                                       int req = sn->getRequested();
+                                       if (pfd[i].revents & req) {
+                                               sn->AddRef();
+                                               sn->activate(pfd[i].revents & req);
+                                               sn->Release();
+                                       }
                                        pfd[i].revents &= ~req;
                                }
                                if (pfd[i].revents & (POLLERR|POLLHUP|POLLNVAL))
@@ -283,7 +298,7 @@ int eMainloop::iterate(unsigned int twisted_timeout, PyObject **res, ePyObject d
 
        if (twisted_timeout)
        {
-               gettimeofday(&m_twisted_timer, 0);
+               clock_gettime(CLOCK_MONOTONIC, &m_twisted_timer);
                m_twisted_timer += twisted_timeout;
        }
 
@@ -302,15 +317,12 @@ int eMainloop::iterate(unsigned int twisted_timeout, PyObject **res, ePyObject d
                int to = 0;
                if (twisted_timeout)
                {
-                       timeval now, timeout;
-                       gettimeofday(&now, 0);
-                       m_twisted_timer += time_offset;  // apply pending offset
+                       timespec now, timeout;
+                       clock_gettime(CLOCK_MONOTONIC, &now);
                        if (m_twisted_timer<=now) // timeout
                                return 0;
                        timeout = m_twisted_timer - now;
-                       to = timeout.tv_sec * 1000 + timeout.tv_usec / 1000;
-                       // remove pending offset .. it is re-applied in next call of processOneEvent.. applyTimeOffset
-                       m_twisted_timer -= time_offset;  
+                       to = timeout.tv_sec * 1000 + timeout.tv_nsec / 1000000;
                }
                ret = processOneEvent(to, res, dict);
        } while ( !ret && !(res && *res) );
@@ -357,38 +369,6 @@ void eMainloop::quit(int ret)
        app_quit_now = true;
 }
 
-void eMainloop::addTimeOffset(int offset)
-{
-       for (ePtrList<eMainloop>::iterator it(existing_loops.begin()); it != existing_loops.end(); ++it )
-               it->addInstanceTimeOffset(offset);
-}
-
-void eMainloop::addInstanceTimeOffset(int offset)
-{
-       singleLock s(recalcLock);
-       if (m_timer_list.empty())
-               time_offset=0;
-       else
-       {
-               if ( time_offset )
-                       eDebug("time_offset %d avail.. add new offset %d than new is %d",
-                       time_offset, offset, time_offset+offset);
-               time_offset+=offset;
-       }
-}
-
-void eMainloop::applyTimeOffset()
-{
-       singleLock s(recalcLock);
-       if ( time_offset )
-       {
-               for (ePtrList<eTimer>::iterator it(m_timer_list.begin()); it != m_timer_list.end(); ++it )
-                       it->addTimeOffset( time_offset );
-               m_twisted_timer += time_offset;
-               time_offset=0;
-       }
-}
-
 eApplication* eApp = 0;
 
 #include "structmember.h"
@@ -407,16 +387,19 @@ struct eTimerPy
 static int
 eTimerPy_traverse(eTimerPy *self, visitproc visit, void *arg)
 {
-       PyObject *obj = self->tm->timeout.get();
-       Py_VISIT(obj);
+       PyObject *obj = self->tm->timeout.getSteal();
+       if (obj) {
+               Py_VISIT(obj);
+       }
        return 0;
 }
 
 static int
 eTimerPy_clear(eTimerPy *self)
 {
-       PyObject *obj = self->tm->timeout.get();
-       Py_CLEAR(obj);
+       PyObject *obj = self->tm->timeout.getSteal(true);
+       if (obj)
+               Py_CLEAR(obj);
        return 0;
 }
 
@@ -426,7 +409,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);
 }
 
@@ -434,7 +417,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;
 }
@@ -475,8 +459,8 @@ eTimerPy_start(eTimerPy* self, PyObject *args)
 static PyObject *
 eTimerPy_start_long(eTimerPy* self, PyObject *args)
 {
-       long v=0;
-       if (!PyArg_ParseTuple(args, "l", &v)) {
+       int v=0;
+       if (!PyArg_ParseTuple(args, "i", &v)) {
                return NULL;
        }
        self->tm->startLongTimer(v);
@@ -611,16 +595,18 @@ struct eSocketNotifierPy
 static int
 eSocketNotifierPy_traverse(eSocketNotifierPy *self, visitproc visit, void *arg)
 {
-       PyObject *obj = self->sn->activated.get();
-       Py_VISIT(obj);
+       PyObject *obj = self->sn->activated.getSteal();
+       if (obj)
+               Py_VISIT(obj);
        return 0;
 }
 
 static int
 eSocketNotifierPy_clear(eSocketNotifierPy *self)
 {
-       PyObject *obj = self->sn->activated.get();
-       Py_CLEAR(obj);
+       PyObject *obj = self->sn->activated.getSteal(true);
+       if (obj)
+               Py_CLEAR(obj);
        return 0;
 }
 
@@ -630,7 +616,7 @@ eSocketNotifierPy_dealloc(eSocketNotifierPy* self)
        if (self->in_weakreflist != NULL)
                PyObject_ClearWeakRefs((PyObject *) self);
        eSocketNotifierPy_clear(self);
-       delete self->sn;
+       self->sn->Release();
        self->ob_type->tp_free((PyObject*)self);
 }
 
@@ -654,7 +640,8 @@ eSocketNotifierPy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
        }
        else if (size < 2 || !PyArg_ParseTuple(args, "ii", &fd, &req))
                return NULL;
-       self->sn = new eSocketNotifier(eApp, fd, req, immediate_start);
+       self->sn = eSocketNotifier::create(eApp, fd, req, immediate_start);
+       self->sn->AddRef();
        self->in_weakreflist = NULL;
        return (PyObject *)self;
 }
@@ -787,9 +774,7 @@ static PyMethodDef module_methods[] = {
 
 void eBaseInit(void)
 {
-       PyObject* m;
-
-       m = Py_InitModule3("eBaseImpl", module_methods,
+       PyObject* m = Py_InitModule3("eBaseImpl", module_methods,
                "Module that implements some enigma classes with working cyclic garbage collection.");
 
        if (m == NULL)