better solution to add possibility to delete eSocketNotifiers,
[vuplus_dvbapp] / lib / base / ebase.cpp
index 038e5ad..a66d195 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,9 +33,10 @@ void eSocketNotifier::start()
 void eSocketNotifier::stop()
 {
        if (state)
+       {
+               state=0;
                context.removeSocketNotifier(this);
-
-       state=0;
+       }
 }
 
                                        // timer
@@ -130,8 +134,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)
@@ -147,18 +156,15 @@ 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())
        {
-               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 )
-                               m_timer_list.begin()->activate();
-                       if (poll_timeout < 0)
-                               poll_timeout = 0;
-                       else /* convert us to ms */
-                               poll_timeout /= 1000;
-               }
+               /* 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();
+               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))
@@ -177,6 +183,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)
        {
@@ -187,6 +194,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)) {
@@ -199,9 +211,14 @@ 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);
 
@@ -220,9 +237,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))
@@ -360,16 +381,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;
 }
 
@@ -428,8 +452,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);
@@ -564,16 +588,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;
 }
 
@@ -583,7 +609,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);
 }
 
@@ -607,7 +633,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;
 }
@@ -740,9 +767,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)