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