1 /* Processed by ecpg (regression mode) */
2 /* These include files are added by the preprocessor */
3 #include <ecpglib.h>
4 #include <ecpgerrno.h>
5 #include <sqlca.h>
6 /* End of automatic include section */
7 #define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))
8
9 #line 1 "thread_implicit.pgc"
10 /*
11 * Thread test program
12 * by Lee Kindness.
13 */
14
15 #include <stdlib.h>
16 #include "ecpg_config.h"
17
18 #ifndef ENABLE_THREAD_SAFETY
19 int
main(void)20 main(void)
21 {
22 printf("No threading enabled.\n");
23 return 0;
24 }
25 #else
26 #ifndef WIN32
27 #include <pthread.h>
28 #else
29 #include <windows.h>
30 #include <locale.h>
31 #endif
32
33
34 #line 1 "regression.h"
35
36
37
38
39
40
41 #line 24 "thread_implicit.pgc"
42
43
44 void *test_thread(void *arg);
45
46 int nthreads = 10;
47 int iterations = 20;
48
main()49 int main()
50 {
51 #ifndef WIN32
52 pthread_t *threads;
53 #else
54 HANDLE *threads;
55 #endif
56 int n;
57 /* exec sql begin declare section */
58
59
60 #line 40 "thread_implicit.pgc"
61 int l_rows ;
62 /* exec sql end declare section */
63 #line 41 "thread_implicit.pgc"
64
65
66 /* Do not switch on debug output for regression tests. The threads get executed in
67 * more or less random order */
68 /* ECPGdebug(1, stderr); */
69
70 /* setup test_thread table */
71 { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); }
72 #line 48 "thread_implicit.pgc"
73
74 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_thread", ECPGt_EOIT, ECPGt_EORT);}
75 #line 49 "thread_implicit.pgc"
76 /* DROP might fail */
77 { ECPGtrans(__LINE__, NULL, "commit");}
78 #line 50 "thread_implicit.pgc"
79
80 { 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);}
81 #line 55 "thread_implicit.pgc"
82
83 { ECPGtrans(__LINE__, NULL, "commit");}
84 #line 56 "thread_implicit.pgc"
85
86 { ECPGdisconnect(__LINE__, "CURRENT");}
87 #line 57 "thread_implicit.pgc"
88
89
90 /* create, and start, threads */
91 threads = calloc(nthreads, sizeof(threads[0]));
92 if( threads == NULL )
93 {
94 fprintf(stderr, "Cannot alloc memory\n");
95 return 1;
96 }
97 for( n = 0; n < nthreads; n++ )
98 {
99 #ifndef WIN32
100 pthread_create(&threads[n], NULL, test_thread, (void *) (long) (n + 1));
101 #else
102 threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) test_thread, (void *) (n+1), 0, NULL);
103 #endif
104 }
105
106 /* wait for thread completion */
107 #ifndef WIN32
108 for( n = 0; n < nthreads; n++ )
109 {
110 pthread_join(threads[n], NULL);
111 }
112 #else
113 WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
114 #endif
115 free(threads);
116
117 /* and check results */
118 { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); }
119 #line 87 "thread_implicit.pgc"
120
121 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_thread", ECPGt_EOIT,
122 ECPGt_int,&(l_rows),(long)1,(long)1,sizeof(int),
123 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
124 #line 88 "thread_implicit.pgc"
125
126 { ECPGtrans(__LINE__, NULL, "commit");}
127 #line 89 "thread_implicit.pgc"
128
129 { ECPGdisconnect(__LINE__, "CURRENT");}
130 #line 90 "thread_implicit.pgc"
131
132 if( l_rows == (nthreads * iterations) )
133 printf("Success.\n");
134 else
135 printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
136
137 return 0;
138 }
139
test_thread(void * arg)140 void *test_thread(void *arg)
141 {
142 long threadnum = (long)arg;
143
144 /* exec sql begin declare section */
145
146
147
148 #line 104 "thread_implicit.pgc"
149 int l_i ;
150
151 #line 105 "thread_implicit.pgc"
152 char l_connection [ 128 ] ;
153 /* exec sql end declare section */
154 #line 106 "thread_implicit.pgc"
155
156
157 /* build up connection name, and connect to database */
158 #ifndef _MSC_VER
159 snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
160 #else
161 _snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
162 #endif
163 /* exec sql whenever sqlerror sqlprint ; */
164 #line 114 "thread_implicit.pgc"
165
166 { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , l_connection, 0);
167 #line 115 "thread_implicit.pgc"
168
169 if (sqlca.sqlcode < 0) sqlprint();}
170 #line 115 "thread_implicit.pgc"
171
172 if( sqlca.sqlcode != 0 )
173 {
174 printf("%s: ERROR: cannot connect to database!\n", l_connection);
175 return NULL;
176 }
177 { ECPGtrans(__LINE__, NULL, "begin");
178 #line 121 "thread_implicit.pgc"
179
180 if (sqlca.sqlcode < 0) sqlprint();}
181 #line 121 "thread_implicit.pgc"
182
183
184 /* insert into test_thread table */
185 for( l_i = 1; l_i <= iterations; l_i++ )
186 {
187 { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into test_thread ( thread , iteration ) values ( $1 , $2 )",
188 ECPGt_char,(l_connection),(long)128,(long)1,(128)*sizeof(char),
189 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
190 ECPGt_int,&(l_i),(long)1,(long)1,sizeof(int),
191 ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
192 #line 126 "thread_implicit.pgc"
193
194 if (sqlca.sqlcode < 0) sqlprint();}
195 #line 126 "thread_implicit.pgc"
196
197 if( sqlca.sqlcode != 0 )
198 printf("%s: ERROR: insert failed!\n", l_connection);
199 }
200
201 /* all done */
202 { ECPGtrans(__LINE__, NULL, "commit");
203 #line 132 "thread_implicit.pgc"
204
205 if (sqlca.sqlcode < 0) sqlprint();}
206 #line 132 "thread_implicit.pgc"
207
208 { ECPGdisconnect(__LINE__, l_connection);
209 #line 133 "thread_implicit.pgc"
210
211 if (sqlca.sqlcode < 0) sqlprint();}
212 #line 133 "thread_implicit.pgc"
213
214 return NULL;
215 }
216 #endif /* ENABLE_THREAD_SAFETY */
217