Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8526f53

Browse files
committed
The "cvs add" of test_thread_implicit.pgc seems to have been missed,
i've attached this again. Additionally I include a small patch to remove mutex locking when a DEFAULT/NULL connection is being retrieved. This is consistent with libpq. Lee Kindness
1 parent bda6e04 commit 8526f53

File tree

2 files changed

+155
-5
lines changed

2 files changed

+155
-5
lines changed

src/interfaces/ecpg/ecpglib/connect.c

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.20 2004/03/14 12:16:29 meskes Exp $ */
1+
/* $PostgreSQL: pgsql/src/interfaces/ecpg/ecpglib/connect.c,v 1.21 2004/03/15 16:27:43 momjian Exp $ */
22

33
#define POSTGRES_ECPG_INTERNAL
44
#include "postgres_fe.h"
@@ -62,18 +62,28 @@ ECPGget_connection(const char *connection_name)
6262
{
6363
struct connection *ret = NULL;
6464

65+
if ((connection_name == NULL) || (strcmp(connection_name, "CURRENT") == 0))
66+
{
6567
#ifdef ENABLE_THREAD_SAFETY
66-
pthread_mutex_lock(&connections_mutex);
68+
ret = pthread_getspecific(actual_connection_key);
69+
#else
70+
ret = actual_connection;
71+
#endif
72+
}
73+
else
74+
{
75+
#ifdef ENABLE_THREAD_SAFETY
76+
pthread_mutex_lock(&connections_mutex);
6777
#endif
6878

69-
ret = ecpg_get_connection_nr(connection_name);
79+
ret = ecpg_get_connection_nr(connection_name);
7080

7181
#ifdef ENABLE_THREAD_SAFETY
72-
pthread_mutex_unlock(&connections_mutex);
82+
pthread_mutex_unlock(&connections_mutex);
7383
#endif
84+
}
7485

7586
return (ret);
76-
7787
}
7888

7989
static void
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Thread test program
3+
* by Lee Kindness.
4+
*/
5+
6+
/* #define ECPGDEBUG */
7+
8+
#include <pthread.h>
9+
#include <stdlib.h>
10+
11+
void *test_thread(void *arg);
12+
13+
EXEC SQL BEGIN DECLARE SECTION;
14+
char *l_dbname;
15+
EXEC SQL END DECLARE SECTION;
16+
int nthreads = 2;
17+
int iterations = 10;
18+
19+
int main(int argc, char *argv[])
20+
{
21+
#ifdef ECPGDEBUG
22+
char debugfilename[] = "thread_test_implicit.log";
23+
FILE *debugfile;
24+
#endif
25+
pthread_t *threads;
26+
int n;
27+
EXEC SQL BEGIN DECLARE SECTION;
28+
int l_rows;
29+
EXEC SQL END DECLARE SECTION;
30+
31+
/* parse command line arguments */
32+
if( (argc < 2) || (argc > 4) )
33+
{
34+
fprintf(stderr, "Usage: %s dbname [threads] [iterations_per_thread]\n", argv[0]);
35+
return( 1 );
36+
}
37+
l_dbname = argv[1];
38+
if( argc >= 3 )
39+
nthreads = atoi(argv[2]);
40+
if( argc == 4 )
41+
iterations = atoi(argv[3]);
42+
43+
/* open ECPG debug log? */
44+
#ifdef ECPGDEBUG
45+
debugfile = fopen(debugfilename, "w");
46+
if( debugfile != NULL )
47+
ECPGdebug(1, debugfile);
48+
else
49+
fprintf(stderr, "Cannot open ECPG debug log: %s\n", debugfilename);
50+
#endif
51+
52+
/* setup test_thread table */
53+
EXEC SQL CONNECT TO:l_dbname;
54+
EXEC SQL DROP TABLE test_thread; /* DROP might fail */
55+
EXEC SQL COMMIT;
56+
EXEC SQL CREATE TABLE
57+
test_thread(tstamp TIMESTAMP NOT NULL DEFAULT CAST(timeofday() AS TIMESTAMP),
58+
thread TEXT NOT NULL,
59+
iteration INTEGER NOT NULL,
60+
PRIMARY KEY(thread, iteration));
61+
EXEC SQL COMMIT;
62+
EXEC SQL DISCONNECT;
63+
64+
/* create, and start, threads */
65+
threads = calloc(nthreads, sizeof(pthread_t));
66+
if( threads == NULL )
67+
{
68+
fprintf(stderr, "Cannot alloc memory\n");
69+
return( 1 );
70+
}
71+
for( n = 0; n < nthreads; n++ )
72+
{
73+
pthread_create(&threads[n], NULL, test_thread, (void *)n + 1);
74+
}
75+
76+
/* wait for thread completion */
77+
for( n = 0; n < nthreads; n++ )
78+
{
79+
pthread_join(threads[n], NULL);
80+
}
81+
free(threads);
82+
83+
/* and check results */
84+
EXEC SQL CONNECT TO :l_dbname;
85+
EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
86+
EXEC SQL COMMIT;
87+
EXEC SQL DISCONNECT;
88+
if( l_rows == (nthreads * iterations) )
89+
printf("\nSuccess.\n");
90+
else
91+
printf("\nERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
92+
93+
/* close ECPG debug log? */
94+
#ifdef ECPGDEBUG
95+
if( debugfile != NULL )
96+
{
97+
ECPGdebug(0, debugfile);
98+
fclose(debugfile);
99+
}
100+
#endif
101+
102+
return( 0 );
103+
}
104+
105+
void *test_thread(void *arg)
106+
{
107+
long threadnum = (long)arg;
108+
EXEC SQL BEGIN DECLARE SECTION;
109+
int l_i;
110+
char l_connection[128];
111+
EXEC SQL END DECLARE SECTION;
112+
113+
/* build up connection name, and connect to database */
114+
snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
115+
EXEC SQL WHENEVER sqlerror sqlprint;
116+
EXEC SQL CONNECT TO :l_dbname AS :l_connection;
117+
if( sqlca.sqlcode != 0 )
118+
{
119+
printf("%s: ERROR: cannot connect to database!\n", l_connection);
120+
return( NULL );
121+
}
122+
EXEC SQL BEGIN;
123+
124+
/* insert into test_thread table */
125+
for( l_i = 1; l_i <= iterations; l_i++ )
126+
{
127+
printf("%s: inserting %d\n", l_connection, l_i);
128+
EXEC SQL INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
129+
if( sqlca.sqlcode == 0 )
130+
printf("%s: insert done\n", l_connection);
131+
else
132+
printf("%s: ERROR: insert failed!\n", l_connection);
133+
}
134+
135+
/* all done */
136+
EXEC SQL COMMIT;
137+
EXEC SQL DISCONNECT :l_connection;
138+
printf("%s: done!\n", l_connection);
139+
return( NULL );
140+
}

0 commit comments

Comments
 (0)