|
1 |
| -/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.19 2003/11/29 19:52:08 pgsql Exp $ */ |
| 1 | +/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.20 2004/03/14 12:16:29 meskes Exp $ */ |
2 | 2 |
|
3 | 3 | #define POSTGRES_ECPG_INTERNAL
|
4 | 4 | #include "postgres_fe.h"
|
|
14 | 14 |
|
15 | 15 | #ifdef ENABLE_THREAD_SAFETY
|
16 | 16 | static pthread_mutex_t connections_mutex = PTHREAD_MUTEX_INITIALIZER;
|
| 17 | +static pthread_key_t actual_connection_key; |
| 18 | +static pthread_once_t actual_connection_key_once = PTHREAD_ONCE_INIT; |
| 19 | +#else |
| 20 | +static struct connection *actual_connection = NULL; |
17 | 21 | #endif
|
18 | 22 | static struct connection *all_connections = NULL;
|
19 |
| -static struct connection *actual_connection = NULL; |
| 23 | + |
| 24 | +#ifdef ENABLE_THREAD_SAFETY |
| 25 | +static void |
| 26 | +ecpg_actual_connection_init(void) |
| 27 | +{ |
| 28 | + pthread_key_create(&actual_connection_key, NULL); |
| 29 | +} |
| 30 | +#endif |
20 | 31 |
|
21 | 32 | static struct connection *
|
22 | 33 | ecpg_get_connection_nr(const char *connection_name)
|
23 | 34 | {
|
24 | 35 | struct connection *ret = NULL;
|
25 | 36 |
|
26 | 37 | if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0))
|
| 38 | + { |
| 39 | +#ifdef ENABLE_THREAD_SAFETY |
| 40 | + ret = pthread_getspecific(actual_connection_key); |
| 41 | +#else |
27 | 42 | ret = actual_connection;
|
| 43 | +#endif |
| 44 | + } |
28 | 45 | else
|
29 | 46 | {
|
30 | 47 | struct connection *con;
|
@@ -86,8 +103,13 @@ ecpg_finish(struct connection * act)
|
86 | 103 | con->next = act->next;
|
87 | 104 | }
|
88 | 105 |
|
| 106 | +#ifdef ENABLE_THREAD_SAFETY |
| 107 | + if( pthread_getspecific(actual_connection_key) == act ) |
| 108 | + pthread_setspecific(actual_connection_key, all_connections); |
| 109 | +#else |
89 | 110 | if (actual_connection == act)
|
90 | 111 | actual_connection = all_connections;
|
| 112 | +#endif |
91 | 113 |
|
92 | 114 | ECPGlog("ecpg_finish: Connection %s closed.\n", act->name);
|
93 | 115 |
|
@@ -150,7 +172,11 @@ ECPGsetconn(int lineno, const char *connection_name)
|
150 | 172 | if (!ECPGinit(con, connection_name, lineno))
|
151 | 173 | return (false);
|
152 | 174 |
|
| 175 | +#ifdef ENABLE_THREAD_SAFETY |
| 176 | + pthread_setspecific(actual_connection_key, con); |
| 177 | +#else |
153 | 178 | actual_connection = con;
|
| 179 | +#endif |
154 | 180 | return true;
|
155 | 181 | }
|
156 | 182 |
|
@@ -370,7 +396,13 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
|
370 | 396 | else
|
371 | 397 | this->next = all_connections;
|
372 | 398 |
|
373 |
| - actual_connection = all_connections = this; |
| 399 | + all_connections = this; |
| 400 | +#ifdef ENABLE_THREAD_SAFETY |
| 401 | + pthread_once(&actual_connection_key_once, ecpg_actual_connection_init); |
| 402 | + pthread_setspecific(actual_connection_key, all_connections); |
| 403 | +#else |
| 404 | + actual_connection = all_connections; |
| 405 | +#endif |
374 | 406 |
|
375 | 407 | ECPGlog("ECPGconnect: opening database %s on %s port %s %s%s%s%s\n",
|
376 | 408 | realname ? realname : "<DEFAULT>",
|
|
0 commit comments