Skip to content

Commit 9dc2e6d

Browse files
author
Michael Meskes
committed
Added patch by Philip Yarra <philip.yarra@internode.on.net> for a bug in thread support.
1 parent 7c13781 commit 9dc2e6d

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/interfaces/ecpg/ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1918,6 +1918,11 @@ Fri Mar 18 10:54:47 CET 2005
19181918

19191919
- Added patch by Christof Petig <christof@petig-baender.de> to work
19201920
around gcc bug on powerpc and amd64.
1921+
1922+
Thu Apr 14 11:59:47 CEST 2005
1923+
1924+
- Added patch by Philip Yarra <philip.yarra@internode.on.net> for a
1925+
bug in thread support.
19211926
- Set ecpg library version to 5.1.
19221927
- Set ecpg version to 4.1.1.
19231928

src/interfaces/ecpg/ecpglib/connect.c

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.24 2004/12/30 09:36:37 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.25 2005/04/14 10:08:57 meskes Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -17,9 +17,8 @@ static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
1717
static pthread_key_t actual_connection_key;
1818
static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT;
1919

20-
#else
21-
static struct connection *actual_connection = NULL;
2220
#endif
21+
static struct connection *actual_connection = NULL;
2322
static struct connection *all_connections = NULL;
2423

2524
#ifdef ENABLE_THREAD_SAFETY
@@ -39,6 +38,16 @@ ecpg_get_connection_nr(const char *connection_name)
3938
{
4039
#ifdef ENABLE_THREAD_SAFETY
4140
ret = pthread_getspecific(actual_connection_key);
41+
/* if no connection in TSD for this thread, get the global default connection
42+
* and hope the user knows what they're doing (i.e. using their own mutex to
43+
* protect that connection from concurrent accesses */
44+
if(NULL == ret)
45+
{
46+
ECPGlog("no TSD connection, going for global\n");
47+
ret = actual_connection;
48+
}
49+
else
50+
ECPGlog("got the TSD connection\n");
4251
#else
4352
ret = actual_connection;
4453
#endif
@@ -67,6 +76,16 @@ ECPGget_connection(const char *connection_name)
6776
{
6877
#ifdef ENABLE_THREAD_SAFETY
6978
ret = pthread_getspecific(actual_connection_key);
79+
/* if no connection in TSD for this thread, get the global default connection
80+
* and hope the user knows what they're doing (i.e. using their own mutex to
81+
* protect that connection from concurrent accesses */
82+
if(NULL == ret)
83+
{
84+
ECPGlog("no TSD connection here either, using global\n");
85+
ret = actual_connection;
86+
}
87+
else
88+
ECPGlog("got TSD connection\n");
7089
#else
7190
ret = actual_connection;
7291
#endif
@@ -117,10 +136,9 @@ ecpg_finish(struct connection * act)
117136
#ifdef ENABLE_THREAD_SAFETY
118137
if (pthread_getspecific(actual_connection_key) == act)
119138
pthread_setspecific(actual_connection_key, all_connections);
120-
#else
139+
#endif
121140
if (actual_connection == act)
122141
actual_connection = all_connections;
123-
#endif
124142

125143
ECPGlog("ecpg_finish: Connection %s closed.\n", act->name);
126144

@@ -416,9 +434,8 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
416434
#ifdef ENABLE_THREAD_SAFETY
417435
pthread_once(&actual_connection_key_once, ecpg_actual_connection_init);
418436
pthread_setspecific(actual_connection_key, all_connections);
419-
#else
420-
actual_connection = all_connections;
421437
#endif
438+
actual_connection = all_connections;
422439

423440
ECPGlog("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n",
424441
realname ? realname : "<DEFAULT>",

0 commit comments

Comments
 (0)