7
#include "ecpg_config.h"
9
#ifndef ENABLE_THREAD_SAFETY
13
printf("No threading enabled.\n");
23
exec sql include ../regression;
25
void *test_thread(void *arg);
30
int main(int argc, char *argv[])
38
EXEC SQL BEGIN DECLARE SECTION;
40
EXEC SQL END DECLARE SECTION;
42
/* Do not switch on debug output for regression tests. The threads get executed in
43
* more or less random order */
44
/* ECPGdebug(1, stderr); */
46
/* setup test_thread table */
47
EXEC SQL CONNECT TO REGRESSDB1;
48
EXEC SQL DROP TABLE test_thread; /* DROP might fail */
51
test_thread(tstamp TIMESTAMP NOT NULL DEFAULT CAST(timeofday() AS TIMESTAMP),
53
iteration INTEGER NOT NULL,
54
PRIMARY KEY(thread, iteration));
58
/* create, and start, threads */
59
threads = calloc(nthreads, sizeof(threads[0]));
62
fprintf(stderr, "Cannot alloc memory\n");
65
for( n = 0; n < nthreads; n++ )
68
pthread_create(&threads[n], NULL, test_thread, (void *) (long) (n + 1));
70
threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_thread, (void *) (n+1), 0, NULL);
74
/* wait for thread completion */
76
for( n = 0; n < nthreads; n++ )
78
pthread_join(threads[n], NULL);
81
WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
85
/* and check results */
86
EXEC SQL CONNECT TO REGRESSDB1;
87
EXEC SQL SELECT COUNT(*) INTO :l_rows FROM test_thread;
90
if( l_rows == (nthreads * iterations) )
93
printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
98
void *test_thread(void *arg)
100
long threadnum = (long)arg;
101
EXEC SQL BEGIN DECLARE SECTION;
103
char l_connection[128];
104
EXEC SQL END DECLARE SECTION;
106
/* build up connection name, and connect to database */
107
#ifndef WIN32_ONLY_COMPILER
108
snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
110
_snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
112
EXEC SQL WHENEVER sqlerror sqlprint;
113
EXEC SQL CONNECT TO REGRESSDB1 AS :l_connection;
114
if( sqlca.sqlcode != 0 )
116
printf("%s: ERROR: cannot connect to database!\n", l_connection);
121
/* insert into test_thread table */
122
for( l_i = 1; l_i <= iterations; l_i++ )
124
EXEC SQL INSERT INTO test_thread(thread, iteration) VALUES(:l_connection, :l_i);
125
if( sqlca.sqlcode != 0 )
126
printf("%s: ERROR: insert failed!\n", l_connection);
131
EXEC SQL DISCONNECT :l_connection;
134
#endif /* ENABLE_THREAD_SAFETY */