PostgreSQL Source Code  git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
thread-thread_implicit.c
Go to the documentation of this file.
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 #include <stdint.h>
15 #include <stdlib.h>
16 #include "ecpg_config.h"
17 
18 #ifndef WIN32
19 #include <pthread.h>
20 #else
21 #include <windows.h>
22 #include <locale.h>
23 #endif
24 
25 
26 #line 1 "regression.h"
27 
28 
29 
30 
31 
32 
33 #line 16 "thread_implicit.pgc"
34 
35 
36 void *test_thread(void *arg);
37 
38 int nthreads = 10;
39 int iterations = 20;
40 
41 int main()
42 {
43 #ifndef WIN32
44  pthread_t *threads;
45 #else
46  HANDLE *threads;
47 #endif
48  intptr_t n;
49  /* exec sql begin declare section */
50 
51 
52 #line 32 "thread_implicit.pgc"
53  int l_rows ;
54 /* exec sql end declare section */
55 #line 33 "thread_implicit.pgc"
56 
57 
58  /* Do not switch on debug output for regression tests. The threads get executed in
59  * more or less random order */
60  /* ECPGdebug(1, stderr); */
61 
62  /* setup test_thread table */
63  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); }
64 #line 40 "thread_implicit.pgc"
65 
66  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table test_thread", ECPGt_EOIT, ECPGt_EORT);}
67 #line 41 "thread_implicit.pgc"
68  /* DROP might fail */
69  { ECPGtrans(__LINE__, NULL, "commit");}
70 #line 42 "thread_implicit.pgc"
71 
72  { 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);}
73 #line 47 "thread_implicit.pgc"
74 
75  { ECPGtrans(__LINE__, NULL, "commit");}
76 #line 48 "thread_implicit.pgc"
77 
78  { ECPGdisconnect(__LINE__, "CURRENT");}
79 #line 49 "thread_implicit.pgc"
80 
81 
82  /* create, and start, threads */
83  threads = calloc(nthreads, sizeof(threads[0]));
84  if( threads == NULL )
85  {
86  fprintf(stderr, "Cannot alloc memory\n");
87  return 1;
88  }
89  for( n = 0; n < nthreads; n++ )
90  {
91 #ifndef WIN32
92  pthread_create(&threads[n], NULL, test_thread, (void *) (n + 1));
93 #else
94  threads[n] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) (void (*) (void)) test_thread, (void *) (n+1), 0, NULL);
95 #endif
96  }
97 
98  /* wait for thread completion */
99 #ifndef WIN32
100  for( n = 0; n < nthreads; n++ )
101  {
102  pthread_join(threads[n], NULL);
103  }
104 #else
105  WaitForMultipleObjects(nthreads, threads, TRUE, INFINITE);
106 #endif
107  free(threads);
108 
109  /* and check results */
110  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); }
111 #line 79 "thread_implicit.pgc"
112 
113  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "select count ( * ) from test_thread", ECPGt_EOIT,
114  ECPGt_int,&(l_rows),(long)1,(long)1,sizeof(int),
115  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EORT);}
116 #line 80 "thread_implicit.pgc"
117 
118  { ECPGtrans(__LINE__, NULL, "commit");}
119 #line 81 "thread_implicit.pgc"
120 
121  { ECPGdisconnect(__LINE__, "CURRENT");}
122 #line 82 "thread_implicit.pgc"
123 
124  if( l_rows == (nthreads * iterations) )
125  printf("Success.\n");
126  else
127  printf("ERROR: Failure - expecting %d rows, got %d.\n", nthreads * iterations, l_rows);
128 
129  return 0;
130 }
131 
132 void *test_thread(void *arg)
133 {
134  long threadnum = (intptr_t) arg;
135 
136  /* exec sql begin declare section */
137 
138 
139 
140 #line 96 "thread_implicit.pgc"
141  int l_i ;
142 
143 #line 97 "thread_implicit.pgc"
144  char l_connection [ 128 ] ;
145 /* exec sql end declare section */
146 #line 98 "thread_implicit.pgc"
147 
148 
149  /* build up connection name, and connect to database */
150 #ifndef _MSC_VER
151  snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
152 #else
153  _snprintf(l_connection, sizeof(l_connection), "thread_%03ld", threadnum);
154 #endif
155  /* exec sql whenever sqlerror sqlprint ; */
156 #line 106 "thread_implicit.pgc"
157 
158  { ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , l_connection, 0);
159 #line 107 "thread_implicit.pgc"
160 
161 if (sqlca.sqlcode < 0) sqlprint();}
162 #line 107 "thread_implicit.pgc"
163 
164  if( sqlca.sqlcode != 0 )
165  {
166  printf("%s: ERROR: cannot connect to database!\n", l_connection);
167  return NULL;
168  }
169  { ECPGtrans(__LINE__, NULL, "begin");
170 #line 113 "thread_implicit.pgc"
171 
172 if (sqlca.sqlcode < 0) sqlprint();}
173 #line 113 "thread_implicit.pgc"
174 
175 
176  /* insert into test_thread table */
177  for( l_i = 1; l_i <= iterations; l_i++ )
178  {
179  { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into test_thread ( thread , iteration ) values ( $1 , $2 )",
180  ECPGt_char,(l_connection),(long)128,(long)1,(128)*sizeof(char),
181  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L,
182  ECPGt_int,&(l_i),(long)1,(long)1,sizeof(int),
183  ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
184 #line 118 "thread_implicit.pgc"
185 
186 if (sqlca.sqlcode < 0) sqlprint();}
187 #line 118 "thread_implicit.pgc"
188 
189  if( sqlca.sqlcode != 0 )
190  printf("%s: ERROR: insert failed!\n", l_connection);
191  }
192 
193  /* all done */
194  { ECPGtrans(__LINE__, NULL, "commit");
195 #line 124 "thread_implicit.pgc"
196 
197 if (sqlca.sqlcode < 0) sqlprint();}
198 #line 124 "thread_implicit.pgc"
199 
200  { ECPGdisconnect(__LINE__, l_connection);
201 #line 125 "thread_implicit.pgc"
202 
203 if (sqlca.sqlcode < 0) sqlprint();}
204 #line 125 "thread_implicit.pgc"
205 
206  return NULL;
207 }
bool ECPGdisconnect(int lineno, const char *connection_name)
Definition: connect.c:676
bool ECPGconnect(int lineno, int c, const char *name, const char *user, const char *passwd, const char *connection_name, int autocommit)
Definition: connect.c:255
@ ECPGst_normal
Definition: ecpgtype.h:97
@ ECPGt_EOIT
Definition: ecpgtype.h:62
@ ECPGt_int
Definition: ecpgtype.h:44
@ ECPGt_NO_INDICATOR
Definition: ecpgtype.h:64
@ ECPGt_EORT
Definition: ecpgtype.h:63
@ ECPGt_char
Definition: ecpgtype.h:43
void sqlprint(void)
Definition: error.c:334
bool ECPGdo(const int lineno, const int compat, const int force_indicator, const char *connection_name, const bool questionmarks, const int st, const char *query,...)
Definition: execute.c:2277
#define calloc(a, b)
Definition: header.h:55
#define free(a)
Definition: header.h:65
bool ECPGtrans(int lineno, const char *connection_name, const char *transaction)
Definition: misc.c:160
void * arg
#define snprintf
Definition: port.h:238
#define fprintf
Definition: port.h:242
#define printf(...)
Definition: port.h:244
#define sqlca
Definition: sqlca.h:59
int iterations
int nthreads
void * test_thread(void *arg)
int main()