6
/* #define ECPGDEBUG */
11
void *test_thread(void *arg);
13
EXEC SQL BEGIN DECLARE SECTION;
15
EXEC SQL END DECLARE SECTION;
19
int main(int argc, char *argv[])
22
char debugfilename[] = "thread_test_implicit.log";
27
EXEC SQL BEGIN DECLARE SECTION;
29
EXEC SQL END DECLARE SECTION;
31
/* parse command line arguments */
32
if( (argc < 2) || (argc > 4) )
34
fprintf(stderr, "Usage: %s dbname [threads] [iterations_per_thread]\n", argv[0]);
39
nthreads = atoi(argv[2]);
41
iterations = atoi(argv[3]);
43
/* open ECPG debug log? */
45
debugfile = fopen(debugfilename, "w");
46
if( debugfile != NULL )
47
ECPGdebug(1, debugfile);
49
fprintf(stderr, "Cannot open ECPG debug log: %s\n", debugfilename);
52
/* setup test_thread table */
53
EXEC SQL CONNECT TO:l_dbname;
54
EXEC SQL DROP TABLE test_thread; /* DROP might fail */
57
test_thread(tstamp TIMESTAMP NOT NULL DEFAULT CAST(timeofday() AS TIMESTAMP),
59
iteration INTEGER NOT NULL,
60
PRIMARY KEY(thread, iteration));
64
/* create, and start, threads */
65
threads = calloc(nthreads, sizeof(pthread_t));
68
fprintf(stderr, "Cannot alloc memory\n");
71
for( n = 0; n < nthreads; n++ )
73
pthread_create(&threads[n], NULL, test_thread, (void *)n + 1);
76
/* wait for thread completion */
77
for( n = 0; n < nthreads; n++ )
79
pthread_join(threads[n], NULL);
83
/* and check results */
84
EXEC SQL CONNECT TO :l_dbname;
85
EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
88
if( l_rows == (nthreads * iterations) )
89
printf("\nSuccess.\n");
91
printf("\nERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
93
/* close ECPG debug log? */
95
if( debugfile != NULL )
97
ECPGdebug(0, debugfile);
105
void *test_thread(void *arg)
107
long threadnum = (long)arg;
108
EXEC SQL BEGIN DECLARE SECTION;
110
char l_connection[128];
111
EXEC SQL END DECLARE SECTION;
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 )
119
printf("%s: ERROR: cannot connect to database!\n", l_connection);
124
/* insert into test_thread table */
125
for( l_i = 1; l_i <= iterations; l_i++ )
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);
132
printf("%s: ERROR: insert failed!\n", l_connection);
137
EXEC SQL DISCONNECT :l_connection;
138
printf("%s: done!\n", l_connection);