2006-08-02 14:14:04 +00:00
|
|
|
/* Processed by ecpg (4.2.1) */
|
|
|
|
/* These include files are added by the preprocessor */
|
|
|
|
#include <ecpgtype.h>
|
|
|
|
#include <ecpglib.h>
|
|
|
|
#include <ecpgerrno.h>
|
|
|
|
#include <sqlca.h>
|
|
|
|
/* End of automatic include section */
|
|
|
|
|
|
|
|
#line 1 "thread.pgc"
|
|
|
|
/*
|
|
|
|
* Thread test program
|
|
|
|
* by Philip Yarra & Lee Kindness.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#ifndef ENABLE_THREAD_SAFETY
|
|
|
|
int
|
|
|
|
main(void)
|
|
|
|
{
|
|
|
|
printf("Success.\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
#undef DEBUG
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-29 13:23:27 +00:00
|
|
|
#line 1 "regression.h"
|
2006-08-02 14:14:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#line 19 "thread.pgc"
|
|
|
|
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
void *test_thread(void *arg);
|
2006-08-02 14:14:04 +00:00
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
int nthreads = 10;
|
|
|
|
int iterations = 20;
|
2006-08-02 14:14:04 +00:00
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
2006-08-02 14:14:04 +00:00
|
|
|
{
|
2006-10-04 00:30:14 +00:00
|
|
|
pthread_t *threads;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
/* exec sql begin declare section */
|
|
|
|
|
|
|
|
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 31 "thread.pgc"
|
2006-10-04 00:30:14 +00:00
|
|
|
int l_rows;
|
|
|
|
|
2006-08-02 14:14:04 +00:00
|
|
|
/* exec sql end declare section */
|
|
|
|
#line 32 "thread.pgc"
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
/*
|
|
|
|
* Switch off debug output for regression tests. The threads get executed
|
|
|
|
* in more or less random order
|
|
|
|
*/
|
|
|
|
ECPGdebug(0, stderr);
|
2006-08-02 14:14:04 +00:00
|
|
|
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
/* setup test_thread table */
|
|
|
|
{
|
|
|
|
ECPGconnect(__LINE__, 0, "regress1", NULL, NULL, NULL, 0);
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 41 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGdo(__LINE__, 0, 1, NULL, "drop table test_thread ", ECPGt_EOIT, ECPGt_EORT);
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 42 "thread.pgc"
|
2006-10-04 00:30:14 +00:00
|
|
|
/* DROP might fail */
|
|
|
|
{
|
|
|
|
ECPGtrans(__LINE__, NULL, "commit");
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 43 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGdo(__LINE__, 0, 1, NULL, "create table test_thread ( tstamp timestamp not null default cast( timeofday () as timestamp ) , thread TEXT not null , iteration integer not null , primary key( thread , iteration ) ) ", ECPGt_EOIT, ECPGt_EORT);
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 48 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGtrans(__LINE__, NULL, "commit");
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 49 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGdisconnect(__LINE__, "CURRENT");
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 50 "thread.pgc"
|
|
|
|
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
/* create, and start, threads */
|
|
|
|
threads = calloc(nthreads, sizeof(pthread_t));
|
|
|
|
if (threads == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Cannot alloc memory\n");
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
for (n = 0; n < nthreads; n++)
|
|
|
|
{
|
|
|
|
pthread_create(&threads[n], NULL, test_thread, (void *) (n + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* wait for thread completion */
|
|
|
|
for (n = 0; n < nthreads; n++)
|
|
|
|
{
|
|
|
|
pthread_join(threads[n], NULL);
|
|
|
|
}
|
|
|
|
free(threads);
|
|
|
|
|
|
|
|
/* and check results */
|
|
|
|
{
|
|
|
|
ECPGconnect(__LINE__, 0, "regress1", NULL, NULL, NULL, 0);
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 72 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGdo(__LINE__, 0, 1, NULL, "select count (*) from test_thread ", ECPGt_EOIT,
|
|
|
|
ECPGt_int, &(l_rows), (long) 1, (long) 1, sizeof(int),
|
|
|
|
ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EORT);
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 73 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGtrans(__LINE__, NULL, "commit");
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 74 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGdisconnect(__LINE__, "CURRENT");
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 75 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
if (l_rows == (nthreads * iterations))
|
|
|
|
printf("Success.\n");
|
|
|
|
else
|
|
|
|
printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
|
2006-08-02 14:14:04 +00:00
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
return (0);
|
2006-08-02 14:14:04 +00:00
|
|
|
}
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
void *
|
|
|
|
test_thread(void *arg)
|
2006-08-02 14:14:04 +00:00
|
|
|
{
|
2006-10-04 00:30:14 +00:00
|
|
|
long threadnum = (long) arg;
|
|
|
|
|
|
|
|
/* exec sql begin declare section */
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 88 "thread.pgc"
|
2006-10-04 00:30:14 +00:00
|
|
|
int l_i;
|
|
|
|
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 89 "thread.pgc"
|
2006-10-04 00:30:14 +00:00
|
|
|
char l_connection[128];
|
|
|
|
|
2006-08-02 14:14:04 +00:00
|
|
|
/* exec sql end declare section */
|
|
|
|
#line 90 "thread.pgc"
|
|
|
|
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
/* build up connection name, and connect to database */
|
|
|
|
snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
|
|
|
|
/* exec sql whenever sqlerror sqlprint ; */
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 94 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGconnect(__LINE__, 0, "regress1", NULL, NULL, l_connection, 0);
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 95 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
if (sqlca.sqlcode < 0)
|
|
|
|
sqlprint();
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 95 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
if (sqlca.sqlcode != 0)
|
|
|
|
{
|
|
|
|
printf("%s: ERROR: cannot connect to database!\n", l_connection);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ECPGtrans(__LINE__, l_connection, "begin transaction ");
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 101 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
if (sqlca.sqlcode < 0)
|
|
|
|
sqlprint();
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 101 "thread.pgc"
|
|
|
|
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
/* insert into test_thread table */
|
|
|
|
for (l_i = 1; l_i <= iterations; l_i++)
|
|
|
|
{
|
2006-08-02 14:14:04 +00:00
|
|
|
#ifdef DEBUG
|
2006-10-04 00:30:14 +00:00
|
|
|
printf("%s: inserting %d\n", l_connection, l_i);
|
2006-08-02 14:14:04 +00:00
|
|
|
#endif
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGdo(__LINE__, 0, 1, l_connection, "insert into test_thread ( thread , iteration ) values( ? , ? ) ",
|
|
|
|
ECPGt_char, (l_connection), (long) 128, (long) 1, (128) * sizeof(char),
|
|
|
|
ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L,
|
|
|
|
ECPGt_int, &(l_i), (long) 1, (long) 1, sizeof(int),
|
|
|
|
ECPGt_NO_INDICATOR, NULL, 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 109 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
if (sqlca.sqlcode < 0)
|
|
|
|
sqlprint();
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 109 "thread.pgc"
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2006-10-04 00:30:14 +00:00
|
|
|
if (sqlca.sqlcode == 0)
|
|
|
|
printf("%s: insert done\n", l_connection);
|
|
|
|
else
|
|
|
|
printf("%s: ERROR: insert failed!\n", l_connection);
|
2006-08-02 14:14:04 +00:00
|
|
|
#endif
|
2006-10-04 00:30:14 +00:00
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
/* all done */
|
|
|
|
{
|
|
|
|
ECPGtrans(__LINE__, l_connection, "commit");
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 119 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
if (sqlca.sqlcode < 0)
|
|
|
|
sqlprint();
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 119 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
{
|
|
|
|
ECPGdisconnect(__LINE__, l_connection);
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 120 "thread.pgc"
|
|
|
|
|
2006-10-04 00:30:14 +00:00
|
|
|
if (sqlca.sqlcode < 0)
|
|
|
|
sqlprint();
|
|
|
|
}
|
2006-08-02 14:14:04 +00:00
|
|
|
#line 120 "thread.pgc"
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2006-10-04 00:30:14 +00:00
|
|
|
printf("%s: done!\n", l_connection);
|
2006-08-02 14:14:04 +00:00
|
|
|
#endif
|
2006-10-04 00:30:14 +00:00
|
|
|
return (NULL);
|
2006-08-02 14:14:04 +00:00
|
|
|
}
|
2006-10-04 00:30:14 +00:00
|
|
|
|
|
|
|
#endif /* ENABLE_THREAD_SAFETY */
|