1 /* Processed by ecpg (regression mode) */
2 /* These include files are added by the preprocessor */
6 /* End of automatic include section */
7 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
9 #line 1 "thread_implicit.pgc"
16 #include "ecpg_config.h"
18 #ifndef ENABLE_THREAD_SAFETY
22 printf("No threading enabled.\n");
33 #line 1 "regression.h"
40 #line 23 "thread_implicit.pgc"
43 void *test_thread(void *arg);
56 /* exec sql begin declare section */
59 #line 39 "thread_implicit.pgc"
61 /* exec sql end declare section */
62 #line 40 "thread_implicit.pgc"
65 /* Do not switch on debug output for regression tests. The threads get executed in
66 * more or less random order */
67 /* ECPGdebug(1, stderr); */
69 /* setup test_thread table */
70 { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
71 #line 47 "thread_implicit.pgc"
73 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_thread", ECPGt_EOIT, ECPGt_EORT);}
74 #line 48 "thread_implicit.pgc"
76 { ECPGtrans(__LINE__, NULL, "commit");}
77 #line 49 "thread_implicit.pgc"
79 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "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);}
80 #line 54 "thread_implicit.pgc"
82 { ECPGtrans(__LINE__, NULL, "commit");}
83 #line 55 "thread_implicit.pgc"
85 { ECPGdisconnect(__LINE__, "CURRENT");}
86 #line 56 "thread_implicit.pgc"
89 /* create, and start, threads */
90 threads = calloc(nthreads, sizeof(threads[0]));
93 fprintf(stderr, "Cannot alloc memory\n");
96 for( n = 0; n < nthreads; n++ )
99 pthread_create(&threads[n], NULL, test_thread, (void *) (long) (n + 1));
101 threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_thread, (void *) (n+1), 0, NULL);
105 /* wait for thread completion */
107 for( n = 0; n < nthreads; n++ )
109 pthread_join(threads[n], NULL);
112 WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
116 /* and check results */
117 { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , NULL, 0); }
118 #line 86 "thread_implicit.pgc"
120 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_thread", ECPGt_EOIT,
121 ECPGt_int,&(l_rows),(long)1,(long)1,sizeof(int),
122 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
123 #line 87 "thread_implicit.pgc"
125 { ECPGtrans(__LINE__, NULL, "commit");}
126 #line 88 "thread_implicit.pgc"
128 { ECPGdisconnect(__LINE__, "CURRENT");}
129 #line 89 "thread_implicit.pgc"
131 if( l_rows == (nthreads * iterations) )
132 printf("Success.\n");
134 printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
139 void *test_thread(void *arg)
141 long threadnum = (long)arg;
142 /* exec sql begin declare section */
146 #line 102 "thread_implicit.pgc"
149 #line 103 "thread_implicit.pgc"
150 char l_connection [ 128 ] ;
151 /* exec sql end declare section */
152 #line 104 "thread_implicit.pgc"
155 /* build up connection name, and connect to database */
156 #ifndef WIN32_ONLY_COMPILER
157 snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
159 _snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
161 /* exec sql whenever sqlerror sqlprint ; */
162 #line 112 "thread_implicit.pgc"
164 { ECPGconnect(__LINE__, 0, "regress1" , NULL, NULL , l_connection, 0);
165 #line 113 "thread_implicit.pgc"
167 if (sqlca.sqlcode < 0) sqlprint();}
168 #line 113 "thread_implicit.pgc"
170 if( sqlca.sqlcode != 0 )
172 printf("%s: ERROR: cannot connect to database!\n", l_connection);
175 { ECPGtrans(__LINE__, NULL, "begin");
176 #line 119 "thread_implicit.pgc"
178 if (sqlca.sqlcode < 0) sqlprint();}
179 #line 119 "thread_implicit.pgc"
182 /* insert into test_thread table */
183 for( l_i = 1; l_i <= iterations; l_i++ )
185 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into test_thread ( thread , iteration ) values ( $1 , $2 )",
186 ECPGt_char,(l_connection),(long)128,(long)1,(128)*sizeof(char),
187 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
188 ECPGt_int,&(l_i),(long)1,(long)1,sizeof(int),
189 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
190 #line 124 "thread_implicit.pgc"
192 if (sqlca.sqlcode < 0) sqlprint();}
193 #line 124 "thread_implicit.pgc"
195 if( sqlca.sqlcode != 0 )
196 printf("%s: ERROR: insert failed!\n", l_connection);
200 { ECPGtrans(__LINE__, NULL, "commit");
201 #line 130 "thread_implicit.pgc"
203 if (sqlca.sqlcode < 0) sqlprint();}
204 #line 130 "thread_implicit.pgc"
206 { ECPGdisconnect(__LINE__, l_connection);
207 #line 131 "thread_implicit.pgc"
209 if (sqlca.sqlcode < 0) sqlprint();}
210 #line 131 "thread_implicit.pgc"
214 #endif /* ENABLE_THREAD_SAFETY */