From b3c87d862e8f44754113ee9bf374e9fcfbc9e7ac Mon Sep 17 00:00:00 2001 Message-Id: From: Mark Wooding Date: Mon, 6 Feb 2006 18:17:44 +0000 Subject: [PATCH] Acquire and release the GIL around select callbacks. Organization: Straylight/Edgeware From: mdw --- bres.pyx | 6 +++++- conn.pyx | 6 +++++- defs.pxi | 3 +++ ident.pyx | 6 +++++- sel-base.pyx | 6 +++++- sel-file.pyx | 6 +++++- sel-timer.pyx | 6 +++++- selbuf.pyx | 6 +++++- selpk.pyx | 9 +++++++-- sig.pyx | 6 +++++- 10 files changed, 50 insertions(+), 10 deletions(-) diff --git a/bres.pyx b/bres.pyx index 162ff02..ebc6262 100644 --- a/bres.pyx +++ b/bres.pyx @@ -88,7 +88,7 @@ cdef class SelResolveByAddr (SelResolve): def __init__(me, addr, resolvedproc = None, failedproc = None): pass -cdef void _resfunc(hostent *h, void *arg): +cdef void _resfunc2(hostent *h, void *arg): cdef SelResolve r cdef int i r = arg @@ -107,6 +107,10 @@ cdef void _resfunc(hostent *h, void *arg): addr.append(inet_ntoa((h.h_addr_list[i])[0])) i = i + 1 r.resolved(h.h_name, alias, addr) +cdef void _resfunc(hostent *h, void *arg): + PyEval_AcquireLock() + _resfunc2(h, arg) + PyEval_ReleaseLock() bres_exec(NULL) bres_init(&_sel) diff --git a/conn.pyx b/conn.pyx index e485f5d..0bb0ee7 100644 --- a/conn.pyx +++ b/conn.pyx @@ -74,7 +74,7 @@ cdef class SelConnect: def error(me, errno, strerror): return _maybecall(me._error, ()) -cdef void _connfunc(int fd, void *arg): +cdef void _connfunc2(int fd, void *arg): cdef SelConnect c c = arg c._dead() @@ -83,5 +83,9 @@ cdef void _connfunc(int fd, void *arg): c.error(errno, strerror(errno)) else: c.connected() +cdef void _connfunc(int fd, void *arg): + PyEval_AcquireLock() + _connfunc2(fd, arg) + PyEval_ReleaseLock() #----- That's all, folks ---------------------------------------------------- diff --git a/defs.pxi b/defs.pxi index 10502f7..c579c1d 100644 --- a/defs.pxi +++ b/defs.pxi @@ -94,6 +94,9 @@ cdef extern from 'Python.h': object PyInt_FromLong(long i) object PyLong_FromUnsignedLong(unsigned long i) + void PyEval_AcquireLock() + void PyEval_ReleaseLock() + ctypedef struct PyObject: pass ctypedef struct PyTypeObject: diff --git a/ident.pyx b/ident.pyx index d19c3a4..e7c05d2 100644 --- a/ident.pyx +++ b/ident.pyx @@ -157,7 +157,7 @@ cdef class SelIdentify: def bogus(me): return _maybecall(me._bogus, ()) -cdef void _identfunc(ident_reply *i, void *arg): +cdef void _identfunc2(ident_reply *i, void *arg): cdef SelIdentify id id = arg id._dead() @@ -167,5 +167,9 @@ cdef void _identfunc(ident_reply *i, void *arg): id.error(i.u.error) elif i.type == IDENT_USERID: id.user(i.u.userid.os, i.u.userid.user) +cdef void _identfunc(ident_reply *i, void *arg): + PyEval_AcquireLock() + _identfunc2(i, arg) + PyEval_ReleaseLock() #----- That's all, folks ---------------------------------------------------- diff --git a/sel-base.pyx b/sel-base.pyx index e7b4fbe..6a5e2c3 100644 --- a/sel-base.pyx +++ b/sel-base.pyx @@ -28,7 +28,11 @@ cdef sel_state _sel def select(): - if sel_select(&_sel) and errno != EINTR and errno != EAGAIN: + cdef int rc + PyEval_ReleaseLock() + rc = sel_select(&_sel) + PyEval_AcquireLock() + if rc and errno != EINTR and errno != EAGAIN: _oserror() sel_init(&_sel) diff --git a/sel-file.pyx b/sel-file.pyx index 72ff166..aba288f 100644 --- a/sel-file.pyx +++ b/sel-file.pyx @@ -88,9 +88,13 @@ cdef class SelFile: def ready(me): return _maybecall(me._ready, ()) -cdef void _filefunc(int fd, unsigned mode, void *arg): +cdef void _filefunc2(void *arg): cdef SelFile sf sf = arg sf.ready() +cdef void _filefunc(int fd, unsigned mode, void *arg): + PyEval_AcquireLock() + _filefunc2(arg) + PyEval_ReleaseLock() #----- That's all, folks ---------------------------------------------------- diff --git a/sel-timer.pyx b/sel-timer.pyx index 261ecde..db61a6c 100644 --- a/sel-timer.pyx +++ b/sel-timer.pyx @@ -72,10 +72,14 @@ cdef class SelTimer: def timer(me, now): return _maybecall(me._timer, ()) -cdef void _timerfunc(timeval *now, void *arg): +cdef void _timerfunc2(timeval *now, void *arg): cdef SelTimer st st = arg st._dead() st.timer(_tvtofloat(now)) +cdef void _timerfunc(timeval *now, void *arg): + PyEval_AcquireLock() + _timerfunc2(now, arg) + PyEval_ReleaseLock() #----- That's all, folks ---------------------------------------------------- diff --git a/selbuf.pyx b/selbuf.pyx index 4a5a288..b8fae43 100644 --- a/selbuf.pyx +++ b/selbuf.pyx @@ -95,12 +95,16 @@ cdef class SelLineBuffer: def eof(me): return _maybecall(me._eof, ()) -cdef void _selbfunc(char *s, size_t n, void *arg): +cdef void _selbfunc2(char *s, size_t n, void *arg): cdef SelLineBuffer sb sb = arg if s is NULL: sb.eof() else: sb.line(PyString_FromStringAndSize(s, n)) +cdef void _selbfunc(char *s, size_t n, void *arg): + PyEval_AcquireLock() + _selbfunc2(s, n, arg) + PyEval_ReleaseLock() #----- That's all, folks ---------------------------------------------------- diff --git a/selpk.pyx b/selpk.pyx index fe85a9f..5d7e9cb 100644 --- a/selpk.pyx +++ b/selpk.pyx @@ -84,8 +84,8 @@ cdef class SelPacketBuffer: def eof(me): return _maybecall(me._eof, ()) -cdef void _selpkfunc(unsigned char *p, size_t n, pkbuf *pk, - size_t *keep, void *arg): +cdef void _selpkfunc2(unsigned char *p, size_t n, pkbuf *pk, + size_t *keep, void *arg): cdef SelPacketBuffer pb cdef void *rp cdef int rn @@ -101,5 +101,10 @@ cdef void _selpkfunc(unsigned char *p, size_t n, pkbuf *pk, if rn: memcpy(p + n - rn, rp, rn) keep[0] = rn +cdef void _selpkfunc(unsigned char *p, size_t n, pkbuf *pk, + size_t *keep, void *arg): + PyEval_AcquireLock() + _selpkfunc2(p, n, pk, keep, arg) + PyEval_ReleaseLock() #----- That's all, folks ---------------------------------------------------- diff --git a/sig.pyx b/sig.pyx index 17237f3..937f29b 100644 --- a/sig.pyx +++ b/sig.pyx @@ -76,10 +76,14 @@ cdef class SelSignal: def signalled(me): return _maybecall(me._signalled, ()) -cdef void _sigfunc(int sig, void *arg): +cdef void _sigfunc2(void *arg): cdef SelSignal s s = arg s.signalled() +cdef void _sigfunc(int sig, void *arg): + PyEval_AcquireLock() + _sigfunc2(arg) + PyEval_ReleaseLock() sig_init(&_sel) -- [mdw]