From: Felix Domke Date: Sat, 25 Feb 2006 02:48:51 +0000 (+0000) Subject: some fixes for twisted X-Git-Url: http://code.vuplus.com/gitweb/?p=vuplus_dvbapp;a=commitdiff_plain;h=842dabf727814691bfd949ac4d910ce04c32b887 some fixes for twisted --- diff --git a/e2reactor.py b/e2reactor.py index 1ecd40e..7caeb7f 100644 --- a/e2reactor.py +++ b/e2reactor.py @@ -35,7 +35,8 @@ class E2SharedPoll: del self.dict[fd] def poll(self, timeout = None): - return getApplication().poll(timeout, self.dict) + r = getApplication().poll(timeout, self.dict) + return r poller = E2SharedPoll() @@ -56,6 +57,8 @@ class PollReactor(posixbase.PosixReactorBase): poller.register(fd, mask) else: if selectables.has_key(fd): del selectables[fd] + + getApplication().interruptPoll() def _dictRemove(self, selectable, mdict): try: @@ -131,6 +134,7 @@ class PollReactor(posixbase.PosixReactorBase): POLLIN=select.POLLIN, POLLOUT=select.POLLOUT): """Poll the poller for new events.""" + if timeout is not None: timeout = int(timeout * 1000) # convert seconds to milliseconds @@ -183,6 +187,9 @@ class PollReactor(posixbase.PosixReactorBase): if why: self._disconnectSelectable(selectable, why, inRead) + def callLater(self, *args, **kwargs): + getApplication().interruptPoll() + return posixbase.PosixReactorBase.callLater(self, *args, **kwargs) def install(): """Install the poll() reactor.""" diff --git a/lib/base/ebase.cpp b/lib/base/ebase.cpp index 7a57d53..addd1b8 100644 --- a/lib/base/ebase.cpp +++ b/lib/base/ebase.cpp @@ -165,7 +165,6 @@ int eMainloop::processOneEvent(unsigned int user_timeout, PyObject **res, PyObje int ret = 0; - if (poll_timeout) { std::multimap::iterator it; @@ -312,10 +311,31 @@ int eMainloop::iterate(unsigned int user_timeout, PyObject **res, PyObject *dict { int ret = 0; + timeval user_timer; + gettimeofday(&user_timer, 0); + user_timer += user_timeout; + + /* TODO: this code just became ugly. fix that. */ do - { + { + if (m_interrupt_requested) + { + m_interrupt_requested = 0; + return 0; + } if (app_quit_now) return -1; - ret = processOneEvent(user_timeout, res, dict); + timeval now, timeout; + gettimeofday(&now, 0); + timeout = user_timer - now; + + if (user_timeout && (user_timer <= now)) + return 0; + + int to = 0; + if (user_timeout) + to = timeout.tv_sec * 1000 + timeout.tv_usec / 1000; + + ret = processOneEvent(to, res, dict); if (res && *res) return ret; } while (ret == 0); @@ -341,7 +361,7 @@ PyObject *eMainloop::poll(PyObject *timeout, PyObject *dict) } int user_timeout = (timeout == Py_None) ? 0 : PyInt_AsLong(timeout); - + iterate(user_timeout, &res, dict); if (!res) /* return empty list on timeout */ @@ -350,6 +370,11 @@ PyObject *eMainloop::poll(PyObject *timeout, PyObject *dict) return res; } +void eMainloop::interruptPoll() +{ + m_interrupt_requested = 1; +} + void eMainloop::quit(int ret) { retval = ret; diff --git a/lib/base/ebase.h b/lib/base/ebase.h index a224e2c..b819fc3 100644 --- a/lib/base/ebase.h +++ b/lib/base/ebase.h @@ -187,6 +187,7 @@ class eMainloop pthread_mutex_t recalcLock; int m_now_is_invalid; + int m_interrupt_requested; #endif public: static void addTimeOffset(int offset); @@ -200,7 +201,7 @@ public: #endif eMainloop() - :app_quit_now(0),loop_level(0),retval(0) + :app_quit_now(0),loop_level(0),retval(0), m_interrupt_requested(0) { m_now_is_invalid = 0; existing_loops.push_back(this); @@ -230,6 +231,7 @@ public: /* our new shared polling interface. */ PyObject *poll(PyObject *dict, PyObject *timeout); + void interruptPoll(); }; /**