From 2a8fa4bef7c71b1bec99ffdeaa7501d97ab11a57 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 7 Mar 2019 22:51:36 +0000 Subject: [PATCH] Dropped locking version of pq_is_busy() The locking version was used for sync connections, the non-locking one for green ones. However it only calls non-blocking functions, so it doesn't really matter releasing the gil. So have only the non-locking one. Note that the name are sort of swapped: pq_is_busy() does now what pq_is_busy_locked() used to do. --- psycopg/connection_int.c | 11 +-------- psycopg/pqpath.c | 51 ++++------------------------------------ psycopg/pqpath.h | 1 - 3 files changed, 6 insertions(+), 57 deletions(-) diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index aea85b9c..7b563c5c 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -936,16 +936,7 @@ _conn_poll_query(connectionObject *self) case ASYNC_READ: Dprintf("conn_poll: async_status = ASYNC_READ"); - if (self->async) { - res = _conn_poll_advance_read(self, pq_is_busy(self)); - } - else { - /* we are a green connection being polled as result of a query. - this means that our caller has the lock and we are being called - from the callback. If we tried to acquire the lock now it would - be a deadlock. */ - res = _conn_poll_advance_read(self, pq_is_busy_locked(self)); - } + res = _conn_poll_advance_read(self, pq_is_busy(self)); break; case ASYNC_DONE: diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 4de0b065..e95893e0 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -770,57 +770,16 @@ exit: means that there is data available to be collected. -1 means an error, the exception will be set accordingly. - this function locks the connection object - this function call Py_*_ALLOW_THREADS macros */ - -int -pq_is_busy(connectionObject *conn) -{ - int res; - Dprintf("pq_is_busy: consuming input"); - - Py_BEGIN_ALLOW_THREADS; - pthread_mutex_lock(&(conn->lock)); - - if (PQconsumeInput(conn->pgconn) == 0) { - Dprintf("pq_is_busy: PQconsumeInput() failed"); - pthread_mutex_unlock(&(conn->lock)); - Py_BLOCK_THREADS; - - /* if the libpq says pgconn is lost, close the py conn */ - if (CONNECTION_BAD == PQstatus(conn->pgconn)) { - conn->closed = 2; - } - - PyErr_SetString(OperationalError, PQerrorMessage(conn->pgconn)); - return -1; - } - - res = PQisBusy(conn->pgconn); - - Py_BLOCK_THREADS; - conn_notifies_process(conn); - conn_notice_process(conn); - Py_UNBLOCK_THREADS; - - pthread_mutex_unlock(&(conn->lock)); - Py_END_ALLOW_THREADS; - - return res; -} - -/* pq_is_busy_locked - equivalent to pq_is_busy but we already have the lock - * * The function should be called with the lock and holding the GIL. */ int -pq_is_busy_locked(connectionObject *conn) +pq_is_busy(connectionObject *conn) { - Dprintf("pq_is_busy_locked: consuming input"); + Dprintf("pq_is_busy: calling PQconsumeInput()"); if (PQconsumeInput(conn->pgconn) == 0) { - Dprintf("pq_is_busy_locked: PQconsumeInput() failed"); + Dprintf("pq_is_busy: PQconsumeInput() failed"); /* if the libpq says pgconn is lost, close the py conn */ if (CONNECTION_BAD == PQstatus(conn->pgconn)) { @@ -831,8 +790,8 @@ pq_is_busy_locked(connectionObject *conn) return -1; } - /* notices and notifies will be processed at the end of the loop we are in - * (async reading) by pq_fetch. */ + conn_notifies_process(conn); + conn_notice_process(conn); return PQisBusy(conn->pgconn); } diff --git a/psycopg/pqpath.h b/psycopg/pqpath.h index 81120a00..58a1c59b 100644 --- a/psycopg/pqpath.h +++ b/psycopg/pqpath.h @@ -60,7 +60,6 @@ HIDDEN int pq_tpc_command_locked(connectionObject *conn, PGresult **pgres, char **error, PyThreadState **tstate); HIDDEN int pq_is_busy(connectionObject *conn); -HIDDEN int pq_is_busy_locked(connectionObject *conn); HIDDEN int pq_flush(connectionObject *conn); HIDDEN void pq_clear_async(connectionObject *conn); RAISES_NEG HIDDEN int pq_set_non_blocking(connectionObject *conn, int arg);