Commit e25f1a0a authored by Jason Madden's avatar Jason Madden

Use vfd_socket_t on all platforms.

cython ignores the preprocessor directives we previously had in
libev.pxd, so that was doing nothing, and it was treating vfd_socket_t
just as something it had to turn into an int.

The function it used to do this was the same function it used to turn
a generic PyObject into an int, once all the typedefs were substituted
in (which does use the c preprocessor from libev_vfd.h). So it should
be functionally equivalent.

The vfd_* functions are no-op macros on POSIX, so there shouldn't be a
performance difference. The minor exception is that __dealloc__ is
always defined, and although it's a static function suitable to
inline, there is an extra Python API call to PyErr_Fetch and
PyErr_Restore. But if that makes a difference, I didn't spot it.
parent 431575b4
......@@ -459,13 +459,8 @@ cdef public class loop [object PyGeventLoopObject, type PyGeventLoop_Type]:
_check_loop(self)
return libev.ev_pending_count(self._ptr)
#ifdef _WIN32
def io(self, libev.vfd_socket_t fd, int events, ref=True, priority=None):
return io(self, fd, events, ref, priority)
#else
def io(self, int fd, int events, ref=True, priority=None):
return io(self, fd, events, ref, priority)
#endif
def timer(self, double after, double repeat=0.0, ref=True, priority=None):
return timer(self, after, repeat, ref, priority)
......@@ -660,6 +655,7 @@ cdef void _libev_unref(watcher self):
libev.ev_unref(self.loop._ptr)
self._flags |= FLAG_WATCHER_NEEDS_EVREF
cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Type]:
"""Abstract base class for all the watchers"""
cdef public loop loop
......@@ -798,7 +794,6 @@ cdef public class watcher [object PyGeventWatcherObject, type PyGeventWatcher_Ty
self.close()
return
cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]:
WATCHER(io)
......@@ -808,36 +803,22 @@ cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]:
args = (GEVENT_CORE_EVENTS, ) + args
watcher.start(self, callback, *args)
cpdef __posix_cinit(self, loop loop, int fd, int events, ref=True, priority=None):
def __init__(self, loop loop, libev.vfd_socket_t fd, int events, ref=True, priority=None):
watcher.__init__(self, loop, ref, priority)
def __cinit__(self, loop loop, libev.vfd_socket_t fd, int events, ref=True, priority=None):
if fd < 0:
raise ValueError('fd must be non-negative: %r' % fd)
if events & ~(libev.EV__IOFDSET | libev.EV_READ | libev.EV_WRITE):
raise ValueError('illegal event mask: %r' % events)
# All the vfd_functions are no-ops on POSIX
cdef int vfd = libev.vfd_open(fd)
libev.ev_io_init(&self._watcher, <void *>gevent_callback_io, fd, events)
self.__watcher = <libev.ev_watcher*>&self._watcher
#ifdef _WIN32
def __cinit__(self, loop loop, libev.vfd_socket_t fd, int events, ref=True, priority=None):
if events & ~(libev.EV__IOFDSET | libev.EV_READ | libev.EV_WRITE):
raise ValueError('illegal event mask: %r' % events)
cdef int vfd = libev.vfd_open(fd)
self.__posix_cinit(loop, vfd, events, ref, priority)
def __init__(self, loop loop, libev.vfd_socket_t fd, int events, ref=True, priority=None):
watcher.__init__(self, loop, ref, priority)
def __dealloc__(self):
libev.vfd_free(self._watcher.fd)
# cythonpp can produce code that cannot compile if the same methods are
# defined in blocks too close together. So the POSIX version of this
# has to be defined several methods down.
#endif
@property
def fd(self):
return libev.vfd_get(self._watcher.fd)
......@@ -867,18 +848,6 @@ cdef public class io(watcher) [object PyGeventIOObject, type PyGeventIO_Type]:
def _format(self):
return ' fd=%s events=%s' % (self.fd, self.events_str)
#ifdef _WIN32
#else
def __cinit__(self, loop loop, int fd, int events, ref=True, priority=None):
self.__posix_cinit(loop, fd, events, ref, priority)
def __init__(self, loop loop, int fd, int events, ref=True, priority=None):
watcher.__init__(self, loop, ref, priority)
#endif
cdef public class timer(watcher) [object PyGeventTimerObject, type PyGeventTimer_Type]:
......
#if defined(LIBEV_EMBED)
#include "ev.c"
#else
#else /* !LIBEV_EMBED */
#include "ev.h"
#ifndef _WIN32
#include <signal.h>
#endif
#endif /* !_WIN32 */
#endif
#endif /* LIBEV_EMBED */
#ifndef _WIN32
......@@ -58,9 +58,9 @@ static void gevent_reset_sigchld_handler(void) {
}
}
#else
#else /* !_WIN32 */
#define gevent_ev_default_loop ev_default_loop
static void gevent_install_sigchld_handler(void) { }
#endif
#endif /* _WIN32 */
cdef extern from "libev_vfd.h":
#ifdef _WIN32
#ifdef _WIN64
# cython doesn't process pre-processor directives, so they
# don't matter in this file. It just takes the last definition it sees.
ctypedef long long vfd_socket_t
#else
ctypedef long vfd_socket_t
#endif
#else
ctypedef int vfd_socket_t
#endif
long vfd_get(int)
int vfd_open(long) except -1
void vfd_free(int)
......
#ifdef _WIN32
#ifdef _WIN64
# ifdef _WIN64
typedef PY_LONG_LONG vfd_socket_t;
#define vfd_socket_object PyLong_FromLongLong
#else
# define vfd_socket_object PyLong_FromLongLong
# else /* _WIN32 && !_WIN64 */
typedef long vfd_socket_t;
#define vfd_socket_object PyInt_FromLong
#endif
# define vfd_socket_object PyInt_FromLong
#endif /* _WIN64 */
#ifdef LIBEV_EMBED
/*
* If libev on win32 is embedded, then we can use an
......@@ -53,13 +56,13 @@ static CRITICAL_SECTION* vfd_make_lock()
#define VFD_GIL_DECLARE PyGILState_STATE ___save
#define VFD_GIL_ENSURE ___save = PyGILState_Ensure()
#define VFD_GIL_RELEASE PyGILState_Release(___save)
#else
#else /* ! WITH_THREAD */
#define VFD_LOCK_ENTER
#define VFD_LOCK_LEAVE
#define VFD_GIL_DECLARE
#define VFD_GIL_ENSURE
#define VFD_GIL_RELEASE
#endif
#endif /*_WITH_THREAD */
/*
* Given a virtual fd returns an OS handle or -1
......@@ -201,7 +204,7 @@ done:
#define vfd_free(fd) vfd_free_((fd), 0)
#define EV_WIN32_CLOSE_FD(fd) vfd_free_((fd), 1)
#else
#else /* !LIBEV_EMBED */
/*
* If libev on win32 is not embedded in gevent, then
* the only way to map vfds is to use the default of
......@@ -211,8 +214,9 @@ done:
#define vfd_get(fd) _get_osfhandle((fd))
#define vfd_open(fd) _open_osfhandle((fd), 0)
#define vfd_free(fd)
#endif
#else
#endif /* LIBEV_EMBED */
#else /* !_WIN32 */
/*
* On non-win32 platforms vfd_* are noop macros
*/
......@@ -220,4 +224,4 @@ typedef int vfd_socket_t;
#define vfd_get(fd) (fd)
#define vfd_open(fd) ((int)(fd))
#define vfd_free(fd)
#endif
#endif /* _WIN32 */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment