diff options
author | Andreas Baumann <mail@andreasbaumann.cc> | 2023-08-03 10:37:12 +0200 |
---|---|---|
committer | Andreas Baumann <mail@andreasbaumann.cc> | 2023-08-03 10:37:12 +0200 |
commit | d9a54c3f6cbf068a1e1553eac9adcf9127b007f5 (patch) | |
tree | e7e8cb1beda5ae9ba83a53d0cea4a29379b6e6a6 /pool.c | |
parent | 6d6365d782a713d7fde48daa70e64e64ac283001 (diff) | |
download | pgfuse-d9a54c3f6cbf068a1e1553eac9adcf9127b007f5.tar.gz pgfuse-d9a54c3f6cbf068a1e1553eac9adcf9127b007f5.tar.bz2 |
fixing for platforms, where pthread_t is not numeric (as for musl, where it is a pointer,
POSIX sadly did away with the requirement that pthread_t must be a numeric):
- printing thread ids as pointers with '%p' instead of converting them to unsigned int and
printing them with '%u'
- using separate 'avail' and 'thread' arrays in pools to record status of pooled database
connections
Diffstat (limited to 'pool.c')
-rw-r--r-- | pool.c | 46 |
1 files changed, 32 insertions, 14 deletions
@@ -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; } |