X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp;a=blobdiff_plain;f=lib%2Fbase%2Febase.cpp;h=700cce368d2c5224cd4ae679e7d0632c7f5bc8e2;hp=62746f4be4ea144618b9ccc1054ea53015e9d103;hb=68685e85480d5539ba0bb884c4ebeaa8c31a07f9;hpb=8ecfc549ce93bc353d6a64f43a0c25dcd65f860e diff --git a/lib/base/ebase.cpp b/lib/base/ebase.cpp index 62746f4..700cce3 100644 --- a/lib/base/ebase.cpp +++ b/lib/base/ebase.cpp @@ -8,9 +8,11 @@ #include #include +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(); } @@ -24,47 +26,58 @@ 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() { if (state) + { + state=0; context.removeSocketNotifier(this); - - state=0; + } } - // 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() @@ -110,6 +123,11 @@ void eTimer::activate() // Internal Funktion... called from eApplication // mainloop ePtrList 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); @@ -122,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; } @@ -131,8 +160,13 @@ void eMainloop::removeSocketNotifier(eSocketNotifier *sn) int fd = sn->getFD(); std::map::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=%p", 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) @@ -152,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 */ @@ -175,6 +214,7 @@ int eMainloop::processOneEvent(unsigned int twisted_timeout, PyObject **res, ePy // build the poll aray pollfd pfd[fdcount]; // make new pollfd array std::map::iterator it = notifiers.begin(); + int i=0; for (; i < nativecount; ++i, ++it) { @@ -199,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); @@ -228,10 +264,15 @@ 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); + m_inActivate = it->second; + int req = m_inActivate->getRequested(); + if (pfd[i].revents & req) { + 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); @@ -368,16 +409,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; } @@ -387,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); } @@ -395,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; } @@ -436,8 +481,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); @@ -572,16 +617,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; } @@ -591,7 +638,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); } @@ -615,7 +662,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; } @@ -748,9 +796,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)