diff --git a/psycopg/config.h b/psycopg/config.h index a2501096..be5e39d3 100644 --- a/psycopg/config.h +++ b/psycopg/config.h @@ -66,8 +66,8 @@ static void Dprintf(const char *fmt, ...) {} #include #define pthread_mutex_t HANDLE #define pthread_condvar_t HANDLE -#define pthread_mutex_lock(object) WaitForSingleObject(object, INFINITE) -#define pthread_mutex_unlock(object) ReleaseMutex(object) +#define pthread_mutex_lock(object) WaitForSingleObject(*(object), INFINITE) +#define pthread_mutex_unlock(object) ReleaseMutex(*(object)) #define pthread_mutex_destroy(ref) (CloseHandle(*(ref))) /* convert pthread mutex to native mutex */ static int pthread_mutex_init(pthread_mutex_t *mutex, void* fake) diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index bcbae3fc..0da50cf5 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -308,15 +308,18 @@ _psyco_curs_execute(cursorObject *self, int res = 0; PyObject *fquery, *cvt = NULL; + Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&(self->conn->lock)); if (self->conn->async_cursor != NULL && self->conn->async_cursor != (PyObject*)self) { pthread_mutex_unlock(&(self->conn->lock)); + Py_BLOCK_THREADS; psyco_set_error(ProgrammingError, (PyObject*)self, "asynchronous query already in execution", NULL, NULL); return 0; } pthread_mutex_unlock(&(self->conn->lock)); + Py_END_ALLOW_THREADS; operation = _psyco_curs_validate_sql_basic(self, operation); @@ -641,15 +644,18 @@ _psyco_curs_prefetch(cursorObject *self) /* check if the fetching cursor is the one that did the asynchronous query and raise an exception if not */ + Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&(self->conn->lock)); if (self->conn->async_cursor != NULL && self->conn->async_cursor != (PyObject*)self) { pthread_mutex_unlock(&(self->conn->lock)); + Py_BLOCK_THREADS; psyco_set_error(ProgrammingError, (PyObject*)self, "asynchronous fetch by wrong cursor", NULL, NULL); return -2; } pthread_mutex_unlock(&(self->conn->lock)); + Py_END_ALLOW_THREADS; if (self->pgres == NULL || self->needsfetch) { self->needsfetch = 0; @@ -1382,12 +1388,12 @@ psyco_curs_fileno(cursorObject *self, PyObject *args) /* note how we call PQflush() to make sure the user will use select() in the safe way! */ - pthread_mutex_lock(&(self->conn->lock)); Py_BEGIN_ALLOW_THREADS; + pthread_mutex_lock(&(self->conn->lock)); PQflush(self->conn->pgconn); socket = (long int)PQsocket(self->conn->pgconn); - Py_END_ALLOW_THREADS; pthread_mutex_unlock(&(self->conn->lock)); + Py_END_ALLOW_THREADS; return PyInt_FromLong(socket); } @@ -1413,10 +1419,12 @@ psyco_curs_isready(cursorObject *self, PyObject *args) } else { IFCLEARPGRES(self->pgres); + Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&(self->conn->lock)); self->pgres = PQgetResult(self->conn->pgconn); self->conn->async_cursor = NULL; pthread_mutex_unlock(&(self->conn->lock)); + Py_END_ALLOW_THREADS; self->needsfetch = 1; Py_INCREF(Py_True); return Py_True; diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 46fde60b..ae7d65c6 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -564,11 +564,12 @@ _pq_fetch_tuples(cursorObject *curs) int fsize = PQfsize(curs->pgres, i); int fmod = PQfmod(curs->pgres, i); - Py_BLOCK_THREADS; PyObject *dtitem = PyTuple_New(7); PyObject *type = PyInt_FromLong(ftype); PyObject *cast = NULL; + Py_BLOCK_THREADS; + PyTuple_SET_ITEM(curs->description, i, dtitem); /* fill the right cast function by accessing three different dictionaries: diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 43b9ea38..2c4be8ce 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -593,6 +593,7 @@ psyco_GetDecimalType(void) static PyObject *cachedType = NULL; #ifdef HAVE_DECIMAL + PyObject *decimal = PyImport_ImportModule("decimal"); /* Use the cached object if running from the main interpreter. */ int can_cache = psyco_is_main_interp(); @@ -602,7 +603,6 @@ psyco_GetDecimalType(void) } /* Get a new reference to the Decimal type. */ - PyObject *decimal = PyImport_ImportModule("decimal"); if (decimal) { decimalType = PyObject_GetAttrString(decimal, "Decimal"); Py_DECREF(decimal);