changeset 771:eac79a449c06

Making app_acct thread_safe for PGsql. NOT TESTED.
author Sebastien Decugis <sdecugis@nict.go.jp>
date Sun, 30 Oct 2011 20:57:59 +0100
parents 27fef2ca2cf6
children c6969f1af9f3
files extensions/app_acct/acct_db.c tests/testappacct.c
diffstat 2 files changed, 22 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/extensions/app_acct/acct_db.c	Sun Oct 30 12:33:28 2011 +0100
+++ b/extensions/app_acct/acct_db.c	Sun Oct 30 20:57:59 2011 +0100
@@ -35,6 +35,10 @@
 
 /* Database interface module */
 
+/* There is one connection to the db per thread. 
+The connection is stored in the pthread_key_t variable */
+
+
 #include "app_acct.h"
 #include <libpq-fe.h>
 
@@ -53,7 +57,8 @@
 #ifndef TEST_DEBUG
 static 
 #endif /* TEST_DEBUG */
-PGconn *conn = NULL;
+pthread_key_t connk;
+
 
 /* Initialize the database context: connection to the DB, prepared statement to insert new records */
 int acct_db_init(void)
@@ -65,6 +70,7 @@
 	size_t sql_offset = 0; /* The actual data already written in this buffer */
 	int idx = 0;
 	PGresult * res;
+	PGconn *conn;
 	#define REALLOC_SIZE	1024	/* We extend the buffer by this amount */
 	
 	TRACE_ENTRY();
@@ -194,6 +200,9 @@
 	free(sql);
 	acct_rec_empty(&emptyrecords);
 	
+	CHECK_POSIX( pthread_key_create(&connk, (void (*)(void*))PQfinish) );
+	CHECK_POSIX( pthread_setspecific(connk, conn) );
+	
 	/* Ok, ready */
 	return 0;
 }
@@ -201,11 +210,7 @@
 /* Terminate the connection to the DB */
 void acct_db_free(void)
 {	
-	if (conn) {
-		/* Note: the prepared statement is automatically freed when the session terminates */
-		PQfinish(conn);
-		conn = NULL;
-	}
+	CHECK_POSIX_DO(pthread_key_delete(connk) , );
 }
 
 /* When a new message has been received, insert the content of the parsed mapping into the DB (using prepared statement) */
@@ -218,9 +223,16 @@
 	int	  size = 0;
 	PGresult *res;
 	struct fd_list *li;
+	PGconn *conn;
 	
 	TRACE_ENTRY("%p", records);
-	CHECK_PARAMS( conn && records );
+	CHECK_PARAMS( records );
+	
+	conn = pthread_getspecific(connk);
+	if (!conn) {
+		conn = PQconnectdb(acct_config->conninfo);
+		CHECK_POSIX( pthread_setspecific(connk, conn) );
+	}
 	
 	/* First, check if the connection with the DB has not staled, and eventually try to fix it */
 	if (PQstatus(conn) != CONNECTION_OK) {
--- a/tests/testappacct.c	Sun Oct 30 12:33:28 2011 +0100
+++ b/tests/testappacct.c	Sun Oct 30 20:57:59 2011 +0100
@@ -90,7 +90,8 @@
 /* Main test routine */
 int main(int argc, char *argv[])
 {
-	extern PGconn *conn; /* in acct_db.c */
+	extern pthread_key_t connk; /* in acct_db.c */
+	PGconn *conn;
 	extern int fd_ext_init(int major, int minor, char * conffile); /* defined in include's extension.h */
 	extern void fd_ext_fini(void); /* defined in the extension itself */
 	struct msg * msg;
@@ -121,6 +122,7 @@
 		
 		/* Now, call the one of the extension */
 		CHECK( 0, fd_ext_init(FD_PROJECT_VERSION_MAJOR, FD_PROJECT_VERSION_MINOR,NULL) );
+		conn = pthread_getspecific(connk);
 	}
 	
 	/* Drop and recreate the table for the test */
"Welcome to our mercurial repository"