mirror of
https://github.com/postgres/postgres.git
synced 2025-07-02 09:02:37 +03:00
Make ecpg thread safe.
Lee Kindness
This commit is contained in:
@ -1,20 +1,24 @@
|
||||
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.2 2003/03/21 15:31:04 meskes Exp $ */
|
||||
/* $Header: /cvsroot/pgsql/src/interfaces/ecpg/ecpglib/misc.c,v 1.3 2003/06/15 04:07:58 momjian Exp $ */
|
||||
|
||||
#define POSTGRES_ECPG_INTERNAL
|
||||
#include "postgres_fe.h"
|
||||
|
||||
#include <unistd.h>
|
||||
#ifdef USE_THREADS
|
||||
#include <pthread.h>
|
||||
#endif
|
||||
#include "ecpgtype.h"
|
||||
#include "ecpglib.h"
|
||||
#include "ecpgerrno.h"
|
||||
#include "extern.h"
|
||||
#include "sqlca.h"
|
||||
|
||||
static struct sqlca sqlca_init =
|
||||
static struct sqlca_t sqlca_init =
|
||||
{
|
||||
{
|
||||
'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '
|
||||
},
|
||||
sizeof(struct sqlca),
|
||||
sizeof(struct sqlca_t),
|
||||
0,
|
||||
{
|
||||
0,
|
||||
@ -36,19 +40,54 @@ static struct sqlca sqlca_init =
|
||||
}
|
||||
};
|
||||
|
||||
static int simple_debug = 0;
|
||||
#ifdef USE_THREADS
|
||||
static pthread_key_t sqlca_key;
|
||||
static pthread_once_t sqlca_key_once = PTHREAD_ONCE_INIT;
|
||||
#else
|
||||
static struct sqlca_t sqlca =
|
||||
{
|
||||
{
|
||||
'S', 'Q', 'L', 'C', 'A', ' ', ' ', ' '
|
||||
},
|
||||
sizeof(struct sqlca_t),
|
||||
0,
|
||||
{
|
||||
0,
|
||||
{
|
||||
0
|
||||
}
|
||||
},
|
||||
{
|
||||
'N', 'O', 'T', ' ', 'S', 'E', 'T', ' '
|
||||
},
|
||||
{
|
||||
0, 0, 0, 0, 0, 0
|
||||
},
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
},
|
||||
{
|
||||
0, 0, 0, 0, 0, 0, 0, 0
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
#ifdef USE_THREADS
|
||||
static pthread_mutex_t debug_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#endif
|
||||
static int simple_debug = 0;
|
||||
static FILE *debugstream = NULL;
|
||||
|
||||
void
|
||||
ECPGinit_sqlca(void)
|
||||
void ECPGinit_sqlca(struct sqlca_t *sqlca)
|
||||
{
|
||||
memcpy((char *) &sqlca, (char *) &sqlca_init, sizeof(sqlca));
|
||||
memcpy((char *)sqlca, (char *)&sqlca_init, sizeof(struct sqlca_t));
|
||||
}
|
||||
|
||||
bool
|
||||
ECPGinit(const struct connection * con, const char *connection_name, const int lineno)
|
||||
{
|
||||
ECPGinit_sqlca();
|
||||
struct sqlca_t *sqlca = ECPGget_sqlca();
|
||||
ECPGinit_sqlca(sqlca);
|
||||
if (con == NULL)
|
||||
{
|
||||
ECPGraise(lineno, ECPG_NO_CONN, connection_name ? connection_name : "NULL");
|
||||
@ -58,6 +97,33 @@ ECPGinit(const struct connection * con, const char *connection_name, const int l
|
||||
return (true);
|
||||
}
|
||||
|
||||
#ifdef USE_THREADS
|
||||
static void ecpg_sqlca_key_init(void)
|
||||
{
|
||||
pthread_key_create(&sqlca_key, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
struct sqlca_t *ECPGget_sqlca(void)
|
||||
{
|
||||
#ifdef USE_THREADS
|
||||
struct sqlca_t *sqlca;
|
||||
|
||||
pthread_once(&sqlca_key_once, ecpg_sqlca_key_init);
|
||||
|
||||
sqlca = pthread_getspecific(&sqlca_key);
|
||||
if( sqlca == NULL )
|
||||
{
|
||||
sqlca = malloc(sizeof(struct sqlca_t));
|
||||
ECPGinit_sqlca(sqlca);
|
||||
pthread_setspecific(&sqlca_key, sqlca);
|
||||
}
|
||||
return( sqlca );
|
||||
#else
|
||||
return( &sqlca );
|
||||
#endif
|
||||
}
|
||||
|
||||
bool
|
||||
ECPGstatus(int lineno, const char *connection_name)
|
||||
{
|
||||
@ -123,9 +189,17 @@ ECPGtrans(int lineno, const char *connection_name, const char *transaction)
|
||||
void
|
||||
ECPGdebug(int n, FILE *dbgs)
|
||||
{
|
||||
#ifdef USE_THREADS
|
||||
pthread_mutex_lock(&debug_mutex);
|
||||
#endif
|
||||
|
||||
simple_debug = n;
|
||||
debugstream = dbgs;
|
||||
ECPGlog("ECPGdebug: set to %d\n", simple_debug);
|
||||
|
||||
#ifdef USE_THREADS
|
||||
pthread_mutex_unlock(&debug_mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
void
|
||||
@ -133,12 +207,20 @@ ECPGlog(const char *format,...)
|
||||
{
|
||||
va_list ap;
|
||||
|
||||
if (simple_debug)
|
||||
{
|
||||
char *f = (char *) malloc(strlen(format) + 100);
|
||||
#ifdef USE_THREADS
|
||||
pthread_mutex_lock(&debug_mutex);
|
||||
#endif
|
||||
|
||||
if (!f)
|
||||
if( simple_debug )
|
||||
{
|
||||
char *f = (char *)malloc(strlen(format) + 100);
|
||||
if( f == NULL )
|
||||
{
|
||||
#ifdef USE_THREADS
|
||||
pthread_mutex_unlock(&debug_mutex);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
sprintf(f, "[%d]: %s", (int) getpid(), format);
|
||||
|
||||
@ -148,4 +230,8 @@ ECPGlog(const char *format,...)
|
||||
|
||||
ECPGfree(f);
|
||||
}
|
||||
|
||||
#ifdef USE_THREADS
|
||||
pthread_mutex_unlock(&debug_mutex);
|
||||
#endif
|
||||
}
|
||||
|
Reference in New Issue
Block a user