summaryrefslogtreecommitdiff
path: root/pool.c
diff options
context:
space:
mode:
Diffstat (limited to 'pool.c')
-rw-r--r--pool.c46
1 files changed, 32 insertions, 14 deletions
diff --git a/pool.c b/pool.c
index 11a0f1f..82b0644 100644
--- a/pool.c
+++ b/pool.c
@@ -22,8 +22,9 @@
#include <stdlib.h> /* for malloc */
#include <syslog.h> /* for syslog */
-#define AVAILABLE -1
-#define ERROR -2
+#define USED 1
+#define AVAILABLE 0
+#define ERROR 3
int psql_pool_init( PgConnPool *pool, const char *conninfo, const size_t max_connections )
{
@@ -35,16 +36,24 @@ int psql_pool_init( PgConnPool *pool, const char *conninfo, const size_t max_con
return -ENOMEM;
}
- pool->avail = (pthread_t *)malloc( sizeof( pthread_t ) * max_connections );
+ pool->avail = (unsigned int *)malloc( sizeof( unsigned int ) * max_connections );
if( pool->avail == NULL ) {
free( pool->conns );
return -ENOMEM;
}
+
+ pool->thread = (pthread_t *)malloc( sizeof( pthread_t ) * max_connections );
+ if( pool->thread == NULL ) {
+ free( pool->avail );
+ free( pool->conns );
+ return -ENOMEM;
+ }
pool->size = max_connections;
res = pthread_mutex_init( &pool->lock, NULL );
if( res < 0 ) {
+ free( pool->thread );
free( pool->avail );
free( pool->conns );
return res;
@@ -53,6 +62,7 @@ int psql_pool_init( PgConnPool *pool, const char *conninfo, const size_t max_con
res = pthread_cond_init( &pool->cond, NULL );
if( res < 0 ) {
(void)pthread_mutex_destroy( &pool->lock );
+ free( pool->thread );
free( pool->avail );
free( pool->conns );
return res;
@@ -80,17 +90,24 @@ int psql_pool_destroy( PgConnPool *pool )
int res2;
for( i = 0; i < pool->size; i++ ) {
- if( pool->avail[i] == AVAILABLE ) {
- PQfinish( pool->conns[i] );
- } else if( pool->avail[i] > 0 ) {
- syslog( LOG_ERR, "Destroying pool connection to thread '%u' which is still in use",
- (unsigned int)pool->avail[i] );
- PQfinish( pool->conns[i] );
+ switch( pool->avail[i] ) {
+ case AVAILABLE:
+ PQfinish( pool->conns[i] );
+ break;
+ case USED:
+ syslog( LOG_ERR, "Destroying pool connection to thread %p which is still in use",
+ pool->thread[i] );
+ PQfinish( pool->conns[i] );
+ break;
+ case ERROR:
+ // ignoring threads in error, asusming PQfinish will fail anyway
+ break;
}
}
free( pool->conns );
free( pool->avail );
+ free( pool->thread );
res1 = pthread_cond_destroy( &pool->cond );
res2 = pthread_mutex_destroy( &pool->lock );
@@ -106,8 +123,8 @@ PGconn *psql_pool_acquire( PgConnPool *pool )
for( ;; ) {
res = pthread_mutex_lock( &pool->lock );
if( res < 0 ) {
- syslog( LOG_ERR, "Locking mutex failed for thread '%u': %d",
- (unsigned int)pthread_self( ), res );
+ syslog( LOG_ERR, "Locking mutex failed for thread %p: %d",
+ pthread_self( ), res );
return NULL;
}
@@ -115,7 +132,8 @@ PGconn *psql_pool_acquire( PgConnPool *pool )
for( i = 0; i < pool->size; i++ ) {
if( pool->avail[i] == AVAILABLE ) {
if( PQstatus( pool->conns[i] ) == CONNECTION_OK ) {
- pool->avail[i] = pthread_self( );
+ pool->avail[i] = USED;
+ pool->thread[i] = pthread_self( );
(void)pthread_mutex_unlock( &pool->lock );
return pool->conns[i];
} else {
@@ -127,8 +145,8 @@ PGconn *psql_pool_acquire( PgConnPool *pool )
/* wait on conditional till a free connection is signalled */
res = pthread_cond_wait( &pool->cond, &pool->lock );
if( res < 0 ) {
- syslog( LOG_ERR, "Error waiting for free condition in thread '%u': %d",
- (unsigned int)pthread_self( ), res );
+ syslog( LOG_ERR, "Error waiting for free condition in thread %p: %d",
+ pthread_self( ), res );
(void)pthread_mutex_unlock( &pool->lock );
return NULL;
}